Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

fix(tests): wait for owned Chrome groups before closing pipes [C7,N26]

Corey Olson5f0fae21b1e2
diff --git a/test/cockpit/fixture.js b/test/cockpit/fixture.js
index c7fe28a9..cf905e00 100644
--- a/test/cockpit/fixture.js
+++ b/test/cockpit/fixture.js
@@ -769,21 +769,60 @@ async function openSnapshotChrome(executable, url, profile) {
}
}
-/** Stop only an owned Chrome process group, with bounded TERM and KILL waits. */
-async function stopOwnedChrome(child, stopped) {
- /** Wait for the owned process to exit, bounding and clearing the timeout either way. */
- const waitBounded = async (ms) => {
- let timer;
- const exited = await Promise.race([stopped.then(() => true), new Promise((resolve) => {
- timer = setTimeout(() => resolve(false), ms);
- })]);
- clearTimeout(timer);
- return exited;
+/** Check the owned group, since a leader's close event also waits for inherited pipes. */
+function ownedChromeGroupAlive(pid) {
+ try { process.kill(-pid, 0); return true; }
+ catch (error) {
+ if (error.code === 'ESRCH') return false;
+ throw error;
+ }
+}
+
+/** Bound the whole group's lifetime, including helpers whose leader has already exited. */
+async function waitForOwnedChromeGroup(pid, timeoutMs) {
+ const deadline = Date.now() + timeoutMs;
+ while (ownedChromeGroupAlive(pid)) {
+ const remaining = deadline - Date.now();
+ if (remaining <= 0) return false;
+ await browserPause(Math.min(20, remaining));
+ }
+ return true;
+}
+
+/** Name surviving group members without exposing any browser arguments or profile paths. */
+function ownedChromeGroupPids(pid) {
+ const rows = execFileSync('ps', ['-axo', 'pid=,pgid='], { encoding: 'utf8' });
+ return rows.trim().split('\n').map(row => row.trim().split(/\s+/u))
+ .filter(row => Number(row[1]) === pid).map(row => Number(row[0]));
+}
+
+/** Stop only an owned Chrome group, then release pipes held by independent crash reporters. */
+async function stopOwnedChrome(child, stopped, { timeoutMs = 5_000, signal = process.kill } = {}) {
+ const pid = child.pid;
+ assert.ok(Number.isSafeInteger(pid) && pid > 0, 'Chrome cleanup requires its owned group leader pid');
+ const startedAt = Date.now();
+ /** An already-exited group needs no signal; other signal failures must stay visible. */
+ const send = name => {
+ try { signal(-pid, name); }
+ catch (error) { if (error.code !== 'ESRCH') throw error; }
};
- try { process.kill(-child.pid, 'SIGTERM'); } catch { /* The owned Chrome group already exited. */ }
- if (await waitBounded(5_000)) return;
- try { process.kill(-child.pid, 'SIGKILL'); } catch { /* The owned Chrome group already exited. */ }
- assert.ok(await waitBounded(5_000), 'owned Chrome exits after SIGKILL within the bounded cleanup window'); // budget: hang ceiling for owned Chrome process-group cleanup; no throughput claim.
+ if (ownedChromeGroupAlive(pid)) send('SIGTERM');
+ if (!await waitForOwnedChromeGroup(pid, timeoutMs)) {
+ send('SIGKILL');
+ if (!await waitForOwnedChromeGroup(pid, timeoutMs)) {
+ const pids = ownedChromeGroupPids(pid);
+ throw new Error(`owned Chrome group ${pid} did not exit after SIGKILL; waited ${Date.now() - startedAt}ms; live pids: ${pids.join(', ') || 'none at final capture'}`);
+ }
+ }
+ // An independent process can inherit stderr without belonging to the owned Chrome group.
+ child.stderr?.destroy();
+ let timer;
+ try {
+ const closed = await Promise.race([stopped.then(() => true), new Promise(resolve => {
+ timer = setTimeout(() => resolve(false), timeoutMs);
+ })]);
+ assert.ok(closed, `owned Chrome group ${pid} is gone but its leader did not close; waited ${Date.now() - startedAt}ms`);
+ } finally { clearTimeout(timer); }
}
/** Close the CDP socket and stop only this test's isolated Chrome process group. */
diff --git a/test/cockpit/fixture.test.js b/test/cockpit/fixture.test.js
index 37e9b4e6..ac9bf921 100644
--- a/test/cockpit/fixture.test.js
+++ b/test/cockpit/fixture.test.js
@@ -1,8 +1,8 @@
/** Cockpit fixture checks, using the shared real-board fixtures [N26]. */
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
-import { spawnSync } from 'node:child_process';
-import { chmodSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
+import { spawn, spawnSync } from 'node:child_process';
+import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
import { createServer, request } from 'node:http';
import diagnosticsChannel from 'node:diagnostics_channel';
import { tmpdir } from 'node:os';
@@ -394,3 +394,80 @@ test('a Chrome wait whose condition throws while the next document loads keeps p
rmSync(profile, { recursive: true, force: true });
}
});
+
+test('owned Chrome cleanup waits out a slow exit and still fails a real hang [C7,N26]', async t => {
+ const dir = realpathSync(mkdtempSync(join(tmpdir(), 'pullboard-owned-chrome-cleanup-')));
+ scratch.push(dir);
+ const launched = [];
+ const independent = [];
+ /** Launch a private stand-in and retain its close promise just as the cockpit launcher does. */
+ function launch(code, args = []) {
+ const child = spawn(process.execPath, ['-e', code, ...args], { detached: true, stdio: ['ignore', 'ignore', 'pipe'] });
+ child.stderr.resume();
+ const stopped = new Promise(resolve => child.once('close', resolve));
+ const result = { child, stopped };
+ launched.push(result);
+ return result;
+ }
+ /** Wait for the child to install its signal handlers before testing cleanup. */
+ async function ready(path) {
+ const deadline = Date.now() + 10_000;
+ while (!existsSync(path) && Date.now() < deadline) await new Promise(resolve => setTimeout(resolve, 20));
+ assert.ok(existsSync(path), 'the owned stand-in installed its handlers');
+ }
+ t.after(async () => {
+ for (const pid of independent) {
+ try { process.kill(pid, 'SIGKILL'); } catch (error) { if (error.code !== 'ESRCH') throw error; }
+ }
+ for (const { child, stopped } of launched) {
+ try { process.kill(-child.pid, 'SIGKILL'); } catch (error) { if (error.code !== 'ESRCH') throw error; }
+ child.stderr.destroy();
+ await stopped;
+ }
+ });
+
+ const pipeReady = join(dir, 'pipe-ready');
+ const holder = `const fs = require('node:fs'); fs.writeFileSync(process.argv[1], String(process.pid)); setInterval(() => {}, 1000);`;
+ const pipe = launch(`const { spawn } = require('node:child_process');
+ process.on('SIGTERM', () => process.exit(0));
+ const helper = spawn(process.execPath, ['-e', process.argv[1], process.argv[2]], { detached: true, stdio: ['ignore', 'ignore', 'inherit'] });
+ helper.unref(); setInterval(() => {}, 1000);`, [holder, pipeReady]);
+ await ready(pipeReady);
+ const holderPid = Number(readFileSync(pipeReady, 'utf8'));
+ independent.push(holderPid);
+ await stopOwnedChrome(pipe.child, pipe.stopped);
+ assert.doesNotThrow(() => process.kill(holderPid, 0), 'cleanup releases stderr without killing an independent holder');
+ assert.ok(pipe.child.exitCode !== null || pipe.child.signalCode !== null, 'the owned leader exited');
+
+ const slowReady = join(dir, 'slow-ready');
+ const slowFinished = join(dir, 'slow-finished');
+ const slowHelper = `const fs = require('node:fs');
+ process.on('SIGTERM', () => setTimeout(() => { fs.writeFileSync(process.argv[2], 'finished'); process.exit(0); }, 150));
+ fs.writeFileSync(process.argv[1], 'ready'); setInterval(() => {}, 1000);`;
+ const slow = launch(`const { spawn } = require('node:child_process');
+ process.on('SIGTERM', () => process.exit(0));
+ spawn(process.execPath, ['-e', process.argv[1], process.argv[2], process.argv[3]], { stdio: 'ignore' });
+ setInterval(() => {}, 1000);`, [slowHelper, slowReady, slowFinished]);
+ await ready(slowReady);
+ await stopOwnedChrome(slow.child, slow.stopped);
+ assert.equal(readFileSync(slowFinished, 'utf8'), 'finished', 'cleanup waited for the helper after the leader and its pipe closed');
+ assert.throws(() => process.kill(-slow.child.pid, 0), { code: 'ESRCH' }, 'no owned helper survives successful cleanup');
+
+ const hangReady = join(dir, 'hang-ready');
+ const hung = launch(`const fs = require('node:fs'); fs.writeFileSync(process.argv[1], 'ready'); setInterval(() => {}, 1000);`, [hangReady]);
+ await ready(hangReady);
+ const signals = [];
+ await assert.rejects(stopOwnedChrome(hung.child, hung.stopped, {
+ timeoutMs: 100, // budget: the stand-in intentionally survives both signals; bound only this forced failure.
+ signal: (pid, name) => { signals.push({ pid, name }); },
+ }), error => {
+ assert.match(error.message, /did not exit after SIGKILL; waited \d+ms; live pids:/u);
+ assert.ok(error.message.split('live pids: ')[1].split(', ').includes(String(hung.child.pid)), 'the hang names its surviving pid');
+ assert.doesNotThrow(() => process.kill(hung.child.pid, 0), 'the reported hang is an actual surviving stand-in');
+ return true;
+ });
+ assert.deepEqual(signals, [
+ { pid: -hung.child.pid, name: 'SIGTERM' },
+ { pid: -hung.child.pid, name: 'SIGKILL' },
+ ], 'cleanup tries both signals against only its owned group');
+});