Skip to content

Commit e233233

Browse files
committed
fix: handle already terminated processes
1 parent 2ba95ef commit e233233

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/killPsTree.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ export const killPsTree = async (
1616
const pids = [rootPid, ...childPids];
1717

1818
for (const pid of pids) {
19-
process.kill(pid, 'SIGTERM');
19+
try {
20+
process.kill(pid, 'SIGTERM');
21+
} catch (error) {
22+
if (error.code === 'ESRCH') {
23+
log.debug({ pid }, 'process already terminated');
24+
} else {
25+
throw error;
26+
}
27+
}
2028
}
2129

2230
let hangingPids = [...pids];
@@ -29,7 +37,15 @@ export const killPsTree = async (
2937
log.debug({ hangingPids }, 'sending SIGKILL to processes...');
3038

3139
for (const pid of hangingPids) {
32-
process.kill(pid, 'SIGKILL');
40+
try {
41+
process.kill(pid, 'SIGKILL');
42+
} catch (error) {
43+
if (error.code === 'ESRCH') {
44+
log.debug({ pid }, 'process already terminated');
45+
} else {
46+
throw error;
47+
}
48+
}
3349
}
3450
}, gracefulTimeout);
3551

0 commit comments

Comments
 (0)