Files
hyperframes/packages/cli/src/browser/chromeTree.test.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

46 lines
1.3 KiB
TypeScript

import { spawn } from "node:child_process";
import { expect, it } from "vitest";
import type { Browser } from "puppeteer-core";
import { waitForTestCondition } from "../utils/processTestUtils.js";
import { closeManagedBrowser } from "./launch.js";
const alive = (pid: number) => {
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
};
it.runIf(process.platform !== "win32")(
"closing a browser also ends the helper processes it leaves behind",
async () => {
const main = spawn(
process.execPath,
[
"-e",
`const h=require("node:child_process").spawn("sleep",["60"],{detached:true,stdio:"ignore"});h.unref();console.log(h.pid);setInterval(()=>{},1e6)`,
],
{ stdio: ["ignore", "pipe", "ignore"] },
);
const helper = Number(
await new Promise<string>((resolve) => main.stdout!.once("data", (d) => resolve(String(d)))),
);
expect(alive(helper)).toBe(true);
const browser = {
process: () => main,
close: async () => {
main.kill("SIGKILL");
},
} as unknown as Browser;
try {
await closeManagedBrowser(browser);
await waitForTestCondition(() => !alive(helper), 5_000);
} finally {
if (alive(helper)) process.kill(helper, "SIGKILL");
}
},
30_000,
);