( info: ProcessInfo, timeoutMs: number, )
| 57 | } |
| 58 | |
| 59 | async function terminateProcess( |
| 60 | info: ProcessInfo, |
| 61 | timeoutMs: number, |
| 62 | ): Promise<{ usedForceKill?: boolean; error?: string }> { |
| 63 | try { |
| 64 | info.process.kill('SIGTERM'); |
| 65 | } catch (error) { |
| 66 | return { error: error instanceof Error ? error.message : String(error) }; |
| 67 | } |
| 68 | |
| 69 | const alreadyExited = info.process.exitCode != null || info.process.signalCode != null; |
| 70 | if (alreadyExited) { |
| 71 | return {}; |
| 72 | } |
| 73 | |
| 74 | let usedForceKill = false; |
| 75 | |
| 76 | const exitPromise = new Promise<'exited'>((resolve) => { |
| 77 | const onExit = (): void => resolve('exited'); |
| 78 | if (typeof info.process.once === 'function') { |
| 79 | info.process.once('exit', onExit); |
| 80 | return; |
| 81 | } |
| 82 | info.process.on('exit', onExit); |
| 83 | }); |
| 84 | |
| 85 | const outcome = await Promise.race([exitPromise, createTimeoutPromise(timeoutMs)]); |
| 86 | if (outcome === 'timed_out') { |
| 87 | try { |
| 88 | info.process.kill('SIGKILL'); |
| 89 | usedForceKill = true; |
| 90 | } catch { |
| 91 | // ignore |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return { usedForceKill }; |
| 96 | } |
| 97 | |
| 98 | export async function terminateTrackedProcess( |
| 99 | pid: number, |
no test coverage detected