Skip to content

Correct detection of running child process #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2022
Merged
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
14 changes: 10 additions & 4 deletions src/utils/execUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,32 @@ function killTree(childProcess: cp.ChildProcessWithoutNullStreams): Promise<void
return;
}

const childProcessPid = childProcess.pid;
let sawChildProcessPid = false;

const childMap: Record<number, number[]> = {};
const pidList = stdout.trim().split(/\s+/);
for (let i = 0; i + 1 < pidList.length; i += 2) {
const childPid = +pidList[i];
const parentPid = +pidList[i + 1];

childMap[parentPid] ||= [];
childMap[parentPid].push(childPid);

sawChildProcessPid ||= childPid === childProcessPid;
}

if (!childMap[childProcess.pid]) {
if (!sawChildProcessPid) {
// Descendent processes may still be alive, but we have no way to identify them
resolve();
return;
}

const strictDescendentPids: number[] = [];
const stack: number[] = [ childProcess.pid ];
const stack: number[] = [ childProcessPid ];
while (stack.length) {
const pid = stack.pop()!;
if (pid !== childProcess.pid) {
if (pid !== childProcessPid) {
strictDescendentPids.push(pid);
}
const children = childMap[pid];
Expand All @@ -113,7 +119,7 @@ function killTree(childProcess: cp.ChildProcessWithoutNullStreams): Promise<void
}
}

console.log(`Killing process ${childProcess.pid} and its descendents: ${strictDescendentPids.join(", ")}`);
console.log(`Killing process ${childProcessPid} and its descendents: ${strictDescendentPids.join(", ")}`);

strictDescendentPids.forEach(pid => process.kill(pid, "SIGKILL"));
childProcess.kill("SIGKILL");
Expand Down