Skip to content

Commit c4fa462

Browse files
fix(provider-acp): force-kill terminal commands that ignore SIGTERM (#24596)
Co-authored-by: saphid <[email protected]>
1 parent 7fd2e09 commit c4fa462

2 files changed

Lines changed: 137 additions & 13 deletions

File tree

‎packages/provider-acp/src/server/clientTerminals.test.ts‎

Lines changed: 130 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
22
import { describe, expect, it } from "@effect/vitest";
3+
import * as Deferred from "effect/Deferred";
34
import * as Effect from "effect/Effect";
5+
import * as Fiber from "effect/Fiber";
6+
import * as Sink from "effect/Sink";
7+
import * as TestClock from "effect/testing/TestClock";
48
import * as Result from "effect/Result";
9+
import * as Stream from "effect/Stream";
10+
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
511
import * as HostProcess from "@t3tools/shared/HostProcess";
612

713
import {
@@ -19,7 +25,7 @@ const withTerminals = <A, E>(use: (terminals: AcpClientTerminals) => Effect.Effe
1925
}).pipe(Effect.provide(NodeServices.layer), Effect.scoped);
2026

2127
describe("AcpClientTerminals", () => {
22-
it.effect("runs a command, buffers output, and reports the exit status", () =>
28+
it.live("runs a command, buffers output, and reports the exit status", () =>
2329
withTerminals((terminals) =>
2430
Effect.gen(function* () {
2531
const created = yield* terminals.create({
@@ -45,7 +51,7 @@ describe("AcpClientTerminals", () => {
4551
),
4652
);
4753

48-
it.effect("resolves terminal environment from the requesting ACP session", () =>
54+
it.live("resolves terminal environment from the requesting ACP session", () =>
4955
Effect.gen(function* () {
5056
const terminals = yield* makeAcpClientTerminals({
5157
defaultCwd: process.cwd(),
@@ -71,7 +77,7 @@ describe("AcpClientTerminals", () => {
7177
}).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
7278
);
7379

74-
it.effect("runs Devin shell source with arguments, pipes, and session fallback variables", () =>
80+
it.live("runs Devin shell source with arguments, pipes, and session fallback variables", () =>
7581
Effect.gen(function* () {
7682
const terminals = yield* makeAcpClientTerminals({
7783
defaultCwd: process.cwd(),
@@ -90,7 +96,7 @@ describe("AcpClientTerminals", () => {
9096
}).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
9197
);
9298

93-
it.effect("truncates buffered output from the beginning at the byte limit", () =>
99+
it.live("truncates buffered output from the beginning at the byte limit", () =>
94100
withTerminals((terminals) =>
95101
Effect.gen(function* () {
96102
const created = yield* terminals.create({
@@ -112,7 +118,7 @@ describe("AcpClientTerminals", () => {
112118
),
113119
);
114120

115-
it.effect("kills long-running commands and rejects released terminal handles", () =>
121+
it.live("kills long-running commands and rejects released terminal handles", () =>
116122
withTerminals((terminals) =>
117123
Effect.gen(function* () {
118124
const created = yield* terminals.create({
@@ -144,7 +150,122 @@ describe("AcpClientTerminals", () => {
144150
),
145151
);
146152

147-
it.effect("reserves terminal capacity across concurrent creates", () =>
153+
it.effect.each([
154+
{ deadline: "default", forceKillAfter: undefined, deadlineMillis: 5000 },
155+
{ deadline: "configured", forceKillAfter: "250 millis" as const, deadlineMillis: 250 },
156+
])(
157+
"waits for the $deadline deadline before force-killing a SIGTERM-ignoring terminal",
158+
({ forceKillAfter, deadlineMillis }) =>
159+
Effect.gen(function* () {
160+
const exited = yield* Deferred.make<ChildProcessSpawner.ExitCode>();
161+
const child = ChildProcessSpawner.makeHandle({
162+
pid: ChildProcessSpawner.ProcessId(1),
163+
exitCode: Deferred.await(exited),
164+
isRunning: Deferred.isDone(exited).pipe(Effect.map((done) => !done)),
165+
// Model a process that only exits when the spawner escalates after its deadline.
166+
kill: (options) =>
167+
options?.forceKillAfter === undefined
168+
? Deferred.await(exited).pipe(Effect.asVoid)
169+
: Effect.sleep(options.forceKillAfter).pipe(
170+
Effect.andThen(Deferred.succeed(exited, ChildProcessSpawner.ExitCode(137))),
171+
Effect.asVoid,
172+
),
173+
stdin: Sink.drain,
174+
stdout: Stream.empty,
175+
stderr: Stream.empty,
176+
all: Stream.empty,
177+
getInputFd: () => Sink.drain,
178+
getOutputFd: () => Stream.empty,
179+
unref: Effect.succeed(Effect.void),
180+
});
181+
const terminals = yield* makeAcpClientTerminals({
182+
defaultCwd: process.cwd(),
183+
...(forceKillAfter === undefined ? {} : { forceKillAfter }),
184+
}).pipe(
185+
Effect.provideService(
186+
ChildProcessSpawner.ChildProcessSpawner,
187+
ChildProcessSpawner.make(() => Effect.succeed(child)),
188+
),
189+
);
190+
yield* Effect.addFinalizer(() =>
191+
Deferred.succeed(exited, ChildProcessSpawner.ExitCode(137)).pipe(
192+
Effect.andThen(terminals.disposeAll),
193+
),
194+
);
195+
const terminal = yield* terminals.create({
196+
sessionId: "session",
197+
command: "ignores-SIGTERM",
198+
});
199+
const request = { sessionId: "session", ...terminal };
200+
const killing = yield* terminals.kill(request).pipe(Effect.forkChild);
201+
const waiting = yield* terminals.waitForExit(request).pipe(Effect.forkChild);
202+
203+
yield* TestClock.adjust(deadlineMillis - 1);
204+
expect(yield* child.isRunning).toBe(true);
205+
expect(killing.pollUnsafe()).toBeUndefined();
206+
expect(waiting.pollUnsafe()).toBeUndefined();
207+
yield* TestClock.adjust("1 millis");
208+
expect(yield* child.isRunning).toBe(false);
209+
yield* Fiber.join(killing);
210+
expect(yield* Fiber.join(waiting)).toEqual({ exitCode: 137, signal: null });
211+
}).pipe(Effect.scoped),
212+
);
213+
214+
it.live("force-kills commands that ignore SIGTERM and resolves wait_for_exit", () =>
215+
Effect.gen(function* () {
216+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
217+
const ready = yield* Deferred.make<void>();
218+
const spawned = yield* Deferred.make<ChildProcessSpawner.ChildProcessHandle>();
219+
let stdout = "";
220+
const terminals = yield* makeAcpClientTerminals({
221+
defaultCwd: process.cwd(),
222+
forceKillAfter: "50 millis",
223+
}).pipe(
224+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, {
225+
...spawner,
226+
spawn: (command) =>
227+
spawner.spawn(command).pipe(
228+
Effect.tap((child) => Deferred.succeed(spawned, child)),
229+
Effect.map((child) => ({
230+
...child,
231+
stdout: child.stdout.pipe(
232+
Stream.tap((chunk) => {
233+
stdout += Buffer.from(chunk).toString("utf8");
234+
return stdout.includes("ready")
235+
? Deferred.succeed(ready, undefined)
236+
: Effect.void;
237+
}),
238+
),
239+
})),
240+
),
241+
}),
242+
);
243+
yield* Effect.addFinalizer(() => terminals.disposeAll);
244+
const terminal = yield* terminals.create({
245+
sessionId: "session",
246+
command: process.execPath,
247+
args: [
248+
"-e",
249+
"process.on('SIGTERM', () => {}); console.log('ready'); setInterval(() => {}, 1000);",
250+
],
251+
});
252+
const child = yield* Deferred.await(spawned);
253+
// Reap only this test's captured child, including when the regression fails.
254+
yield* Effect.addFinalizer(() => child.kill({ killSignal: "SIGKILL" }).pipe(Effect.ignore));
255+
yield* Deferred.await(ready);
256+
257+
const request = { sessionId: "session", ...terminal };
258+
yield* terminals.kill(request);
259+
const exit = yield* terminals.waitForExit(request);
260+
// Windows has no signals: the command is terminated without a signal name.
261+
if (!(yield* HostProcess.isWindows)) {
262+
expect(exit).toEqual({ exitCode: null, signal: "SIGKILL" });
263+
}
264+
expect(yield* child.isRunning).toBe(false);
265+
}).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
266+
);
267+
268+
it.live("reserves terminal capacity across concurrent creates", () =>
148269
withTerminals((terminals) =>
149270
Effect.gen(function* () {
150271
const results = yield* Effect.all(
@@ -166,7 +287,7 @@ describe("AcpClientTerminals", () => {
166287
),
167288
);
168289

169-
it.effect("bounds unreleased terminal handles without invalidating existing ids", () =>
290+
it.live("bounds unreleased terminal handles without invalidating existing ids", () =>
170291
withTerminals((terminals) =>
171292
Effect.gen(function* () {
172293
const terminalIds: string[] = [];
@@ -197,7 +318,7 @@ describe("AcpClientTerminals", () => {
197318
),
198319
);
199320

200-
it.effect("bounds aggregate output retained across unreleased terminals", () =>
321+
it.live("bounds aggregate output retained across unreleased terminals", () =>
201322
withTerminals((terminals) =>
202323
Effect.gen(function* () {
203324
const terminalIds: string[] = [];
@@ -228,7 +349,7 @@ describe("AcpClientTerminals", () => {
228349
),
229350
);
230351

231-
it.effect("rejects terminal handles from a different ACP session", () =>
352+
it.live("rejects terminal handles from a different ACP session", () =>
232353
withTerminals((terminals) =>
233354
Effect.gen(function* () {
234355
const created = yield* terminals.create({

‎packages/provider-acp/src/server/clientTerminals.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,21 @@ function acpTerminalCommand(input: {
137137
readonly shellCommands?: boolean | undefined;
138138
readonly defaultCwd: string;
139139
readonly environment?: NodeJS.ProcessEnv | undefined;
140-
readonly forceKillAfter?: Duration.Input | undefined;
140+
readonly forceKillAfter: Duration.Input;
141141
}) {
142142
return ChildProcess.make(input.request.command, input.request.args ?? [], {
143143
cwd: input.request.cwd ?? input.defaultCwd,
144144
...(input.environment === undefined ? {} : { env: input.environment }),
145145
shell: input.shellCommands === true && (input.request.args?.length ?? 0) === 0,
146-
forceKillAfter: input.forceKillAfter ?? "5 seconds",
146+
forceKillAfter: input.forceKillAfter,
147147
});
148148
}
149149

150150
export const makeAcpClientTerminals = Effect.fn("makeAcpClientTerminals")(function* (
151151
options: AcpClientTerminalsOptions,
152152
): Effect.fn.Return<AcpClientTerminals, never, ChildProcessSpawner.ChildProcessSpawner> {
153153
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
154+
const forceKillAfter = options.forceKillAfter ?? "5 seconds";
154155
const creationPermit = yield* Semaphore.make(1);
155156
const terminals = new Map<string, ManagedAcpTerminal>();
156157
let nextTerminalNumber = 1;
@@ -232,7 +233,7 @@ export const makeAcpClientTerminals = Effect.fn("makeAcpClientTerminals")(functi
232233
shellCommands: options.shellCommands,
233234
defaultCwd: options.defaultCwd,
234235
environment,
235-
forceKillAfter: options.forceKillAfter,
236+
forceKillAfter,
236237
}),
237238
)
238239
.pipe(
@@ -266,7 +267,9 @@ export const makeAcpClientTerminals = Effect.fn("makeAcpClientTerminals")(functi
266267
),
267268
},
268269
exit: yield* Deferred.make<EffectAcpSchema.WaitForTerminalExitResponse>(),
269-
kill: child.kill().pipe(Effect.ignore),
270+
// The spawner only applies the command's forceKillAfter on scope close;
271+
// an explicit kill must pass it or a SIGTERM-ignoring command never exits.
272+
kill: child.kill({ forceKillAfter }).pipe(Effect.ignore),
270273
dispose: Scope.close(terminalScope, Exit.void).pipe(Effect.ignore),
271274
exitStatus: undefined,
272275
released: false,

0 commit comments

Comments
 (0)