@devries-cameron20 / t3code-1 / commits / c4fa4629eb
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(provider-acp): force-kill terminal commands that ignore SIGTERM (#24596)

Co-authored-by: saphid <saphid@users.noreply.github.com>

Julius Marminge committed Oct 24, 2026 at 16:34 UTC c4fa4629eb645b95aae2beef56c70f705b736049
2 files changed +137 -13
packages/provider-acp/src/server/clientTerminals.test.ts
+130 -9
@@ -1,7 +1,13 @@
1 import * as NodeServices from "@effect/platform-node/NodeServices";
2 import { describe, expect, it } from "@effect/vitest";
3 +import * as Deferred from "effect/Deferred";
4 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";
8 import * as Result from "effect/Result";
9 +import * as Stream from "effect/Stream";
10 +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
11 import * as HostProcess from "@t3tools/shared/HostProcess";
12
13 import {
@@ -19,7 +25,7 @@ const withTerminals = <A, E>(use: (terminals: AcpClientTerminals) => Effect.Effe
25 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped);
26
27 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", () =>
29 withTerminals((terminals) =>
30 Effect.gen(function* () {
31 const created = yield* terminals.create({
@@ -45,7 +51,7 @@ describe("AcpClientTerminals", () => {
51 ),
52 );
53
48 - it.effect("resolves terminal environment from the requesting ACP session", () =>
54 + it.live("resolves terminal environment from the requesting ACP session", () =>
55 Effect.gen(function* () {
56 const terminals = yield* makeAcpClientTerminals({
57 defaultCwd: process.cwd(),
@@ -71,7 +77,7 @@ describe("AcpClientTerminals", () => {
77 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
78 );
79
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", () =>
81 Effect.gen(function* () {
82 const terminals = yield* makeAcpClientTerminals({
83 defaultCwd: process.cwd(),
@@ -90,7 +96,7 @@ describe("AcpClientTerminals", () => {
96 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
97 );
98
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", () =>
100 withTerminals((terminals) =>
101 Effect.gen(function* () {
102 const created = yield* terminals.create({
@@ -112,7 +118,7 @@ describe("AcpClientTerminals", () => {
118 ),
119 );
120
115 - it.effect("kills long-running commands and rejects released terminal handles", () =>
121 + it.live("kills long-running commands and rejects released terminal handles", () =>
122 withTerminals((terminals) =>
123 Effect.gen(function* () {
124 const created = yield* terminals.create({
@@ -144,7 +150,122 @@ describe("AcpClientTerminals", () => {
150 ),
151 );
152
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", () =>
269 withTerminals((terminals) =>
270 Effect.gen(function* () {
271 const results = yield* Effect.all(
@@ -166,7 +287,7 @@ describe("AcpClientTerminals", () => {
287 ),
288 );
289
169 - it.effect("bounds unreleased terminal handles without invalidating existing ids", () =>
290 + it.live("bounds unreleased terminal handles without invalidating existing ids", () =>
291 withTerminals((terminals) =>
292 Effect.gen(function* () {
293 const terminalIds: string[] = [];
@@ -197,7 +318,7 @@ describe("AcpClientTerminals", () => {
318 ),
319 );
320
200 - it.effect("bounds aggregate output retained across unreleased terminals", () =>
321 + it.live("bounds aggregate output retained across unreleased terminals", () =>
322 withTerminals((terminals) =>
323 Effect.gen(function* () {
324 const terminalIds: string[] = [];
@@ -228,7 +349,7 @@ describe("AcpClientTerminals", () => {
349 ),
350 );
351
231 - it.effect("rejects terminal handles from a different ACP session", () =>
352 + it.live("rejects terminal handles from a different ACP session", () =>
353 withTerminals((terminals) =>
354 Effect.gen(function* () {
355 const created = yield* terminals.create({
packages/provider-acp/src/server/clientTerminals.ts
+7 -4
@@ -137,13 +137,13 @@ function acpTerminalCommand(input: {
137 readonly shellCommands?: boolean | undefined;
138 readonly defaultCwd: string;
139 readonly environment?: NodeJS.ProcessEnv | undefined;
140 - readonly forceKillAfter?: Duration.Input | undefined;
140 + readonly forceKillAfter: Duration.Input;
141 }) {
142 return ChildProcess.make(input.request.command, input.request.args ?? [], {
143 cwd: input.request.cwd ?? input.defaultCwd,
144 ...(input.environment === undefined ? {} : { env: input.environment }),
145 shell: input.shellCommands === true && (input.request.args?.length ?? 0) === 0,
146 - forceKillAfter: input.forceKillAfter ?? "5 seconds",
146 + forceKillAfter: input.forceKillAfter,
147 });
148 }
149
@@ -151,6 +151,7 @@ export const makeAcpClientTerminals = Effect.fn("makeAcpClientTerminals")(functi
151 options: AcpClientTerminalsOptions,
152 ): Effect.fn.Return<AcpClientTerminals, never, ChildProcessSpawner.ChildProcessSpawner> {
153 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
154 + const forceKillAfter = options.forceKillAfter ?? "5 seconds";
155 const creationPermit = yield* Semaphore.make(1);
156 const terminals = new Map<string, ManagedAcpTerminal>();
157 let nextTerminalNumber = 1;
@@ -232,7 +233,7 @@ export const makeAcpClientTerminals = Effect.fn("makeAcpClientTerminals")(functi
233 shellCommands: options.shellCommands,
234 defaultCwd: options.defaultCwd,
235 environment,
235 - forceKillAfter: options.forceKillAfter,
236 + forceKillAfter,
237 }),
238 )
239 .pipe(
@@ -266,7 +267,9 @@ export const makeAcpClientTerminals = Effect.fn("makeAcpClientTerminals")(functi
267 ),
268 },
269 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),
273 dispose: Scope.close(terminalScope, Exit.void).pipe(Effect.ignore),
274 exitStatus: undefined,
275 released: false,