From 9d13dfa5e75c705f4c0cf9f24b8fc7b8b1c4abad Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Thu, 12 Mar 2026 15:14:21 -0700 Subject: [PATCH] kill escaped procs inline, guard pid > 1 --- src/run.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/run.ts b/src/run.ts index b5fbcc6..666cd78 100644 --- a/src/run.ts +++ b/src/run.ts @@ -29,23 +29,22 @@ type RunOptions = { } function killTree(pid: number): void { - // Find any processes that escaped the process group (e.g. via setsid) - // using a single cross-platform ps call, then kill them before the group. + // Find any processes that escaped the process group (e.g. via setsid). + // This assumes pid === pgid, which holds because the child is spawned + // with detached: true (making it a process group leader). try { const result = Bun.spawnSync(["ps", "-eo", "pid,pgid"]) const output = result.stdout.toString() const pgid = String(pid) - const escapees: number[] = [] for (const line of output.split("\n")) { const parts = line.trim().split(/\s+/) if (parts[1] === pgid) { const p = parseInt(parts[0]!, 10) - if (!isNaN(p) && p !== pid) escapees.push(p) + if (!isNaN(p) && p !== pid && p > 1) { + try { process.kill(p, "SIGKILL") } catch {} + } } } - for (const p of escapees) { - try { process.kill(p, "SIGKILL") } catch {} - } } catch {} // Kill the process group try { process.kill(-pid, "SIGKILL") } catch {}