Files
hyperframes/packages/cli/src/browser/chromeTree.ts
T
Miguel Ángel b428ea92be fix(cli): closing the managed Chrome also ends the helper processes it leaves behind (#6542)
* fix(cli): closing the managed Chrome also ends the helper processes it leaves behind

The close waited a second for Chrome to exit and then killed only Chrome's own process.
Under load that second is often not enough, and the helpers (the GPU and renderer
processes, crashpad_handler) were reparented to init and kept running after a finished
run. The close now records the processes under Chrome before it starts, and ends any of
them still running (under the same command) once Chrome is gone.

* test(cli): the helper-reaping test polls for the helper's exit instead of waiting 200 ms

* fix(cli): the Chrome process listing is bounded and no longer runs at import

The ps call that records Chrome's helpers ran with no time limit before the
bounded browser close, so a stalled ps made the close unbounded. It now has a
2 s limit, and a timeout records no helpers.

execFile is resolved when ps runs, not when the module loads, so tests that
mock node:child_process without execFile import it again.
2026-10-27 11:08:53 +00:00

58 lines
1.8 KiB
TypeScript

import { execFile } from "node:child_process";
import { promisify } from "node:util";
const PS_TIMEOUT_MS = 2_000;
export interface TreeMember {
pid: number;
command: string;
}
async function processTable(): Promise<Array<TreeMember & { ppid: number }>> {
let stdout: string;
try {
({ stdout } = await promisify(execFile)("ps", ["-axo", "pid=,ppid=,command="], {
maxBuffer: 16 * 1024 * 1024,
timeout: PS_TIMEOUT_MS,
}));
} catch (error) {
// A stalled ps is killed at the timeout: nothing recorded, so the close stays bounded.
if ((error as { killed?: boolean }).killed) return [];
throw error;
}
return stdout.split("\n").flatMap((line) => {
const row = /^\s*(\d+)\s+(\d+)\s+(.*)$/.exec(line);
return row ? [{ pid: Number(row[1]), ppid: Number(row[2]), command: row[3]! }] : [];
});
}
/** Every process under `rootPid` now, so they can be ended after the root is gone and they are reparented to init. */
export async function chromeDescendants(rootPid: number): Promise<TreeMember[]> {
const table = await processTable();
const found: TreeMember[] = [];
const queue = [rootPid];
while (queue.length > 0) {
const parent = queue.shift()!;
for (const row of table) {
if (row.ppid !== parent) continue;
found.push({ pid: row.pid, command: row.command });
queue.push(row.pid);
}
}
return found;
}
/** Ends the members still running under the command they had, so a reused pid is left alone. */
export async function reapChromeTree(members: readonly TreeMember[]): Promise<void> {
if (members.length === 0) return;
const commands = new Map((await processTable()).map((row) => [row.pid, row.command]));
for (const { pid, command } of members) {
if (commands.get(pid) !== command) continue;
try {
process.kill(pid, "SIGKILL");
} catch {
/* already gone */
}
}
}