From a2df08e49ebb2b2d285c0e8a7c173f953b032a9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sat, 21 Nov 2026 10:38:23 +0000 Subject: [PATCH] fix(cli): a stop reads the process tree from /proc on Linux, so load cannot hide a child (#8363) * test(cli): a stop finds the process its child started even when pgrep fails * fix(cli): a stop reads the process tree from /proc on Linux, so load cannot hide a child --- packages/cli/src/utils/processTree.ts | 31 ++++++++++++++++++- .../cli/src/utils/stoppableExecFile.test.ts | 24 ++++++++++++-- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/packages/cli/src/utils/processTree.ts b/packages/cli/src/utils/processTree.ts index c68135cca4..c74cb94fc0 100644 --- a/packages/cli/src/utils/processTree.ts +++ b/packages/cli/src/utils/processTree.ts @@ -81,7 +81,36 @@ function signalProcessGroup(pid: number, signal: NodeJS.Signals): void { } catch {} } +function childrenFromProc(): Map | undefined { + try { + const children = new Map(); + for (const entry of readdirSync("/proc")) { + if (!/^\d+$/.test(entry)) continue; + let stat: string; + try { + stat = readFileSync(`/proc/${entry}/stat`, "utf8"); + } catch { + continue; + } + const parent = Number(stat.slice(stat.lastIndexOf(") ") + 2).split(/\s+/)[1]); + children.set(parent, [...(children.get(parent) ?? []), Number(entry)]); + } + return children; + } catch { + return undefined; + } +} + +function descendantsIn(tree: Map, pid: number): number[] { + return (tree.get(pid) ?? []).flatMap((child) => [child, ...descendantsIn(tree, child)]); +} + function getDescendants(pid: number): number[] { + const tree = process.platform === "linux" ? childrenFromProc() : undefined; + return tree ? descendantsIn(tree, pid) : pgrepDescendants(pid); +} + +function pgrepDescendants(pid: number): number[] { let children: number[]; try { const raw = execFileSync("pgrep", ["-P", String(pid)], { @@ -95,7 +124,7 @@ function getDescendants(pid: number): number[] { } catch { return []; } - return children.flatMap((childPid) => [childPid, ...getDescendants(childPid)]); + return children.flatMap((childPid) => [childPid, ...pgrepDescendants(childPid)]); } function signalProcesses(pids: readonly number[], signal: NodeJS.Signals): void { diff --git a/packages/cli/src/utils/stoppableExecFile.test.ts b/packages/cli/src/utils/stoppableExecFile.test.ts index 3433b4d13f..f59dbb90ba 100644 --- a/packages/cli/src/utils/stoppableExecFile.test.ts +++ b/packages/cli/src/utils/stoppableExecFile.test.ts @@ -96,7 +96,7 @@ describe("stoppableExecFile", () => { let dir: string; afterEach(() => rmSync(dir, { recursive: true, force: true })); - it("ends the child and the process it started, then rejects with the reason", async () => { + async function stopsTheTree() { dir = mkdtempSync(join(tmpdir(), "hf-stoppable-exec-")); const pids = join(dir, "pids"); const script = join(dir, "tool.sh"); @@ -119,7 +119,27 @@ describe("stoppableExecFile", () => { for (const pid of readFileSync(pids, "utf8").trim().split(" ").map(Number)) { expect(() => process.kill(pid, 0), `pid ${pid}`).toThrow(); } - }); + } + + it("ends the child and the process it started, then rejects with the reason", stopsTheTree); + + // fails without the fix: a pgrep that failed or ran past its 2 s on a loaded runner found no children, so the + // orphaned grandchild kept the output pipes open and the stop waited it out (CI: timed out at 20 s). + it.skipIf(process.platform !== "linux")( + "finds the process it started on Linux without pgrep", + async () => { + const bin = mkdtempSync(join(tmpdir(), "hf-no-pgrep-")); + writeFileSync(join(bin, "pgrep"), "#!/bin/sh\nexit 2\n", { mode: 0o755 }); + const path = process.env.PATH; + process.env.PATH = `${bin}:${path}`; + try { + await stopsTheTree(); + } finally { + process.env.PATH = path; + rmSync(bin, { recursive: true, force: true }); + } + }, + ); it("starts nothing for a signal that is already aborted", async () => { dir = mkdtempSync(join(tmpdir(), "hf-stoppable-exec-"));