Skip to content

Commit 4cf26f9

Browse files
krissdingclaude
andcommitted
fix(process): handle async spawn errors in taskkill force-kill fallback
Node's spawn() emits 'error' asynchronously when the binary is missing or fails to execute. The try/catch at the force-kill taskkill site (line 546) only catches synchronous throws, so an async spawn error permanently leaks the child process tree on Windows. Add an 'error' event listener that falls back to child.kill("SIGKILL"), mirroring the existing catch-block behavior. Fixes #101381 Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
1 parent ad833d7 commit 4cf26f9

2 files changed

Lines changed: 83 additions & 1 deletion

File tree

‎src/process/exec.test.ts‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,85 @@ describe("runCommandWithTimeout", () => {
414414

415415
expect(result.preservedStdoutLines).toEqual(["x".repeat(22)]);
416416
});
417+
418+
it(
419+
"falls back to SIGKILL when force-kill taskkill spawn emits async error",
420+
{ timeout: 5_000 },
421+
async () => {
422+
vi.useFakeTimers();
423+
424+
const platformStub = vi.spyOn(process, "platform", "get").mockReturnValue("win32");
425+
426+
// Create a child that survives the first taskkill so the force-kill
427+
// timer fires: exitCode/signalCode must stay null.
428+
const mainChild = new EventEmitter() as EventEmitter & ChildProcess;
429+
mainChild.stdout = new EventEmitter() as NonNullable<ChildProcess["stdout"]>;
430+
mainChild.stderr = new EventEmitter() as NonNullable<ChildProcess["stderr"]>;
431+
mainChild.stdin = new EventEmitter() as NonNullable<ChildProcess["stdin"]>;
432+
mainChild.stdin.write = vi.fn(() => true) as NonNullable<ChildProcess["stdin"]>["write"];
433+
mainChild.stdin.end = vi.fn() as NonNullable<ChildProcess["stdin"]>["end"];
434+
let mainKilled = false;
435+
const killSpy = vi.fn((_signal?: NodeJS.Signals) => {
436+
mainKilled = true;
437+
return true;
438+
});
439+
mainChild.kill = killSpy as ChildProcess["kill"];
440+
Object.defineProperties(mainChild, {
441+
pid: { get: () => 5678 },
442+
killed: { get: () => mainKilled },
443+
exitCode: { get: () => null },
444+
signalCode: { get: () => null },
445+
});
446+
447+
let forceKillChild: EventEmitter | undefined;
448+
449+
let spawnCount = 0;
450+
spawnMock.mockImplementation(() => {
451+
spawnCount++;
452+
if (spawnCount >= 3) {
453+
// Third spawn: force-kill taskkill inside the 300ms timer.
454+
forceKillChild = new EventEmitter();
455+
(forceKillChild as EventEmitter & { kill?: unknown }).kill = vi.fn(() => true);
456+
return forceKillChild;
457+
}
458+
// First spawn: main child. Second spawn: first taskkill.
459+
if (spawnCount === 1) return mainChild;
460+
// Return a dummy for the first taskkill so it doesn't crash.
461+
const dummy = new EventEmitter();
462+
(dummy as EventEmitter & { kill?: unknown }).kill = vi.fn(() => true);
463+
return dummy;
464+
});
465+
466+
await loadExecModules({ mockSpawn: true });
467+
468+
const resultPromise = runCommandWithTimeout(createSilentIdleArgv(), {
469+
timeoutMs: 100,
470+
killProcessTree: true,
471+
});
472+
473+
// Advance past the timeout to trigger killChild → first taskkill → timer.
474+
await vi.advanceTimersByTimeAsync(150);
475+
// Advance past COMMAND_PROCESS_TREE_KILL_GRACE_MS so the force-kill spawns.
476+
await vi.advanceTimersByTimeAsync(350);
477+
478+
if (!forceKillChild) {
479+
throw new Error("expected force-kill child to be spawned");
480+
}
481+
482+
// Async spawn error: taskkill.exe not found / failed to execute.
483+
forceKillChild.emit("error", new Error("taskkill.exe not found"));
484+
485+
expect(killSpy).toHaveBeenCalledWith("SIGKILL");
486+
487+
// Let the process settle.
488+
mainChild.emit("exit", null, "SIGTERM");
489+
mainChild.emit("close", null, "SIGTERM");
490+
await vi.runAllTimersAsync();
491+
await resultPromise;
492+
493+
platformStub.mockRestore();
494+
},
495+
);
417496
});
418497

419498
describe("attachChildProcessBridge", () => {

‎src/process/exec.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,10 +543,13 @@ export async function runCommandWithTimeout(
543543
return;
544544
}
545545
try {
546-
spawn(taskkillPath, ["/PID", String(child.pid), "/T", "/F"], {
546+
const forceKill = spawn(taskkillPath, ["/PID", String(child.pid), "/T", "/F"], {
547547
stdio: "ignore",
548548
windowsHide: true,
549549
});
550+
forceKill.on("error", () => {
551+
child.kill("SIGKILL");
552+
});
550553
} catch {
551554
child.kill("SIGKILL");
552555
}

0 commit comments

Comments
 (0)