From 96b7ec0a1a53ce0ba41ebc99cf7caa6db4b21bd9 Mon Sep 17 00:00:00 2001 From: MartinFILLON Date: Sun, 29 Nov 2026 10:57:02 +0100 Subject: [PATCH] fix(cli): use portable ps columns for tree killing Nightly images ship BusyBox ps, which does not support the GNU-only --ppid/--no-headers flags used by treeKill and its tests. The listing failed, so the process tree was empty and the tree-kill tests timed out. Use `ps -o pid,ppid` and filter by parent pid instead, which works with both GNU procps and BusyBox ps. Keep `pgrep -P` on macOS. --- src/lib/tree-kill/tree-kill.spec.ts | 19 ++++++---- src/lib/tree-kill/tree-kill.ts | 56 +++++++++++++++++++++-------- 2 files changed, 53 insertions(+), 22 deletions(-) diff --git a/src/lib/tree-kill/tree-kill.spec.ts b/src/lib/tree-kill/tree-kill.spec.ts index 556922b..5cfe465 100644 --- a/src/lib/tree-kill/tree-kill.spec.ts +++ b/src/lib/tree-kill/tree-kill.spec.ts @@ -28,13 +28,18 @@ const isAlive = (pid: number): boolean => { const childPids = (pid: number): number[] => { try { - const out = - process.platform === "darwin" - ? execFileSync("pgrep", ["-P", String(pid)], { encoding: "utf8" }) - : execFileSync("ps", ["-o", "pid", "--no-headers", "--ppid", String(pid)], { - encoding: "utf8", - }); - return out.split(/\s+/).filter(Boolean).map(Number); + if (process.platform === "darwin") { + const out = execFileSync("pgrep", ["-P", String(pid)], { encoding: "utf8" }); + return out.split(/\s+/).filter(Boolean).map(Number); + } + // `--ppid`/`--no-headers` are GNU-only; plain columns work on BusyBox too. + const out = execFileSync("ps", ["-o", "pid,ppid"], { encoding: "utf8" }); + const columns = out.match(/\d+/g) ?? []; + const children: number[] = []; + for (let i = 0; i + 1 < columns.length; i += 2) { + if (Number(columns[i + 1]) === pid) children.push(Number(columns[i])); + } + return children; } catch { return []; } diff --git a/src/lib/tree-kill/tree-kill.ts b/src/lib/tree-kill/tree-kill.ts index bb7366f..333d1ea 100755 --- a/src/lib/tree-kill/tree-kill.ts +++ b/src/lib/tree-kill/tree-kill.ts @@ -1,6 +1,24 @@ import { type ChildProcessWithoutNullStreams, execSync, spawn } from "node:child_process"; type ProcessTree = { [key: number]: number[] }; +type ParseChildren = (output: string, parentPid: number) => number[]; + +/** `pgrep -P` prints one pid per line. */ +const parsePgrepOutput = (output: string): number[] => output.match(/\d+/g)?.map(Number) ?? []; + +/** + * `ps -o pid,ppid` prints a header followed by `pid ppid` rows. These plain + * columns work with both GNU procps and BusyBox ps; `--ppid` and `--no-headers` + * are GNU-only and fail in Alpine-based CI images. + */ +const parsePsOutput: ParseChildren = (output, parentPid) => { + const columns = output.match(/\d+/g) ?? []; + const children: number[] = []; + for (let i = 0; i + 1 < columns.length; i += 2) { + if (Number(columns[i + 1]) === parentPid) children.push(Number(columns[i])); + } + return children; +}; const killPid = (pid: number, signal: NodeJS.Signals): void => { try { @@ -45,6 +63,7 @@ const buildProcessTree = ( pidsToProcess: { [key: number]: number }, helpers: Set, spawnChildProcessesList: (parentPid: string) => ChildProcessWithoutNullStreams, + parseChildren: ParseChildren, cb: () => void, ): void => { const ps = spawnChildProcessesList(parentPid.toString()); @@ -67,16 +86,22 @@ const buildProcessTree = ( return; } - if (allData) - allData.match(/\d+/g)?.forEach((childPid: string) => { - const intPid = Number(childPid); - if (helpers.has(intPid)) return; - if (tree[parentPid] === undefined) tree[parentPid] = []; - tree[parentPid].push(intPid); - tree[intPid] = []; - pidsToProcess[intPid] = 1; - buildProcessTree(intPid, tree, pidsToProcess, helpers, spawnChildProcessesList, cb); - }); + for (const childPid of parseChildren(allData, parentPid)) { + if (helpers.has(childPid)) continue; + if (tree[parentPid] === undefined) tree[parentPid] = []; + tree[parentPid].push(childPid); + tree[childPid] = []; + pidsToProcess[childPid] = 1; + buildProcessTree( + childPid, + tree, + pidsToProcess, + helpers, + spawnChildProcessesList, + parseChildren, + cb, + ); + } if (Object.keys(pidsToProcess).length === 0) cb(); }); }; @@ -96,11 +121,12 @@ export const treeKill = (pid: number, signal: NodeJS.Signals): Promise => } const tree: ProcessTree = { [pid]: [] }; - const listChildren = - process.platform === "darwin" - ? (parentPid: string) => spawn("pgrep", ["-P", parentPid]) - : (parentPid: string) => spawn("ps", ["-o", "pid", "--no-headers", "--ppid", parentPid]); - buildProcessTree(pid, tree, { [pid]: 1 }, new Set(), listChildren, () => { + const darwin = process.platform === "darwin"; + const listChildren = darwin + ? (parentPid: string) => spawn("pgrep", ["-P", parentPid]) + : () => spawn("ps", ["-o", "pid,ppid"]); + const parseChildren = darwin ? parsePgrepOutput : parsePsOutput; + buildProcessTree(pid, tree, { [pid]: 1 }, new Set(), listChildren, parseChildren, () => { killAll(tree, pid, signal); resolve(); });