Files
ccs/src/utils/process-utils.ts
T
Tam Nhu Tran dc9b27623b test(utils): add unit tests for killWithEscalation
- Add 7 test cases covering SIGTERM/SIGKILL escalation, timer cleanup,
  default and custom grace periods, and already-exited process edge case
- Add timer.unref() to prevent keeping event loop alive during shutdown
- Add comment explaining 10s grace period in headless-executor timeout
2026-02-11 02:25:10 +07:00

22 lines
685 B
TypeScript

/**
* Process management utilities
*/
import { ChildProcess } from 'child_process';
/**
* Kill process with SIGTERM, escalating to SIGKILL if it doesn't exit.
* Uses exitCode === null (not proc.killed) to check if process is still running,
* since proc.killed only indicates a signal was sent, not that the process exited.
*/
export function killWithEscalation(proc: ChildProcess, gracePeriodMs = 3000): void {
proc.kill('SIGTERM');
const timer = setTimeout(() => {
if (proc.exitCode === null) {
proc.kill('SIGKILL');
}
}, gracePeriodMs);
timer.unref(); // Don't keep event loop alive just for escalation
proc.once('exit', () => clearTimeout(timer));
}