(pid: number)
| 29 | } |
| 30 | |
| 31 | export function killProcessTree(pid: number): void { |
| 32 | if (!Number.isFinite(pid) || pid <= 0) { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | // process.kill(-pid) is Unix-only; on Windows we must use taskkill to kill the full tree. |
| 37 | if (process.platform === "win32") { |
| 38 | try { |
| 39 | execFileSync("taskkill", ["/PID", String(pid), "/T", "/F"], { |
| 40 | stdio: "ignore", |
| 41 | windowsHide: true, |
| 42 | }); |
| 43 | } catch { |
| 44 | // Ignore errors - process may already have exited. |
| 45 | } |
| 46 | |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | // Prefer killing the entire process group. This requires the target process to be a group leader. |
| 51 | try { |
| 52 | process.kill(-pid, "SIGKILL"); |
| 53 | } catch { |
| 54 | // Fall back to just the individual process. |
| 55 | try { |
| 56 | process.kill(pid, "SIGKILL"); |
| 57 | } catch { |
| 58 | // ignore |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Disposable wrapper for child processes that ensures immediate cleanup. |
no test coverage detected