fix(delegation): use exitCode instead of killed for process termination checks

- Replace `!proc.killed` with `proc.exitCode === null` in headless-executor
- Extract `killWithEscalation` helper for SIGTERM→SIGKILL escalation
- Fixes pre-existing dead code where SIGKILL was never sent
This commit is contained in:
Tam Nhu Tran
2026-02-11 01:58:35 +07:00
parent 086bf958e9
commit 4b1fcacf30
+18 -11
View File
@@ -5,7 +5,20 @@
* Spawns claude with -p flag for single-turn execution * Spawns claude with -p flag for single-turn execution
*/ */
import { spawn } from 'child_process'; import { spawn, ChildProcess } from 'child_process';
/**
* Kill process with SIGTERM, escalating to SIGKILL if it doesn't exit
*/
function killWithEscalation(proc: ChildProcess, gracePeriodMs = 3000): void {
proc.kill('SIGTERM');
const timer = setTimeout(() => {
if (proc.exitCode === null) {
proc.kill('SIGKILL');
}
}, gracePeriodMs);
proc.once('exit', () => clearTimeout(timer));
}
import * as path from 'path'; import * as path from 'path';
import * as fs from 'fs'; import * as fs from 'fs';
import { SessionManager } from './session-manager'; import { SessionManager } from './session-manager';
@@ -214,11 +227,8 @@ export class HeadlessExecutor {
// Setup signal handlers for cleanup // Setup signal handlers for cleanup
const cleanupHandler = () => { const cleanupHandler = () => {
if (!proc.killed) { if (proc.exitCode === null) {
proc.kill('SIGTERM'); killWithEscalation(proc, 2000);
setTimeout(() => {
if (!proc.killed) proc.kill('SIGKILL');
}, 2000);
} }
}; };
process.once('SIGINT', cleanupHandler); process.once('SIGINT', cleanupHandler);
@@ -326,16 +336,13 @@ export class HeadlessExecutor {
// Handle timeout // Handle timeout
if (timeout > 0) { if (timeout > 0) {
const timeoutHandle = setTimeout(() => { const timeoutHandle = setTimeout(() => {
if (!proc.killed) { if (proc.exitCode === null) {
timedOut = true; timedOut = true;
if (progressInterval) { if (progressInterval) {
clearInterval(progressInterval); clearInterval(progressInterval);
process.stderr.write('\r\x1b[K'); process.stderr.write('\r\x1b[K');
} }
proc.kill('SIGTERM'); killWithEscalation(proc, 10000);
setTimeout(() => {
if (!proc.killed) proc.kill('SIGKILL');
}, 10000);
} }
}, timeout); }, timeout);
proc.on('close', () => clearTimeout(timeoutHandle)); proc.on('close', () => clearTimeout(timeoutHandle));