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: fall back to retained browser child when provider kill stalls

Adam Eivy19d0edd23453
diff --git a/scripts/lib/ownedBrowserCleanup.js b/scripts/lib/ownedBrowserCleanup.js
index 4d7205f89a..a7746531a6 100644
--- a/scripts/lib/ownedBrowserCleanup.js
+++ b/scripts/lib/ownedBrowserCleanup.js
@@ -51,6 +51,7 @@ export const reapOwnedBrowser = async (server, budgetMs, { requireProfileRemoval
const pipes = () => [...new Set([...(child?.stdio ?? []), child?.stdout, child?.stderr])].filter(Boolean);
const phases = [];
const settlement = { close: 'not-started', kill: 'not-started' };
+ let directKill = 'not-needed';
let closeObserved = false;
const onClose = () => { closeObserved = true; };
// Node emits exit before close if a helper retains inherited pipes. Release
@@ -65,7 +66,7 @@ export const reapOwnedBrowser = async (server, budgetMs, { requireProfileRemoval
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()}`;
+ + ` closePromise=${settlement.close} killPromise=${settlement.kill} profile=${profileState()} directKill=${directKill}`;
};
const run = async (phase, ms) => {
const started = Date.now();
@@ -79,8 +80,21 @@ export const reapOwnedBrowser = async (server, budgetMs, { requireProfileRemoval
settlement[phase] = 'rejected';
return 'failed'; // Provider errors can contain profile paths and host identifiers.
});
+ // Playwright signals a process group and swallows dispatch errors before
+ // waiting for cleanup. If that path has not established exit halfway
+ // through its share, signal only the retained ChildProcess handle. Never
+ // search for PIDs or infer success from either signal's return value.
+ let fallback;
+ if (phase === 'kill' && typeof child?.kill === 'function') {
+ fallback = setTimeout(() => {
+ if (exitStatus() !== null || settlement.kill !== 'pending') return;
+ try { directKill = child.kill('SIGKILL') ? 'sent' : 'refused'; }
+ catch { directKill = 'failed'; }
+ }, Math.floor(ms / 2));
+ }
const failure = await browserCleanupDeadline(work, ms, () => timeoutError(ms))
- .catch(() => `stalled after ${ms}ms`);
+ .catch(() => `stalled after ${ms}ms`)
+ .finally(() => clearTimeout(fallback));
phases.push(`${phase} elapsed=${Date.now() - started}ms ${snapshot()}`);
return failure;
};
diff --git a/scripts/ownedBrowserCleanup.test.js b/scripts/ownedBrowserCleanup.test.js
new file mode 100644
index 0000000000..67f0fe0642
--- /dev/null
+++ b/scripts/ownedBrowserCleanup.test.js
@@ -0,0 +1,63 @@
+import { spawn } from 'node:child_process';
+import { once } from 'node:events';
+import { existsSync, mkdtempSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { expect, it, vi } from 'vitest';
+import { reapOwnedBrowser } from './lib/ownedBrowserCleanup.js';
+
+it('reaps the retained child when the provider kill never dispatches, requiring its cleanup to settle', async () => {
+ const profile = mkdtempSync(join(tmpdir(), 'playwright_chromiumdev_profile-'));
+ const child = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)', '--', `--user-data-dir=${profile}`], {
+ stdio: 'ignore',
+ });
+ const exited = once(child, 'close');
+ // Models Playwright swallowing a failed group signal and awaiting cleanup.
+ const cleanup = exited.then(() => rmSync(profile, { recursive: true, force: true }));
+ const kill = vi.fn(() => cleanup);
+ try {
+ await once(child, 'spawn');
+ const evidence = await reapOwnedBrowser({ process: () => child, close: () => new Promise(() => {}), kill },
+ 1000, { requireProfileRemoval: true });
+ expect(child.signalCode).toBe('SIGKILL');
+ expect(kill).toHaveBeenCalledTimes(1);
+ expect(evidence).toContain('exit=confirmed');
+ expect(evidence).toContain('killPromise=fulfilled profile=absent directKill=sent');
+ expect(existsSync(profile)).toBe(false);
+ } finally {
+ if (child.exitCode === null && child.signalCode === null) child.kill('SIGKILL');
+ await exited;
+ rmSync(profile, { recursive: true, force: true });
+ }
+});
+
+it.each(['refused', 'failed', 'exit-without-cleanup'])(
+ 'keeps direct termination %s a failed teardown and clears its timers', async outcome => {
+ vi.useFakeTimers();
+ const { EventEmitter } = await import('node:events');
+ const child = Object.assign(new EventEmitter(), { exitCode: null, signalCode: null });
+ child.kill = vi.fn(() => {
+ if (outcome === 'failed') throw new Error('private signal error');
+ if (outcome === 'exit-without-cleanup') {
+ child.signalCode = 'SIGKILL';
+ child.emit('exit');
+ return true;
+ }
+ return false;
+ });
+ const pending = () => new Promise(() => {});
+ try {
+ const closing = reapOwnedBrowser({ process: () => child, close: pending, kill: pending }, 100).catch(error => error);
+ await vi.advanceTimersByTimeAsync(90);
+ const error = await closing;
+ expect(error).toBeInstanceOf(Error);
+ expect(error.message).toContain(`directKill=${outcome === 'exit-without-cleanup' ? 'sent' : outcome}`);
+ expect(error.message).toContain(`exit=${outcome === 'exit-without-cleanup' ? 'confirmed' : 'unconfirmed'}`);
+ expect(error.message).toContain('killPromise=pending');
+ expect(error.message).not.toContain('private signal error');
+ expect(child.kill).toHaveBeenCalledExactlyOnceWith('SIGKILL');
+ expect(vi.getTimerCount()).toBe(0);
+ expect(child.eventNames()).toEqual([]);
+ } finally { vi.useRealTimers(); }
+ },
+);