Skip to content

Commit 0d33560

Browse files
committed
Properly wait for existing Chrom* to exit
1 parent f575433 commit 0d33560

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/interceptors/chromium-based-interceptors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
Browser,
1111
LaunchOptions
1212
} from '../browsers';
13-
import { delay, readFile, deleteFolder, listRunningProcesses, windowsClose } from '../util';
13+
import { delay, readFile, deleteFolder, listRunningProcesses, windowsClose, waitForExit } from '../util';
1414
import { HideWarningServer } from '../hide-warning-server';
1515
import { Interceptor } from '.';
1616
import { reportError } from '../error-tracking';
@@ -234,7 +234,7 @@ abstract class ExistingChromiumBasedInterceptor implements Interceptor {
234234
process.kill(existingPid);
235235
}
236236

237-
await delay(1000);
237+
await waitForExit(existingPid);
238238
}
239239

240240
const browserDetails = await getBrowserDetails(this.config, this.variantName);

src/util.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,27 @@ export async function listRunningProcesses(): Promise<Array<Proc>> {
167167
}
168168
}
169169

170+
export async function waitForExit(pid: number, timeout: number = 5000): Promise<void> {
171+
const startTime = Date.now();
172+
173+
while (true) {
174+
try {
175+
process.kill(pid, 0) as void | boolean;
176+
177+
// Didn't throw. If we haven't timed out, check again after 250ms:
178+
if (Date.now() - startTime > timeout) {
179+
throw new Error("Process did not exit before timeout");
180+
}
181+
await delay(250);
182+
} catch (e) {
183+
if ((e as Error & { code?: string }).code === 'ESRCH') {
184+
return; // Process doesn't exist! We're done.
185+
}
186+
else throw e;
187+
}
188+
}
189+
}
190+
170191
// Cleanly close (simulate closing the main window) on a specific windows process
171192
export async function windowsClose(pid: number) {
172193
await spawnToResult('taskkill', [

0 commit comments

Comments
 (0)