Skip to content

Commit 1941809

Browse files
authored
Correct detection of running child process (#77)
1 parent b4cba04 commit 1941809

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/utils/execUtils.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,26 +85,32 @@ function killTree(childProcess: cp.ChildProcessWithoutNullStreams): Promise<void
8585
return;
8686
}
8787

88+
const childProcessPid = childProcess.pid;
89+
let sawChildProcessPid = false;
90+
8891
const childMap: Record<number, number[]> = {};
8992
const pidList = stdout.trim().split(/\s+/);
9093
for (let i = 0; i + 1 < pidList.length; i += 2) {
9194
const childPid = +pidList[i];
9295
const parentPid = +pidList[i + 1];
96+
9397
childMap[parentPid] ||= [];
9498
childMap[parentPid].push(childPid);
99+
100+
sawChildProcessPid ||= childPid === childProcessPid;
95101
}
96102

97-
if (!childMap[childProcess.pid]) {
103+
if (!sawChildProcessPid) {
98104
// Descendent processes may still be alive, but we have no way to identify them
99105
resolve();
100106
return;
101107
}
102108

103109
const strictDescendentPids: number[] = [];
104-
const stack: number[] = [ childProcess.pid ];
110+
const stack: number[] = [ childProcessPid ];
105111
while (stack.length) {
106112
const pid = stack.pop()!;
107-
if (pid !== childProcess.pid) {
113+
if (pid !== childProcessPid) {
108114
strictDescendentPids.push(pid);
109115
}
110116
const children = childMap[pid];
@@ -113,7 +119,7 @@ function killTree(childProcess: cp.ChildProcessWithoutNullStreams): Promise<void
113119
}
114120
}
115121

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

118124
strictDescendentPids.forEach(pid => process.kill(pid, "SIGKILL"));
119125
childProcess.kill("SIGKILL");

0 commit comments

Comments
 (0)