Skip to content

Commit 1553c4f

Browse files
committed
fix(process): handle taskkill spawn errors
1 parent cc085da commit 1553c4f

4 files changed

Lines changed: 116 additions & 32 deletions

File tree

‎packages/agent-core/src/harness/env/kill-tree.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,12 @@ function signalProcessTreeUnix(
110110

111111
function runTaskkill(args: string[]): void {
112112
try {
113-
spawn("taskkill", args, {
113+
const child = spawn("taskkill", args, {
114114
stdio: "ignore",
115115
detached: true,
116116
windowsHide: true,
117117
});
118+
child.once("error", () => {});
118119
} catch {
119120
// Ignore taskkill spawn failures.
120121
}

‎src/process/exec.ts‎

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,32 @@ export async function runCommandWithTimeout(
514514
processTreeForceKillTimer = null;
515515
};
516516

517+
const killDirectChild = () => {
518+
if (
519+
settled ||
520+
childExitState != null ||
521+
child.exitCode != null ||
522+
child.signalCode != null
523+
) {
524+
return;
525+
}
526+
child.kill("SIGKILL");
527+
};
528+
529+
const spawnTaskkillOrFallback = (args: string[], onSpawnError: () => void): boolean => {
530+
try {
531+
const taskkillChild = spawn(getWindowsSystem32ExePath("taskkill.exe"), args, {
532+
stdio: "ignore",
533+
windowsHide: true,
534+
});
535+
taskkillChild.once("error", onSpawnError);
536+
return true;
537+
} catch {
538+
onSpawnError();
539+
return false;
540+
}
541+
};
542+
517543
const killChild = (byTimeout = true) => {
518544
if (settled || typeof child?.kill !== "function") {
519545
return;
@@ -525,12 +551,14 @@ export async function runCommandWithTimeout(
525551
}
526552
if (killProcessTree && typeof child.pid === "number" && child.pid > 0) {
527553
if (process.platform === "win32") {
528-
const taskkillPath = getWindowsSystem32ExePath("taskkill.exe");
529-
try {
530-
spawn(taskkillPath, ["/PID", String(child.pid), "/T"], {
531-
stdio: "ignore",
532-
windowsHide: true,
533-
});
554+
const taskkillStarted = spawnTaskkillOrFallback(
555+
["/PID", String(child.pid), "/T"],
556+
() => {
557+
clearProcessTreeForceKillTimer();
558+
killDirectChild();
559+
},
560+
);
561+
if (taskkillStarted) {
534562
if (!processTreeForceKillTimer) {
535563
processTreeForceKillTimer = setTimeout(() => {
536564
processTreeForceKillTimer = null;
@@ -542,41 +570,24 @@ export async function runCommandWithTimeout(
542570
) {
543571
return;
544572
}
545-
try {
546-
spawn(taskkillPath, ["/PID", String(child.pid), "/T", "/F"], {
547-
stdio: "ignore",
548-
windowsHide: true,
549-
});
550-
} catch {
551-
child.kill("SIGKILL");
552-
}
573+
spawnTaskkillOrFallback(
574+
["/PID", String(child.pid), "/T", "/F"],
575+
killDirectChild,
576+
);
553577
}, COMMAND_PROCESS_TREE_KILL_GRACE_MS);
554578
processTreeForceKillTimer.unref();
555579
}
556-
return;
557-
} catch {
558-
// Fall through to Node's direct child kill as a last resort.
559580
}
581+
return;
560582
}
561583
terminateProcessTree(child.pid, { graceMs: COMMAND_PROCESS_TREE_KILL_GRACE_MS });
562584
return;
563585
}
564586
if (process.platform === "win32" && typeof child.pid === "number" && child.pid > 0) {
565-
try {
566-
spawn(
567-
getWindowsSystem32ExePath("taskkill.exe"),
568-
["/PID", String(child.pid), "/T", "/F"],
569-
{
570-
stdio: "ignore",
571-
windowsHide: true,
572-
},
573-
);
574-
return;
575-
} catch {
576-
// Fall through to Node's direct child kill as a last resort.
577-
}
587+
spawnTaskkillOrFallback(["/PID", String(child.pid), "/T", "/F"], killDirectChild);
588+
return;
578589
}
579-
child.kill("SIGKILL");
590+
killDirectChild();
580591
};
581592

582593
const armNoOutputTimer = () => {

‎src/process/exec.windows.test.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,65 @@ describe("windows command wrapper behavior", () => {
514514
}
515515
});
516516

517+
it("falls back to direct child kill when forced Windows taskkill emits a spawn error", async () => {
518+
vi.useFakeTimers();
519+
const child = createMockChild({ autoClose: false });
520+
child.exitCode = null;
521+
const taskkillChild = createMockChild({ autoClose: false });
522+
523+
spawnMock.mockImplementationOnce(() => child).mockImplementationOnce(() => taskkillChild);
524+
525+
try {
526+
await withMockedWindowsPlatform(async () => {
527+
const resultPromise = runCommandWithTimeout(["node", "idle.js"], { timeoutMs: 80 });
528+
529+
await vi.advanceTimersByTimeAsync(81);
530+
taskkillChild.emit("error", Object.assign(new Error("spawn ENOENT"), { code: "ENOENT" }));
531+
532+
expect(child.kill).toHaveBeenCalledWith("SIGKILL");
533+
534+
child.emit("close", null, "SIGKILL");
535+
const result = await resultPromise;
536+
expect(result.termination).toBe("timeout");
537+
expect(result.code).toBe(124);
538+
});
539+
} finally {
540+
vi.useRealTimers();
541+
}
542+
});
543+
544+
it("falls back to direct child kill when graceful Windows taskkill emits a spawn error", async () => {
545+
vi.useFakeTimers();
546+
const child = createMockChild({ autoClose: false });
547+
child.exitCode = null;
548+
const taskkillChild = createMockChild({ autoClose: false });
549+
550+
spawnMock.mockImplementationOnce(() => child).mockImplementationOnce(() => taskkillChild);
551+
552+
try {
553+
await withMockedWindowsPlatform(async () => {
554+
const resultPromise = runCommandWithTimeout(["node", "idle.js"], {
555+
killProcessTree: true,
556+
timeoutMs: 80,
557+
});
558+
559+
await vi.advanceTimersByTimeAsync(81);
560+
taskkillChild.emit("error", Object.assign(new Error("spawn ENOENT"), { code: "ENOENT" }));
561+
562+
expect(child.kill).toHaveBeenCalledWith("SIGKILL");
563+
await vi.advanceTimersByTimeAsync(300);
564+
expect(spawnMock).toHaveBeenCalledTimes(2);
565+
566+
child.emit("close", null, "SIGKILL");
567+
const result = await resultPromise;
568+
expect(result.termination).toBe("timeout");
569+
expect(result.code).toBe(124);
570+
});
571+
} finally {
572+
vi.useRealTimers();
573+
}
574+
});
575+
517576
it("decodes GBK stdout and stderr from runExec on Windows", async () => {
518577
const stdout = Buffer.from([0xb2, 0xe2, 0xca, 0xd4]);
519578
const stderr = Buffer.from([0xa3, 0xbb]);

‎src/process/kill-tree.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Kill tree tests cover process tree termination and platform-specific fallbacks.
2+
import { EventEmitter } from "node:events";
23
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
34
import { withMockedPlatform } from "../test-utils/vitest-spies.js";
45

@@ -237,4 +238,16 @@ describe("killProcessTree", () => {
237238
expectTaskkillCall(0, ["/F", "/T", "/PID", "9999"]);
238239
});
239240
});
241+
242+
it("on Windows ignores async taskkill spawn errors", async () => {
243+
const taskkillChild = new EventEmitter();
244+
spawnMock.mockReturnValueOnce(taskkillChild);
245+
246+
await withMockedPlatform("win32", async () => {
247+
killProcessTree(9191, { force: true });
248+
249+
expect(() => taskkillChild.emit("error", new Error("spawn ENOENT"))).not.toThrow();
250+
expectTaskkillCall(0, ["/F", "/T", "/PID", "9191"]);
251+
});
252+
});
240253
});

0 commit comments

Comments
 (0)