Skip to content

Commit

Permalink
fix: handle already terminated processes
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Jun 6, 2023
1 parent 2ba95ef commit e233233
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/killPsTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ export const killPsTree = async (
const pids = [rootPid, ...childPids];

for (const pid of pids) {
process.kill(pid, 'SIGTERM');
try {
process.kill(pid, 'SIGTERM');
} catch (error) {
if (error.code === 'ESRCH') {
log.debug({ pid }, 'process already terminated');
} else {
throw error;
}
}
}

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

for (const pid of hangingPids) {
process.kill(pid, 'SIGKILL');
try {
process.kill(pid, 'SIGKILL');
} catch (error) {
if (error.code === 'ESRCH') {
log.debug({ pid }, 'process already terminated');
} else {
throw error;
}
}
}
}, gracefulTimeout);

Expand Down

0 comments on commit e233233

Please sign in to comment.