Skip to content

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

fix(tests): retain browser teardown evidence and verify profile removal (#13308)

[._.]/ Adam Eivy39cd61816b29
diff --git a/client/src/test/browserFixture.js b/client/src/test/browserFixture.js
index d8f6a3b18c..331ffeb288 100644
--- a/client/src/test/browserFixture.js
+++ b/client/src/test/browserFixture.js
@@ -54,7 +54,9 @@
* well under a second and removes the profile, so it gets most of the budget. 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
+ * that server's own process, and requires its exit, Playwright cleanup settlement,
+ * and observed profile absence. Redacted cleanup phases survive CI silent mode
+ * in the step summary. A
* `{ launch }` stand-in — the CDP adapter attaching to an external browser
* among them — keeps only `close()`, which never terminates what it attached
* to.
@@ -265,7 +267,15 @@ export async function startBrowserFixture({
return { browser, closer: () => browser.close() };
}
const server = await chromium.launchServer(options);
- const closer = () => reapOwnedBrowser(server, phaseMs.cleanup);
+ const closer = async () => {
+ const evidence = await reapOwnedBrowser(server, phaseMs.cleanup, { requireProfileRemoval: true }).catch(error => {
+ retainLine(`❌ ${name} browser cleanup: ${error.message}`);
+ throw error;
+ });
+ const line = `🧹 ${name} browser cleanup (${evidence})`;
+ console.log(line);
+ retainLine(line);
+ };
// Nobody will use a browser startup already gave up on: skip connecting.
if (isAbandoned()) return { closer };
try {
diff --git a/client/src/test/browserFixture.test.js b/client/src/test/browserFixture.test.js
index 9c46ba2e1a..8f7d7176f0 100644
--- a/client/src/test/browserFixture.test.js
+++ b/client/src/test/browserFixture.test.js
@@ -1,5 +1,6 @@
// @vitest-environment node
import { afterEach, describe, expect, it, vi } from 'vitest';
+import { EventEmitter } from 'node:events';
import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
@@ -17,12 +18,17 @@ const fakeBrowser = (page) => ({ close: vi.fn(async () => {}), newPage: vi.fn(as
// 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 profiles = [];
const ownedServer = ({ close, kill } = {}) => {
- const child = { exitCode: null, signalCode: null };
+ const profile = mkdtempSync(join(tmpdir(), 'playwright_chromiumdev_profile-'));
+ profiles.push(profile);
+ const clearProfile = () => rmSync(profile, { recursive: true, force: true });
+ const child = Object.assign(new EventEmitter(), { exitCode: null, signalCode: null,
+ spawnargs: ['example-chrome', `--user-data-dir=${profile}`] });
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 };
+ close: vi.fn(close ?? (async () => { child.exitCode = 0; clearProfile(); })),
+ kill: vi.fn(kill ?? (async () => { child.signalCode = 'SIGKILL'; clearProfile(); })) };
+ return { child, server, profile, clearProfile };
};
const ownedChromium = (server, browser = fakeBrowser()) => ({
launchServer: vi.fn(async () => server), connect: vi.fn(async () => browser),
@@ -50,7 +56,7 @@ const stubStepSummary = () => {
afterEach(() => {
vi.useRealTimers();
vi.unstubAllEnvs();
- for (const dir of summaryDirs.splice(0)) rmSync(dir, { recursive: true, force: true });
+ for (const dir of [...summaryDirs.splice(0), ...profiles.splice(0)]) rmSync(dir, { recursive: true, force: true });
});
describe('startBrowserFixture', () => {
@@ -204,6 +210,7 @@ describe('startBrowserFixture', () => {
expect(settled).toBe(true);
expect(owned.server.kill).toHaveBeenCalledTimes(1);
expect(owned.child.signalCode).toBe('SIGKILL');
+ expect(existsSync(owned.profile)).toBe(false);
expect(server.close).toHaveBeenCalledTimes(1);
expect(existsSync(fixture.temp)).toBe(false);
});
@@ -216,11 +223,66 @@ describe('startBrowserFixture', () => {
const error = await fixture.close().catch(caught => caught);
- expect(error.message).toBe('browser close stalled after 15ms, then its kill returned without an exit status');
+ expect(error.message).toMatch(/^browser close stalled after 15ms, then its kill returned without an exit status; cleanup phases:/);
+ expect(error.message).toContain('killPromise=fulfilled profile=present');
expect(owned.server.kill).toHaveBeenCalledTimes(1);
expect(existsSync(fixture.temp)).toBe(false);
});
+ it.each(['exit', 'child-close', 'playwright-cleanup'])(
+ 'distinguishes a stalled %s boundary without accepting unconfirmed cleanup', async boundary => {
+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'Date'] });
+ const summary = stubStepSummary();
+ const { createServer } = fakeVite();
+ const owned = ownedServer({ close: stalls, kill: stalls });
+ const pipe = { destroyed: false, closed: false, destroy: vi.fn(() => { pipe.destroyed = true; }) };
+ owned.child.stdio = [null, pipe];
+ owned.child.stdout = pipe;
+ const fixture = await startBrowserFixture({ name: 'example', createServer, viteConfig: () => SCOPED,
+ chromium: ownedChromium(owned.server), phaseMs: PHASE_MS });
+ const closing = fixture.close().catch(error => error);
+ await vi.advanceTimersByTimeAsync(15);
+ expect(pipe.destroy).not.toHaveBeenCalled();
+ if (boundary !== 'exit') {
+ owned.child.exitCode = 0;
+ owned.child.emit('exit', 0, null);
+ }
+ if (boundary === 'playwright-cleanup') {
+ pipe.closed = true;
+ owned.child.emit('close', 0, null);
+ }
+ await vi.advanceTimersByTimeAsync(30);
+ const error = await closing;
+ expect(error).toBeInstanceOf(Error);
+ expect(error.message).toContain('close elapsed=15ms owner=retained exit=unconfirmed');
+ expect(error.message).toContain(`kill elapsed=30ms owner=retained exit=${boundary === 'exit' ? 'unconfirmed' : 'confirmed'}`);
+ expect(error.message).toContain(`childClose=${boundary === 'playwright-cleanup' ? 'observed' : 'unobserved'} pipes=1/${boundary === 'exit' ? 0 : 1}-destroyed/${boundary === 'playwright-cleanup' ? 1 : 0}-closed`);
+ expect(error.message).toContain('closePromise=pending killPromise=pending profile=present');
+ expect(error.message).not.toContain(owned.profile);
+ expect(existsSync(owned.profile)).toBe(true);
+ expect(pipe.destroy).toHaveBeenCalledTimes(boundary === 'exit' ? 0 : 1);
+ expect(owned.child.eventNames()).toEqual([]);
+ expect(vi.getTimerCount()).toBe(0);
+ expect(summary.read()).toContain(error.message);
+ },
+ );
+
+ it.each(['present', 'unavailable'])('rejects fulfilled Playwright cleanup with profile %s despite exit', async state => {
+ const { createServer } = fakeVite();
+ const owned = ownedServer();
+ // Playwright logs directory-removal errors but fulfills its cleanup promise.
+ owned.server.close.mockImplementation(async () => { owned.child.exitCode = 0; });
+ owned.server.kill.mockImplementation(async () => {});
+ if (state === 'unavailable') owned.child.spawnargs.push('--user-data-dir=/unowned-profile');
+ 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).toContain('returned without confirmed profile removal');
+ expect(error.message).toContain(`closePromise=fulfilled killPromise=fulfilled profile=${state}`);
+ expect(error.message).not.toContain(owned.profile);
+ expect(existsSync(owned.profile)).toBe(true);
+ });
+
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.
@@ -253,8 +315,8 @@ describe('startBrowserFixture', () => {
expect(reaped.child.signalCode).toBe('SIGKILL');
expect(failureLine).not.toHaveBeenCalled();
await vi.advanceTimersByTimeAsync(30);
- expect(failureLine).toHaveBeenCalledWith('❌ example browser fixture could not close a late browser: '
- + 'browser close stalled after 15ms, then its kill stalled after 30ms (no exit status)');
+ expect(failureLine).toHaveBeenCalledWith(expect.stringContaining('❌ example browser fixture could not close a late browser: '
+ + 'browser close stalled after 15ms, then its kill stalled after 30ms (no exit status); cleanup phases:'));
// An abandoned launch is never connected to.
expect(chromium.connect).not.toHaveBeenCalled();
failureLine.mockRestore();
diff --git a/scripts/lib/ownedBrowserCleanup.js b/scripts/lib/ownedBrowserCleanup.js
index 64b0d8fe97..4d7205f89a 100644
--- a/scripts/lib/ownedBrowserCleanup.js
+++ b/scripts/lib/ownedBrowserCleanup.js
@@ -1,3 +1,7 @@
+import { lstatSync, realpathSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { basename, dirname, isAbsolute, normalize } from 'node:path';
+
/** Test-only owned Playwright lifecycle shared by the server and client fixtures. */
const timeoutError = ms => Object.assign(new Error(`timed out after ${ms}ms`), { name: 'TimeoutError' });
@@ -9,45 +13,89 @@ export const browserCleanupDeadline = (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) => browserCleanupDeadline(
- Promise.resolve().then(work).then(() => null, error => `failed: ${error.message}`),
- ms, () => timeoutError(ms),
-).catch(() => `stalled after ${ms}ms`);
+// Read only the profile named by this retained child. Never enumerate or remove
+// directories, and never include its path or raw filesystem errors in evidence.
+const observeOwnedBrowserProfile = (child) => {
+ const args = child?.spawnargs ?? [];
+ const paths = [];
+ for (let i = 0; i < args.length; i++) {
+ if (args[i] === '--user-data-dir') paths.push(args[++i]);
+ else if (args[i].startsWith('--user-data-dir=')) paths.push(args[i].slice('--user-data-dir='.length));
+ }
+ const path = paths[0];
+ if (paths.length !== 1 || !path || !isAbsolute(path) || normalize(path) !== path
+ || !/^playwright_chromiumdev_profile-[\w-]+$/.test(basename(path))) return () => 'unavailable';
+ try {
+ if (realpathSync(dirname(path)) !== realpathSync(tmpdir())) return () => 'unavailable';
+ } catch { return () => 'unavailable'; }
+ return () => {
+ try {
+ const stat = lstatSync(path, { throwIfNoEntry: false });
+ if (!stat) return 'absent';
+ return stat.isDirectory() && !stat.isSymbolicLink() ? 'present' : 'changed';
+ } catch { return 'unavailable'; }
+ };
+};
/**
- * Closes a browser server the fixture launched, inside `budgetMs`: a graceful
- * close for 30% of it, then the server's own kill for 60%, 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, saying whether the process had
- * exited — the kill then waits on Playwright's own stdio and profile cleanup.
+ * Spend the unchanged cleanup budget on graceful close (30%), then Playwright's
+ * owned kill (60%). A signal is never success: require exit and settlement of
+ * Playwright's close/cleanup promise. Client callers also require profile absence
+ * because Playwright can swallow removal errors. Server callers already verify
+ * and remove their captured profile separately.
*/
-export const reapOwnedBrowser = async (server, budgetMs) => {
+export const reapOwnedBrowser = async (server, budgetMs, { requireProfileRemoval = false } = {}) => {
const child = server.process();
+ const profileState = observeOwnedBrowserProfile(child);
const exitStatus = () => child?.exitCode ?? child?.signalCode ?? null;
- // Node emits 'exit' before 'close' when a browser helper retains inherited
- // pipes. Playwright waits for 'close' before removing its profile, and both
- // server.close() and server.kill() await that cleanup. Release only this
- // exited child's streams so its close/cleanup can actually settle.
+ const pipes = () => [...new Set([...(child?.stdio ?? []), child?.stdout, child?.stderr])].filter(Boolean);
+ const phases = [];
+ const settlement = { close: 'not-started', kill: 'not-started' };
+ let closeObserved = false;
+ const onClose = () => { closeObserved = true; };
+ // Node emits exit before close if a helper retains inherited pipes. Release
+ // only this exited child's streams so Playwright's close/cleanup can settle.
const releaseExitedPipes = () => {
if (exitStatus() === null) return;
- const pipes = new Set([...(child?.stdio ?? []), child?.stdout, child?.stderr]);
- for (const pipe of pipes) pipe?.destroy?.();
+ for (const pipe of pipes()) pipe.destroy?.();
+ };
+ const snapshot = () => {
+ const streams = pipes();
+ const closed = streams.filter(pipe => pipe.closed === true).length;
+ const destroyed = streams.filter(pipe => pipe.destroyed === true).length;
+ return `owner=${child ? 'retained' : 'missing'} exit=${exitStatus() === null ? 'unconfirmed' : 'confirmed'}`
+ + ` childClose=${closeObserved ? 'observed' : 'unobserved'} pipes=${streams.length}/${destroyed}-destroyed/${closed}-closed`
+ + ` closePromise=${settlement.close} killPromise=${settlement.kill} profile=${profileState()}`;
+ };
+ const run = async (phase, ms) => {
+ const started = Date.now();
+ settlement[phase] = 'pending';
+ const work = Promise.resolve().then(() => server[phase]()).then(() => {
+ settlement[phase] = 'fulfilled';
+ if (exitStatus() === null) return 'returned without an exit status';
+ if (requireProfileRemoval && profileState() !== 'absent') return 'returned without confirmed profile removal';
+ return null;
+ }, () => {
+ settlement[phase] = 'rejected';
+ return 'failed'; // Provider errors can contain profile paths and host identifiers.
+ });
+ const failure = await browserCleanupDeadline(work, ms, () => timeoutError(ms))
+ .catch(() => `stalled after ${ms}ms`);
+ phases.push(`${phase} elapsed=${Date.now() - started}ms ${snapshot()}`);
+ return failure;
};
child?.once?.('exit', releaseExitedPipes);
+ child?.once?.('close', onClose);
try {
- releaseExitedPipes(); // Exit may have occurred before fixture teardown.
- const confirmed = failure => failure ?? (exitStatus() === null ? 'returned without an exit status' : null);
- const closeFailure = confirmed(await failureWithin(() => server.close(), Math.floor(budgetMs * 0.3)));
- if (!closeFailure) return;
- const killFailure = confirmed(await failureWithin(() => server.kill(), Math.floor(budgetMs * 0.6)));
- if (!killFailure) return;
- const exit = exitStatus();
- const state = exit !== null ? ` (process exited with ${exit})` : killFailure.endsWith('exit status') ? '' : ' (no exit status)';
- throw new Error(`browser close ${closeFailure}, then its kill ${killFailure}${state}`);
+ releaseExitedPipes();
+ const closeFailure = await run('close', Math.floor(budgetMs * 0.3));
+ if (!closeFailure) return phases.join('; ');
+ const killFailure = await run('kill', Math.floor(budgetMs * 0.6));
+ if (!killFailure) return phases.join('; ');
+ const state = exitStatus() !== null ? ' (process exited)' : killFailure.endsWith('exit status') ? '' : ' (no exit status)';
+ throw new Error(`browser close ${closeFailure}, then its kill ${killFailure}${state}; cleanup phases: ${phases.join('; ')}`);
} finally {
child?.removeListener?.('exit', releaseExitedPipes);
+ child?.removeListener?.('close', onClose);
}
};