Skip to content

Commit 2499141

Browse files
committed
fix: kill extension exec descendants on timeout
1 parent 816038e commit 2499141

2 files changed

Lines changed: 96 additions & 10 deletions

File tree

‎src/agents/sessions/exec.test.ts‎

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// termination semantics used by agent sessions.
33
import { EventEmitter } from "node:events";
44
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
5+
import { withMockedPlatform } from "../../test-utils/vitest-spies.js";
56

6-
const { spawnMock, waitForChildProcessMock } = vi.hoisted(() => ({
7+
const { killProcessTreeMock, spawnMock, waitForChildProcessMock } = vi.hoisted(() => ({
8+
killProcessTreeMock: vi.fn(),
79
spawnMock: vi.fn(),
810
waitForChildProcessMock: vi.fn(),
911
}));
@@ -16,8 +18,13 @@ vi.mock("../utils/child-process.js", () => ({
1618
waitForChildProcess: waitForChildProcessMock,
1719
}));
1820

21+
vi.mock("../../process/kill-tree.js", () => ({
22+
killProcessTree: killProcessTreeMock,
23+
}));
24+
1925
type StubChild = EventEmitter & {
2026
kill: ReturnType<typeof vi.fn>;
27+
pid?: number;
2128
stderr: EventEmitter;
2229
stdout: EventEmitter;
2330
};
@@ -42,6 +49,7 @@ function createDeferred<T>() {
4249

4350
describe("execCommand", () => {
4451
beforeEach(() => {
52+
killProcessTreeMock.mockReset();
4553
spawnMock.mockReset();
4654
waitForChildProcessMock.mockReset();
4755
vi.useRealTimers();
@@ -93,8 +101,37 @@ describe("execCommand", () => {
93101
expect(result.stdoutTruncatedChars).toBe(3);
94102
});
95103

104+
it("spawns with a process group when the platform supports tree signaling", async () => {
105+
const { execCommand } = await import("./exec.js");
106+
107+
for (const [platform, expectedDetached] of [
108+
["linux", true],
109+
["win32", false],
110+
] as const) {
111+
await withMockedPlatform(platform, async () => {
112+
const child = createStubChild();
113+
const wait = createDeferred<number | null>();
114+
spawnMock.mockReturnValue(child);
115+
waitForChildProcessMock.mockReturnValue(wait.promise);
116+
117+
const resultPromise = execCommand("cmd", ["arg"], "/tmp");
118+
wait.resolve(0);
119+
await resultPromise;
120+
121+
expect(spawnMock).toHaveBeenLastCalledWith("cmd", ["arg"], {
122+
cwd: "/tmp",
123+
detached: expectedDetached,
124+
shell: false,
125+
stdio: ["ignore", "pipe", "pipe"],
126+
windowsHide: true,
127+
});
128+
});
129+
}
130+
});
131+
96132
it("fails instead of silently truncating default exec output", async () => {
97133
const child = createStubChild();
134+
child.pid = 2468;
98135
const wait = createDeferred<number | null>();
99136
spawnMock.mockReturnValue(child);
100137
waitForChildProcessMock.mockReturnValue(wait.promise);
@@ -105,7 +142,8 @@ describe("execCommand", () => {
105142
wait.resolve(0);
106143

107144
const result = await resultPromise;
108-
expect(child.kill).toHaveBeenCalledWith("SIGTERM");
145+
expect(killProcessTreeMock).toHaveBeenCalledWith(2468, { graceMs: 5000 });
146+
expect(child.kill).not.toHaveBeenCalled();
109147
expect(result.code).toBe(1);
110148
expect(result.killed).toBe(true);
111149
expect(result.outputLimitExceeded).toBe("stdout");
@@ -114,7 +152,47 @@ describe("execCommand", () => {
114152
expect(result.stderr).toContain("exec stdout exceeded output limit");
115153
});
116154

117-
it("escalates timed-out commands to SIGKILL after the grace period", async () => {
155+
it("kills the process tree when commands time out", async () => {
156+
vi.useFakeTimers();
157+
const child = createStubChild();
158+
child.pid = 1357;
159+
const wait = createDeferred<number | null>();
160+
spawnMock.mockReturnValue(child);
161+
waitForChildProcessMock.mockReturnValue(wait.promise);
162+
const { execCommand } = await import("./exec.js");
163+
164+
const resultPromise = execCommand("cmd", [], "/tmp", { timeout: 10 });
165+
await vi.advanceTimersByTimeAsync(10);
166+
167+
expect(killProcessTreeMock).toHaveBeenCalledWith(1357, { graceMs: 5000 });
168+
expect(child.kill).not.toHaveBeenCalled();
169+
170+
wait.resolve(null);
171+
const result = await resultPromise;
172+
expect(result.killed).toBe(true);
173+
});
174+
175+
it("kills the process tree when aborted", async () => {
176+
const child = createStubChild();
177+
child.pid = 9753;
178+
const wait = createDeferred<number | null>();
179+
const controller = new AbortController();
180+
spawnMock.mockReturnValue(child);
181+
waitForChildProcessMock.mockReturnValue(wait.promise);
182+
const { execCommand } = await import("./exec.js");
183+
184+
const resultPromise = execCommand("cmd", [], "/tmp", { signal: controller.signal });
185+
controller.abort();
186+
187+
expect(killProcessTreeMock).toHaveBeenCalledWith(9753, { graceMs: 5000 });
188+
expect(child.kill).not.toHaveBeenCalled();
189+
190+
wait.resolve(null);
191+
const result = await resultPromise;
192+
expect(result.killed).toBe(true);
193+
});
194+
195+
it("falls back to direct kill when timed-out commands have no pid", async () => {
118196
// SIGTERM gives child processes a chance to clean up; SIGKILL is the
119197
// bounded fallback so an ignored signal cannot hang the session.
120198
vi.useFakeTimers();
@@ -126,6 +204,7 @@ describe("execCommand", () => {
126204

127205
const resultPromise = execCommand("cmd", [], "/tmp", { timeout: 10 });
128206
await vi.advanceTimersByTimeAsync(10);
207+
expect(killProcessTreeMock).not.toHaveBeenCalled();
129208
expect(child.kill).toHaveBeenCalledWith("SIGTERM");
130209
expect(child.kill).not.toHaveBeenCalledWith("SIGKILL");
131210

‎src/agents/sessions/exec.ts‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
import { spawn } from "node:child_process";
6+
import { killProcessTree } from "../../process/kill-tree.js";
67
import { waitForChildProcess } from "../utils/child-process.js";
78

89
const DEFAULT_OUTPUT_LIMIT_CHARS = 16 * 1024 * 1024;
@@ -81,8 +82,10 @@ export async function execCommand(
8182
return new Promise((resolve) => {
8283
const proc = spawn(command, args, {
8384
cwd,
85+
detached: process.platform !== "win32",
8486
shell: false,
8587
stdio: ["ignore", "pipe", "pipe"],
88+
windowsHide: true,
8689
});
8790

8891
let stdout: OutputCapture = { text: "", truncatedChars: 0 };
@@ -136,13 +139,17 @@ export async function execCommand(
136139
const killProcess = () => {
137140
if (!killed) {
138141
killed = true;
139-
proc.kill("SIGTERM");
140-
forceKillTimer = setTimeout(() => {
141-
if (!settled) {
142-
proc.kill("SIGKILL");
143-
}
144-
}, FORCE_KILL_GRACE_MS);
145-
forceKillTimer.unref?.();
142+
if (proc.pid && proc.pid > 0) {
143+
killProcessTree(proc.pid, { graceMs: FORCE_KILL_GRACE_MS });
144+
} else {
145+
proc.kill("SIGTERM");
146+
forceKillTimer = setTimeout(() => {
147+
if (!settled) {
148+
proc.kill("SIGKILL");
149+
}
150+
}, FORCE_KILL_GRACE_MS);
151+
forceKillTimer.unref?.();
152+
}
146153
}
147154
};
148155

0 commit comments

Comments
 (0)