Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/lib/tree-kill/tree-kill.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}
Expand Down
56 changes: 41 additions & 15 deletions src/lib/tree-kill/tree-kill.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -45,6 +63,7 @@ const buildProcessTree = (
pidsToProcess: { [key: number]: number },
helpers: Set<number>,
spawnChildProcessesList: (parentPid: string) => ChildProcessWithoutNullStreams,
parseChildren: ParseChildren,
cb: () => void,
): void => {
const ps = spawnChildProcessesList(parentPid.toString());
Expand All @@ -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();
});
};
Expand All @@ -96,11 +121,12 @@ export const treeKill = (pid: number, signal: NodeJS.Signals): Promise<void> =>
}

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();
});
Expand Down