Skip to content

Turn any idea, plan, or codebase into a beautiful interactive diagram. An agent skill for Claude Code, Codex, and more.

fix browser process cleanup lifecycle

tt-a1i4feec81efd61
diff --git a/archify/bin/visual-check.mjs b/archify/bin/visual-check.mjs
index 45092762..dd75cafd 100644
--- a/archify/bin/visual-check.mjs
+++ b/archify/bin/visual-check.mjs
@@ -1719,20 +1719,41 @@ export class ChromeVisualBrowser {
return metrics;
}
- async close() {
+ close() {
+ if (!this.closePromise) this.closePromise = this.finishClose();
+ return this.closePromise;
+ }
+
+ async finishClose() {
this.cdp.failAll(new Error('visual-check finished'));
- if (this.child.exitCode === null && this.child.signalCode === null) {
- this.child.kill('SIGTERM');
- await new Promise((resolve) => {
- const timer = setTimeout(() => {
- if (this.child.exitCode === null && this.child.signalCode === null) this.child.kill('SIGKILL');
- resolve();
- }, 1500);
- this.child.once('exit', () => {
- clearTimeout(timer);
- resolve();
+ try {
+ if (this.child.exitCode === null && this.child.signalCode === null) {
+ await new Promise((resolve) => {
+ let timer = null;
+ let settled = false;
+ const finish = () => {
+ if (settled) return;
+ settled = true;
+ if (timer) clearTimeout(timer);
+ this.child.removeListener('exit', finish);
+ resolve();
+ };
+ this.child.once('exit', finish);
+ this.child.kill('SIGTERM');
+ if (settled) return;
+ timer = setTimeout(() => {
+ if (this.child.exitCode === null && this.child.signalCode === null) this.child.kill('SIGKILL');
+ finish();
+ }, 1500);
});
- });
+ }
+ } finally {
+ // `exit` only reports that Chrome's main process is gone. A helper may
+ // still hold an inherited pipe open, so explicitly release every stream
+ // this browser instance owns before its test cleanup hook resolves.
+ for (const stream of [this.child.stderr, this.child.stdio[3], this.child.stdio[4]]) {
+ if (stream && !stream.destroyed) stream.destroy();
+ }
}
try {
fs.rmSync(this.profileRoot, { recursive: true, force: true });
diff --git a/archify/test/chrome-pipe-transport.test.mjs b/archify/test/chrome-pipe-transport.test.mjs
index 0daf92d5..f1aa3020 100644
--- a/archify/test/chrome-pipe-transport.test.mjs
+++ b/archify/test/chrome-pipe-transport.test.mjs
@@ -71,3 +71,19 @@ test('Chrome pipe EOF fails the pending first command without waiting for proces
await browser.close();
}
});
+
+test('Chrome close releases inherited process pipes after the main process exits', async () => {
+ const child = chromeChild();
+ const browser = browserFor(child);
+ child.exitCode = 0;
+ child.emit('exit', 0, null);
+ const session = assert.rejects(browser.sessionPromise, /visual-check finished/);
+ const first = browser.close();
+ const second = browser.close();
+ assert.strictEqual(second, first);
+ await first;
+ await session;
+ assert.equal(child.stderr.destroyed, true);
+ assert.equal(child.stdio[3].destroyed, true);
+ assert.equal(child.stdio[4].destroyed, true);
+});