|
1 | 1 | import { spawn, SpawnOptions } from 'node:child_process'; |
2 | 2 | import * as child_process from 'node:child_process'; |
3 | 3 | import { getGlobalVariable, getGlobalVariablesEnv } from './env'; |
4 | | -import { setTimeout as sleep } from 'node:timers/promises'; |
5 | 4 | import { delimiter, join, resolve } from 'node:path'; |
6 | 5 | import { stripVTControlCharacters, styleText } from 'node:util'; |
7 | 6 |
|
@@ -255,35 +254,33 @@ export async function waitForAnyProcessOutputToMatch( |
255 | 254 | return matchingProcess; |
256 | 255 | } |
257 | 256 |
|
| 257 | +async function killProcess(pid: number, signal: string): Promise<void> { |
| 258 | + if (process.platform === 'win32') { |
| 259 | + // /T kills child processes, /F forces it |
| 260 | + await exec(`taskkill /pid ${pid} /T /F`); |
| 261 | + } else { |
| 262 | + process.kill(-pid, signal); |
| 263 | + } |
| 264 | +} |
| 265 | + |
258 | 266 | /** |
259 | | - * Kills all tracked processes with a retry mechanism. |
| 267 | + * Kills all tracked processes |
260 | 268 | */ |
261 | 269 | export async function killAllProcesses(signal: NodeJS.Signals = 'SIGTERM'): Promise<void> { |
262 | 270 | let attempts = 0; |
263 | | - const maxRetries = 3; |
264 | | - |
265 | | - while (_processes.length > 0 && attempts < maxRetries) { |
| 271 | + while (_processes.length > 0) { |
266 | 272 | attempts++; |
267 | 273 |
|
268 | 274 | // Iterate backwards so we can remove elements while looping if needed. |
269 | 275 | for (let i = _processes.length - 1; i >= 0; i--) { |
270 | 276 | const childProc = _processes[i]; |
271 | 277 |
|
272 | | - if (!childProc || childProc.killed) { |
| 278 | + if (!childProc || childProc.killed || !childProc.pid) { |
273 | 279 | _processes.splice(i, 1); |
274 | 280 | continue; |
275 | 281 | } |
276 | 282 |
|
277 | | - const killed = childProc.kill(signal); |
278 | | - if (killed) { |
279 | | - _processes.splice(i, 1); |
280 | | - continue; |
281 | | - } |
282 | | - } |
283 | | - |
284 | | - // If still have processes, wait a bit before the next retry (e.g., 100ms) |
285 | | - if (_processes.length > 0 && attempts < maxRetries) { |
286 | | - await sleep(100); |
| 283 | + await killProcess(childProc.pid, signal); |
287 | 284 | } |
288 | 285 | } |
289 | 286 | } |
|
0 commit comments