diff --git a/browse/src/browser-manager.ts b/browse/src/browser-manager.ts index 255f6b583..c93fa5971 100644 --- a/browse/src/browser-manager.ts +++ b/browse/src/browser-manager.ts @@ -727,6 +727,12 @@ export class BrowserManager { } async close() { + // unref'd race timer: without unref, every successful close still pins + // the caller's event loop for the full window. + const raceTimeout = (ms: number) => new Promise((resolve) => { + const t = setTimeout(() => resolve(false), ms); + (t as { unref?: () => void }).unref?.(); + }); if (this.browser || (this.connectionMode === 'headed' && this.context)) { if (this.connectionMode === 'headed') { // Headed/persistent context mode: close the context (which closes the browser) @@ -734,15 +740,24 @@ export class BrowserManager { if (this.browser) this.browser.removeAllListeners('disconnected'); await Promise.race([ this.context ? this.context.close() : Promise.resolve(), - new Promise(resolve => setTimeout(resolve, 5000)), + raceTimeout(5000), ]).catch(() => {}); } else { - // Launched mode: close the browser we spawned + // Launched mode: close the browser we spawned. this.browser.removeAllListeners('disconnected'); - await Promise.race([ - this.browser.close(), - new Promise(resolve => setTimeout(resolve, 5000)), - ]).catch(() => {}); + // Grab the child handle BEFORE the race: nulling this.browser after a + // race-timeout used to ABANDON a live Chromium whose sockets kept the + // caller's event loop (and keep-alive connections into test servers) + // open forever — the intermittent whole-suite wedge. If graceful close + // doesn't finish in time, the child gets SIGKILL, not freedom. + const child = this.browser.process?.(); + const closed = await Promise.race([ + this.browser.close().then(() => true as const), + raceTimeout(5000), + ]).catch(() => false as const); + if (closed === false && child && child.exitCode === null && !child.killed) { + try { child.kill('SIGKILL'); } catch { /* already gone */ } + } } this.browser = null; } diff --git a/browse/test/batch.test.ts b/browse/test/batch.test.ts index a6ee8a2b0..452f60e08 100644 --- a/browse/test/batch.test.ts +++ b/browse/test/batch.test.ts @@ -43,7 +43,7 @@ beforeAll(async () => { }); afterAll(async () => { - try { testServer.server.stop(); } catch {} + try { testServer.server.stop(true); } catch {} // force-close keep-alives — a lingering Chromium connection otherwise blocks stop() forever // Close only this file's own browser — never process.exit(): bun test runs // all files in one process, so a delayed exit kills the whole suite // (see test/no-suicide-exit.test.ts). close() can hang when the browser diff --git a/browse/test/commands.test.ts b/browse/test/commands.test.ts index 8b33fdb75..caf1464df 100644 --- a/browse/test/commands.test.ts +++ b/browse/test/commands.test.ts @@ -95,7 +95,7 @@ beforeAll(async () => { }); afterAll(async () => { - try { testServer.server.stop(); } catch {} + try { testServer.server.stop(true); } catch {} // force-close keep-alives — a lingering Chromium connection otherwise blocks stop() forever // Close only this file's own browser — never process.exit(): bun test runs // all files in one process, so a delayed exit kills the whole suite // (see test/no-suicide-exit.test.ts). close() can hang when the browser diff --git a/browse/test/handoff.test.ts b/browse/test/handoff.test.ts index a395ab51d..22d87b3af 100644 --- a/browse/test/handoff.test.ts +++ b/browse/test/handoff.test.ts @@ -27,7 +27,7 @@ beforeAll(async () => { }); afterAll(async () => { - try { testServer.server.stop(); } catch {} + try { testServer.server.stop(true); } catch {} // force-close keep-alives — a lingering Chromium connection otherwise blocks stop() forever // Close only this file's own browser — never process.exit(): bun test runs // all files in one process, so a delayed exit kills the whole suite // (see test/no-suicide-exit.test.ts). close() can hang when the browser diff --git a/browse/test/snapshot.test.ts b/browse/test/snapshot.test.ts index d3c012eaf..51d2b24f1 100644 --- a/browse/test/snapshot.test.ts +++ b/browse/test/snapshot.test.ts @@ -32,7 +32,7 @@ beforeAll(async () => { }); afterAll(async () => { - try { testServer.server.stop(); } catch {} + try { testServer.server.stop(true); } catch {} // force-close keep-alives — a lingering Chromium connection otherwise blocks stop() forever // Close only this file's own browser — never process.exit(): bun test runs // all files in one process, so a delayed exit kills the whole suite // (see test/no-suicide-exit.test.ts). close() can hang when the browser