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

test: release owned browser pipes after process exit

Adam Eivy947fd46710e7
diff --git a/scripts/lib/ownedBrowserCleanup.js b/scripts/lib/ownedBrowserCleanup.js
index d2b388f218..64b0d8fe97 100644
--- a/scripts/lib/ownedBrowserCleanup.js
+++ b/scripts/lib/ownedBrowserCleanup.js
@@ -27,12 +27,27 @@ const failureWithin = (work, ms) => browserCleanupDeadline(
export const reapOwnedBrowser = async (server, budgetMs) => {
const child = server.process();
const exitStatus = () => child?.exitCode ?? child?.signalCode ?? null;
- 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}`);
+ // 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 releaseExitedPipes = () => {
+ if (exitStatus() === null) return;
+ const pipes = new Set([...(child?.stdio ?? []), child?.stdout, child?.stderr]);
+ for (const pipe of pipes) pipe?.destroy?.();
+ };
+ child?.once?.('exit', releaseExitedPipes);
+ 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}`);
+ } finally {
+ child?.removeListener?.('exit', releaseExitedPipes);
+ }
};
diff --git a/server/test/browserFixture.test.js b/server/test/browserFixture.test.js
index 4773cb0cad..f9750da6c6 100644
--- a/server/test/browserFixture.test.js
+++ b/server/test/browserFixture.test.js
@@ -1,6 +1,6 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { EventEmitter } from 'node:events';
-import { existsSync, mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
+import { existsSync, lstatSync, mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { lstat, rm } from 'node:fs/promises';
@@ -122,6 +122,43 @@ describe('owned server browser fixture lifecycle', () => {
expect(vi.getTimerCount()).toBe(0);
});
+ it.each(['during close', 'before close'])('releases inherited pipes only after owned exit %s and verifies cleanup', async timing => {
+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
+ // Make filesystem completion deterministic while preserving real identity
+ // and absence checks at the public fixture boundary.
+ rm.mockImplementation(async (path, options) => rmSync(path, options));
+ lstat.mockImplementation(async path => lstatSync(path));
+ const fixture = owned();
+ const pipesClosed = deferred();
+ const remaining = new Set(['stdout', 'stderr']);
+ const pipes = [...remaining].map(name => ({ destroy: vi.fn(() => {
+ remaining.delete(name);
+ if (!remaining.size) pipesClosed.resolve();
+ }) }));
+ [fixture.child.stdout, fixture.child.stderr] = pipes;
+ fixture.child.stdio = [null, ...pipes];
+ fixture.server.close.mockImplementation(() => pipesClosed.promise);
+ fixture.server.kill.mockImplementation(() => pipesClosed.promise);
+ const started = await fixture.start();
+ const temp = recordRoot(fixture);
+ if (timing === 'before close') fixture.child.exitCode = 0;
+ const result = started.close().catch(error => error);
+ if (timing === 'during close') {
+ await vi.advanceTimersByTimeAsync(29);
+ for (const pipe of pipes) expect(pipe.destroy).not.toHaveBeenCalled();
+ fixture.child.exitCode = 0;
+ fixture.child.emit('exit', 0, null);
+ }
+ await vi.advanceTimersByTimeAsync(100);
+ expect(await result).toBeUndefined();
+ for (const pipe of pipes) expect(pipe.destroy).toHaveBeenCalledTimes(1);
+ expect(fixture.server.kill).not.toHaveBeenCalled();
+ expect(existsSync(temp)).toBe(false);
+ expect(existsSync(fixture.profile)).toBe(false);
+ expect(fixture.child.eventNames()).toEqual([]);
+ expect(vi.getTimerCount()).toBe(0);
+ });
+
it.each(['returns without exit', 'never settles'])('rejects when owned kill %s and preserves the private root', async mode => {
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
const fixture = owned();