* Waits for a process to die, escalating to SIGKILL if SIGTERM doesn't work.
(pid: number, force: boolean)
| 119 | * Waits for a process to die, escalating to SIGKILL if SIGTERM doesn't work. |
| 120 | */ |
| 121 | async function waitForProcessToDie(pid: number, force: boolean): Promise<void> { |
| 122 | const maxWait = 2000; |
| 123 | const pollInterval = 20; |
| 124 | let waited = 0; |
| 125 | |
| 126 | while (isProcessAlive(pid) && waited < maxWait) { |
| 127 | await new Promise(r => setTimeout(r, pollInterval)); |
| 128 | waited += pollInterval; |
| 129 | } |
| 130 | |
| 131 | // If SIGTERM didn't work and we haven't tried SIGKILL yet, escalate |
| 132 | if (!force && isProcessAlive(pid)) { |
| 133 | try { |
| 134 | process.kill(pid, 'SIGKILL'); |
| 135 | } catch { |
| 136 | return; |
| 137 | } |
| 138 | waited = 0; |
| 139 | while (isProcessAlive(pid) && waited < 1000) { |
| 140 | await new Promise(r => setTimeout(r, pollInterval)); |
| 141 | waited += pollInterval; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | export async function killProcessByChildProcess( |
| 147 | child: ChildProcess, |
no test coverage detected