mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-23 11:29:54 +00:00
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
This commit is contained in:
@@ -81,7 +81,36 @@ function signalProcessGroup(pid: number, signal: NodeJS.Signals): void {
|
||||
} catch {}
|
||||
}
|
||||
|
||||
function childrenFromProc(): Map<number, number[]> | undefined {
|
||||
try {
|
||||
const children = new Map<number, number[]>();
|
||||
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<number, number[]>, 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 {
|
||||
|
||||
@@ -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-"));
|
||||
|
||||
Reference in New Issue
Block a user