Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: reap the browser-test Chromium when its graceful close outlasts the budget (#10830)
Adam Eivy1e47e9739e9e
diff --git a/client/src/components/Drawer.browser.test.js b/client/src/components/Drawer.browser.test.jsindex 5d0fcd7df5..55c1651594 100644--- a/client/src/components/Drawer.browser.test.js+++ b/client/src/components/Drawer.browser.test.js@@ -90,7 +90,7 @@ describe.skipIf(!chrome && !auditCdp)('Drawer native disabled-state focus bounda});}// close() disconnects an attached CDP client; only a launched browser is- // owned by this suite and terminated by Playwright.+ // owned by this suite and reaped by the fixture.await fixture?.close();});diff --git a/client/src/test/README.md b/client/src/test/README.mdindex 1f642f2896..d6f8ab37b2 100644--- a/client/src/test/README.md+++ b/client/src/test/README.md@@ -10,7 +10,7 @@ a new one.| --- | --- || `actWarnings.js` | React act(...) warning diagnostics — records the test active when a warning was observed, without claiming to identify where asynchronous work began. || `ariaRefAssertions.js` | Asserts a rendered tree's `aria-controls`/`aria-labelledby` IDREFs all resolve to a real element. |-| `browserFixture.js` | `startBrowserFixture` — phase-bounded Vite + Chromium + warmup startup for `*.browser.test.js` (a stalled Chromium launch is retried once with a fresh process), naming the stalled phase and closing everything it started; a timed-out launch attempt reports redacted Linux facts about its own browser child, and on GitHub Actions the ready/failure lines are kept in the step summary. |+| `browserFixture.js` | `startBrowserFixture` — phase-bounded Vite + Chromium + warmup startup for `*.browser.test.js` (a stalled Chromium launch is retried once with a fresh process), naming the stalled phase and closing everything it started (a real launch is reaped through its own Playwright browser server: graceful close, then that server's kill, confirmed by the process's exit status); a timed-out launch attempt reports redacted Linux facts about its own browser child, and on GitHub Actions the ready/failure lines are kept in the step summary. || `classNameScan.js` | Shared string-literal scanner (comment-stripping + literal walk) for the class-string convention guards. || `dndKeyboardDrag.js` | Drives a real `@dnd-kit` keyboard drag under happy-dom, stubbing the layout happy-dom can't provide. || `downloadPreflightConfirm.js` | `clickStartDownload` — waits for the confirm button to be enabled (built on `enabledBarrier.js`) before clicking, then settles. |diff --git a/client/src/test/browserFixture.js b/client/src/test/browserFixture.jsindex f8e19a9476..c42b9723ee 100644--- a/client/src/test/browserFixture.js+++ b/client/src/test/browserFixture.js@@ -46,6 +46,18 @@* passing fixture's console output, so on GitHub Actions the ready and failure* lines are also appended to the job's step summary, which keeps the passing* timings for comparison with the runner's Chrome warm-up.+ *+ * Teardown stalls too (#10830): a graceful close of a system Chrome took 6–10s+ * on a busy macOS host, past the cleanup budget, which turned it into an+ * error (#10791) while the browser and its profile directory stayed behind.+ * Playwright's own kill of the same process takes ~100ms and removes the+ * profile. A `Browser` from `launch()` exposes no process, so a real launch+ * goes through Playwright's process-backed `launchServer()` and connects to+ * it: the fixture keeps the server, closes gracefully first, then kills only+ * that server's own process, and reports success only on its exit status. A+ * `{ launch }` stand-in — the CDP adapter attaching to an external browser+ * among them — keeps only `close()`, which never terminates what it attached+ * to.*/import { appendFileSync, closeSync, openSync, opendirSync, readSync } from 'node:fs';import { mkdtemp, rm } from 'node:fs/promises';@@ -148,13 +160,38 @@ const withDeadline = (promise, ms, onTimeout) => {return Promise.race([promise, deadline]).finally(() => clearTimeout(timer));};+// Resolves to null once the work succeeds within `ms`, or else to why it did not.+const failureWithin = (work, ms) => withDeadline(+ Promise.resolve().then(work).then(() => null, error => `failed: ${error.message}`),+ ms, () => timeoutError(ms),+).catch(() => `stalled after ${ms}ms`);++/**+ * Closes a browser server the fixture launched, inside `budgetMs`: a graceful+ * close for half of it, then the server's own kill, which signals only the+ * process Playwright spawned (and its process group) and resolves once that+ * process closed and its profile was removed. Each step counts only with the+ * child's exit status in hand, never because a signal was sent; when neither+ * produces one, the close rejects.+ */+const reapOwnedBrowser = async (server, budgetMs) => {+ const child = server.process();+ const confirmed = failure => failure+ ?? (child && (child.exitCode !== null || child.signalCode !== null) ? null : 'returned without an exit status');+ const closeFailure = confirmed(await failureWithin(() => server.close(), Math.floor(budgetMs / 2)));+ if (!closeFailure) return;+ const killFailure = confirmed(await failureWithin(() => server.kill(), Math.floor(budgetMs * 0.4)));+ if (killFailure) throw new Error(`browser close ${closeFailure}, then its kill ${killFailure}`);+};+/*** @param {object} options* @param {string} options.name - fixture label used in errors and the temp-dir prefix* @param {Function} options.createServer - vite's `createServer`* @param {(temp: string) => object} options.viteConfig - inline Vite config; `temp` is the fixture's private temp dir- * @param {object} options.chromium - playwright-core's `chromium`, or any `{ launch }` stand-in (e.g. one wrapping `connectOverCDP`)- * @param {object} [options.launchOptions] - extra `chromium.launch` options (executablePath, args, …)+ * @param {object} options.chromium - playwright-core's `chromium`, whose `launchServer` gives the fixture the browser+ * process to reap; or any `{ launch }` stand-in (e.g. one wrapping `connectOverCDP`), which it only ever `close()`s+ * @param {object} [options.launchOptions] - extra launch options (executablePath, args, …)* @param {(page: object, origin: string) => Promise<void>} [options.warmup] - first navigation; the page is closed afterward* @param {object} [options.phaseMs] - per-phase budgets; defaults to BROWSER_FIXTURE_PHASE_MS* @param {() => () => string} [options.observeLaunch] - starts observing a launch attempt; defaults to _observeOwnedBrowserLaunch@@ -172,21 +209,29 @@ export async function startBrowserFixture({const closers = [];const completed = [];let failed = false;+ // Every closer runs under its own cleanup deadline, so one stalled teardown+ // neither starves another nor outlives the `afterAll` hook (#10791).+ const closeBounded = ({ closer, label }) => withDeadline(Promise.resolve().then(closer), phaseMs.cleanup,+ () => new Error(`${label} close stalled after ${phaseMs.cleanup}ms`));+ // Nothing awaits a resource closed after the hook that acquired it gave up,+ // so a failure to close it is reported here rather than lost.+ const discard = owned => closeBounded(owned).catch((error) => {+ const line = `❌ ${name} browser fixture could not close a late ${owned.label}: ${error.message}`;+ console.error(line);+ retainLine(line);+ });// A resource acquired after its phase already failed is closed at once// instead of leaking past the hook that abandoned it.const own = (closer, label) => {- if (failed) closer().catch(() => {});+ if (failed) discard({ closer, label });else closers.push({ closer, label });};- // Every closer runs under its own cleanup deadline, concurrently, so one- // stalled teardown neither starves the other nor outlives the `afterAll`- // hook (#10791). The temp dir is removed whether or not a closer stalled.+ // Closers run concurrently, and the temp dir is removed whether or not one+ // stalled.const closeOwned = async () => {const errors = [];- await Promise.all(closers.splice(0).reverse().map(({ closer, label }) => withDeadline(- Promise.resolve().then(closer), phaseMs.cleanup,- () => new Error(`${label} close stalled after ${phaseMs.cleanup}ms`),- ).catch(error => errors.push(error.message))));+ await Promise.all(closers.splice(0).reverse().map(owned => closeBounded(owned)+ .catch(error => errors.push(error.message))));await rm(temp, { recursive: true, force: true });if (errors.length) throw new Error(errors.join('; '));};@@ -205,13 +250,35 @@ export async function startBrowserFixture({retried = '';return value;};+ // A real launch: Playwright's server owns the browser process and the+ // fixture's browser is its client, so closing reaps that process. A+ // stand-in without `launchServer` — an attached CDP browser above all —+ // keeps only its own `close()`.+ const ownsProcess = typeof chromium.launchServer === 'function';+ const launchBrowser = async (options, isAbandoned) => {+ if (!ownsProcess) {+ const browser = await chromium.launch(options);+ return { browser, closer: () => browser.close() };+ }+ const server = await chromium.launchServer(options);+ const closer = () => reapOwnedBrowser(server, phaseMs.cleanup);+ // Nobody will use a browser startup already gave up on: skip connecting.+ if (isAbandoned()) return { closer };+ try {+ return { browser: await chromium.connect(server.wsEndpoint(), { timeout: options.timeout }), closer };+ } catch (error) {+ discard({ closer, label: 'browser' });+ throw error;+ }+ };// Facts of every launch attempt that timed out, in attempt order, and the// recorder of the attempt in flight.const launchFacts = [];let recordTimedOutAttempt;// One Chromium process. A browser that arrives after its attempt was- // abandoned is closed at once; one that arrives in time joins `own`, which- // closes it at once if the fixture has failed meanwhile. Shortly before its+ // abandoned is closed at once, under the same bounded cleanup; one that+ // arrives in time joins `own`, which closes it at once if the fixture has+ // failed meanwhile. Shortly before its// deadline, an attempt still pending samples its child: Playwright's own// launch timeout kills that child at the deadline, so a later look would// describe the kill rather than the stall.@@ -229,16 +296,16 @@ export async function startBrowserFixture({try { facts = sample(); } catch { facts = 'unavailable'; }}, sampleAt);const settle = () => { settled = true; clearTimeout(timer); };- const launching = chromium.launch({+ const launching = launchBrowser({headless: true,timeout,...launchOptions,env: { ...process.env, ...launchOptions.env, TMPDIR: temp, TMP: temp, TEMP: temp },- }).then((launched) => {+ }, () => abandoned || failed).then(({ browser, closer }) => {settle();- if (abandoned) launched.close().catch(() => {});- else own(() => launched.close(), 'browser');- return launched;+ if (abandoned) discard({ closer, label: 'browser' });+ else own(closer, 'browser');+ return browser;}, (error) => { settle(); throw error; });const record = () => {if (recorded) return;diff --git a/client/src/test/browserFixture.test.js b/client/src/test/browserFixture.test.jsindex a0dd3ee952..a5357f6e2e 100644--- a/client/src/test/browserFixture.test.js+++ b/client/src/test/browserFixture.test.js@@ -14,6 +14,20 @@ const fakeVite = () => {return { server, createServer: vi.fn(async () => server) };};const fakeBrowser = (page) => ({ close: vi.fn(async () => {}), newPage: vi.fn(async () => page) });+// A process-backed launch: a Playwright browser server owning `child`, and+// the client browser `connect` hands the suite. By default a graceful close+// exits the child; `close`/`kill` replace that server's own steps.+const ownedServer = ({ close, kill } = {}) => {+ const child = { exitCode: null, signalCode: null };+ const server = { process: () => child, wsEndpoint: () => 'ws://127.0.0.1:1/owned',+ close: vi.fn(close ?? (async () => { child.exitCode = 0; })),+ kill: vi.fn(kill ?? (async () => { child.signalCode = 'SIGKILL'; })) };+ return { child, server };+};+const ownedChromium = (server, browser = fakeBrowser()) => ({+ launchServer: vi.fn(async () => server), connect: vi.fn(async () => browser),+});+const stalls = () => new Promise(() => {});// Each launch attempt's observation reports which attempt it described and// when it was taken, so a test can prove the facts came from before the kill.const fakeObserver = () => {@@ -135,11 +149,12 @@ describe('startBrowserFixture', () => {expect(server.listen).not.toHaveBeenCalled();});- it('closes the launched browser and Vite when the warmup page fails', async () => {+ it('closes the launched browser\'s own server and Vite when the warmup page fails', async () => {const { server, createServer } = fakeVite();const page = { close: vi.fn(async () => {}) };const browser = fakeBrowser(page);- const chromium = { launch: vi.fn(async () => browser) };+ const owned = ownedServer();+ const chromium = ownedChromium(owned.server, browser);const cause = new Error('net::ERR_CONNECTION_REFUSED');const { observeLaunch, samples } = fakeObserver();@@ -152,23 +167,105 @@ describe('startBrowserFixture', () => {expect(samples).toEqual([]);expect(error.cause).toBe(cause);expect(page.close).toHaveBeenCalledTimes(1);- expect(browser.close).toHaveBeenCalledTimes(1);+ expect(chromium.connect).toHaveBeenCalledWith('ws://127.0.0.1:1/owned', { timeout: 25 });+ // A graceful close that exits the child needs no kill.+ expect(owned.server.close).toHaveBeenCalledTimes(1);+ expect(owned.child.exitCode).toBe(0);+ expect(owned.server.kill).not.toHaveBeenCalled();expect(server.close).toHaveBeenCalledTimes(1);});- it('bounds close() so a stalled browser teardown rejects naming it, still closes Vite, and removes the temp dir', async () => {+ it('kills an owned browser whose graceful close stalls, inside the cleanup budget, once it has an exit status', async () => {+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });+ const { server, createServer } = fakeVite();+ const owned = ownedServer({ close: stalls });+ const chromium = ownedChromium(owned.server);+ const fixture = await startBrowserFixture({ name: 'example', createServer, viteConfig: () => SCOPED, chromium, phaseMs: PHASE_MS });+ expect(chromium.launchServer).toHaveBeenCalledWith(expect.objectContaining({ env: expect.objectContaining({ TMPDIR: fixture.temp }) }));++ let settled = false;+ const closing = fixture.close().finally(() => { settled = true; });+ await vi.advanceTimersByTimeAsync(24);+ expect(owned.server.kill).not.toHaveBeenCalled();+ // Half the budget is the graceful close's; the kill and its exit fit in the rest.+ await vi.advanceTimersByTimeAsync(1);+ await closing;++ expect(settled).toBe(true);+ expect(owned.server.kill).toHaveBeenCalledTimes(1);+ expect(owned.child.signalCode).toBe('SIGKILL');+ expect(server.close).toHaveBeenCalledTimes(1);+ expect(existsSync(fixture.temp)).toBe(false);+ });++ it('keeps a reap without an exit status a failure — a kill that returns proves nothing', async () => {+ const { createServer } = fakeVite();+ const owned = ownedServer({ close: stalls, kill: async () => {} });+ const fixture = await startBrowserFixture({ name: 'example', createServer, viteConfig: () => SCOPED,+ chromium: ownedChromium(owned.server), phaseMs: PHASE_MS });++ const error = await fixture.close().catch(caught => caught);++ expect(error.message).toBe('browser close stalled after 25ms, then its kill returned without an exit status');+ expect(owned.server.kill).toHaveBeenCalledTimes(1);+ expect(existsSync(fixture.temp)).toBe(false);+ });++ it('reaps owned browsers that arrive after startup gave up with the same bounded cleanup, and reports one it cannot', async () => {+ // The reap's two steps share one budget with the backstop deadline around+ // it; real timers would race them on a loaded runner.+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });+ const { createServer } = fakeVite();+ const reaped = ownedServer({ close: stalls });+ const unreaped = ownedServer({ close: stalls, kill: stalls });+ const arrive = [];+ const chromium = {+ launchServer: vi.fn(() => new Promise(resolve => { arrive.push(resolve); })),+ connect: vi.fn(),+ };+ const failureLine = vi.spyOn(console, 'error').mockImplementation(() => {});+ let error;+ const failing = startBrowserFixture({ name: 'example', createServer, viteConfig: () => SCOPED, chromium,+ phaseMs: PHASE_MS, observeLaunch: () => () => 'sampled' }).catch((caught) => { error = caught; });++ while (!chromium.launchServer.mock.calls.length) await new Promise(resolve => setImmediate(resolve));+ await vi.advanceTimersByTimeAsync(50);+ // Startup cleanup removes the temp dir with real I/O.+ while (!error) await new Promise(resolve => setImmediate(resolve));+ await failing;+ expect(error.message).toMatch(/^example startup failed during Chromium launch: timed out after 50ms/);++ arrive[0](reaped.server);+ arrive[1](unreaped.server);+ await vi.advanceTimersByTimeAsync(25);+ // Both graceful closes stalled for their half of the budget.+ expect(reaped.server.kill).toHaveBeenCalledTimes(1);+ expect(reaped.child.signalCode).toBe('SIGKILL');+ expect(failureLine).not.toHaveBeenCalled();+ await vi.advanceTimersByTimeAsync(20);+ expect(failureLine).toHaveBeenCalledWith('❌ example browser fixture could not close a late browser: '+ + 'browser close stalled after 25ms, then its kill stalled after 20ms');+ // An abandoned launch is never connected to.+ expect(chromium.connect).not.toHaveBeenCalled();+ failureLine.mockRestore();+ });++ it('only ever close()s an attached `{ launch }` adapter — a stalled one rejects naming it, never terminates it, and Vite and the temp dir still go', async () => {const { server, createServer } = fakeVite();- const browser = fakeBrowser({ close: vi.fn(async () => {}) });- browser.close = vi.fn(() => new Promise(() => {}));+ const browser = fakeBrowser();+ browser.close = vi.fn(stalls);const chromium = { launch: vi.fn(async () => browser) };+ const signal = vi.spyOn(process, 'kill');const fixture = await startBrowserFixture({ name: 'example', createServer, viteConfig: () => SCOPED, chromium, phaseMs: PHASE_MS });expect(existsSync(fixture.temp)).toBe(true);const error = await fixture.close().catch(caught => caught);expect(error.message).toBe('browser close stalled after 50ms');+ expect(signal).not.toHaveBeenCalled();expect(server.close).toHaveBeenCalledTimes(1);expect(existsSync(fixture.temp)).toBe(false);+ signal.mockRestore();});});