Skip to content

Commit 54f1410

Browse files
committed
Fix extension exec process-tree termination
1 parent 816038e commit 54f1410

2 files changed

Lines changed: 59 additions & 19 deletions

File tree

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

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import { EventEmitter } from "node:events";
44
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
55

6-
const { spawnMock, waitForChildProcessMock } = vi.hoisted(() => ({
6+
const { killProcessTreeMock, spawnMock, waitForChildProcessMock } = vi.hoisted(() => ({
7+
killProcessTreeMock: vi.fn(),
78
spawnMock: vi.fn(),
89
waitForChildProcessMock: vi.fn(),
910
}));
@@ -16,14 +17,20 @@ vi.mock("../utils/child-process.js", () => ({
1617
waitForChildProcess: waitForChildProcessMock,
1718
}));
1819

20+
vi.mock("../../process/kill-tree.js", () => ({
21+
killProcessTree: killProcessTreeMock,
22+
}));
23+
1924
type StubChild = EventEmitter & {
2025
kill: ReturnType<typeof vi.fn>;
26+
pid?: number;
2127
stderr: EventEmitter;
2228
stdout: EventEmitter;
2329
};
2430

2531
function createStubChild(): StubChild {
2632
const child = new EventEmitter() as StubChild;
33+
child.pid = 1234;
2734
child.stdout = new EventEmitter();
2835
child.stderr = new EventEmitter();
2936
child.kill = vi.fn();
@@ -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();
@@ -76,6 +84,26 @@ describe("execCommand", () => {
7684
expect(result.stderrTruncatedChars).toBeGreaterThan(0);
7785
});
7886

87+
it("spawns commands with process-tree cleanup options", async () => {
88+
const child = createStubChild();
89+
const wait = createDeferred<number | null>();
90+
spawnMock.mockReturnValue(child);
91+
waitForChildProcessMock.mockReturnValue(wait.promise);
92+
const { execCommand } = await import("./exec.js");
93+
94+
const resultPromise = execCommand("cmd", ["arg"], "/tmp");
95+
wait.resolve(0);
96+
await resultPromise;
97+
98+
expect(spawnMock).toHaveBeenCalledWith("cmd", ["arg"], {
99+
cwd: "/tmp",
100+
detached: process.platform !== "win32",
101+
shell: false,
102+
stdio: ["ignore", "pipe", "pipe"],
103+
windowsHide: true,
104+
});
105+
});
106+
79107
it("honors caller-supplied small output caps", async () => {
80108
const child = createStubChild();
81109
const wait = createDeferred<number | null>();
@@ -105,7 +133,11 @@ describe("execCommand", () => {
105133
wait.resolve(0);
106134

107135
const result = await resultPromise;
108-
expect(child.kill).toHaveBeenCalledWith("SIGTERM");
136+
expect(killProcessTreeMock).toHaveBeenCalledWith(1234, {
137+
detached: process.platform !== "win32",
138+
graceMs: 5000,
139+
});
140+
expect(child.kill).not.toHaveBeenCalled();
109141
expect(result.code).toBe(1);
110142
expect(result.killed).toBe(true);
111143
expect(result.outputLimitExceeded).toBe("stdout");
@@ -114,9 +146,9 @@ describe("execCommand", () => {
114146
expect(result.stderr).toContain("exec stdout exceeded output limit");
115147
});
116148

117-
it("escalates timed-out commands to SIGKILL after the grace period", async () => {
118-
// SIGTERM gives child processes a chance to clean up; SIGKILL is the
119-
// bounded fallback so an ignored signal cannot hang the session.
149+
it("terminates timed-out commands through the process-tree killer", async () => {
150+
// Extension exec uses the same tree-kill boundary as the built-in shell so
151+
// timed-out wrappers do not leave descendant processes running.
120152
vi.useFakeTimers();
121153
const child = createStubChild();
122154
const wait = createDeferred<number | null>();
@@ -126,13 +158,11 @@ describe("execCommand", () => {
126158

127159
const resultPromise = execCommand("cmd", [], "/tmp", { timeout: 10 });
128160
await vi.advanceTimersByTimeAsync(10);
129-
expect(child.kill).toHaveBeenCalledWith("SIGTERM");
130-
expect(child.kill).not.toHaveBeenCalledWith("SIGKILL");
131-
132-
await vi.advanceTimersByTimeAsync(4_999);
133-
expect(child.kill).not.toHaveBeenCalledWith("SIGKILL");
134-
await vi.advanceTimersByTimeAsync(1);
135-
expect(child.kill).toHaveBeenCalledWith("SIGKILL");
161+
expect(killProcessTreeMock).toHaveBeenCalledWith(1234, {
162+
detached: process.platform !== "win32",
163+
graceMs: 5000,
164+
});
165+
expect(child.kill).not.toHaveBeenCalled();
136166

137167
wait.resolve(null);
138168
const result = await resultPromise;

‎src/agents/sessions/exec.ts‎

Lines changed: 17 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,20 @@ 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) {
143+
killProcessTree(proc.pid, {
144+
detached: process.platform !== "win32",
145+
graceMs: FORCE_KILL_GRACE_MS,
146+
});
147+
} else {
148+
proc.kill("SIGTERM");
149+
forceKillTimer = setTimeout(() => {
150+
if (!settled) {
151+
proc.kill("SIGKILL");
152+
}
153+
}, FORCE_KILL_GRACE_MS);
154+
forceKillTimer.unref?.();
155+
}
146156
}
147157
};
148158

0 commit comments

Comments
 (0)