The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 00:30 UTC
fix(server): keep helper processes from outliving the backend (#12513)
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
tachytelicdetonation committed
Oct 19, 2026 at 20:36 UTC
e85539ab59f13bafd156f831d2e6690df67eb677
7 files changed
+281
-4
apps/server/src/processRunner.test.ts
+124
@@ -1,18 +1,24 @@
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 Duration from "effect/Duration";
5
import * as Effect from "effect/Effect";
6
import * as Fiber from "effect/Fiber";
7
+import * as FileSystem from "effect/FileSystem";
8
import * as Layer from "effect/Layer";
9
+import * as Path from "effect/Path";
10
import * as PlatformError from "effect/PlatformError";
11
import * as Sink from "effect/Sink";
12
import * as Stream from "effect/Stream";
13
import { TestClock } from "effect/testing";
14
import { ChildProcessSpawner } from "effect/process";
15
+import * as ChildProcess from "effect/process/ChildProcess";
16
import * as HostProcess from "@t3tools/shared/HostProcess";
17
import { SpawnExecutableResolution } from "@t3tools/shared/shell";
18
19
import * as ProcessRunner from "./processRunner.ts";
20
+import { writeFakeCli } from "@t3tools/provider-testing/fakeCli";
21
+import { makeProcessProbe, processProbeSource } from "./testUtils/processProbe.ts";
22
23
type ChildProcessCommand = {
24
readonly command: string;
@@ -398,6 +404,124 @@ describe("runProcess", () => {
404
}),
405
);
406
407
+ it.effect("kills a helper that ignores SIGTERM when its run times out", () =>
408
+ Effect.gen(function* () {
409
+ const fs = yield* FileSystem.FileSystem;
410
+ const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-process-runner-" });
411
+ const probe = yield* makeProcessProbe;
412
+ const helper = writeFakeCli({
413
+ directory,
414
+ name: "stubborn",
415
+ source: `process.on("SIGTERM", () => {}); ${processProbeSource(probe.port)}`,
416
+ });
417
+ const runner = yield* ProcessRunner.ProcessRunner;
418
+ const fiber = yield* runner
419
+ .run({ command: helper, args: [], timeout: "1 second" })
420
+ .pipe(Effect.flip, Effect.forkScoped);
421
+ yield* probe.started;
422
+
423
+ yield* TestClock.adjust("1 second");
424
+ yield* probe.exited;
425
+ expect((yield* Fiber.join(fiber))._tag).toBe("ProcessTimeoutError");
426
+ }).pipe(Effect.provide(ProcessRunner.layer.pipe(Layer.provideMerge(NodeServices.layer)))),
427
+ );
428
+
429
+ for (const wrapperExit of ["timeout", "success"] as const) {
430
+ it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
431
+ `terminates descendants after wrapper ${wrapperExit}`,
432
+ () =>
433
+ Effect.gen(function* () {
434
+ const fs = yield* FileSystem.FileSystem;
435
+ const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-process-tree-" });
436
+ const probe = yield* makeProcessProbe;
437
+ const helper = writeFakeCli({
438
+ directory,
439
+ name: "wrapper",
440
+ source: `
441
+ import { spawn } from "node:child_process";
442
+ process.on("SIGTERM", () => {});
443
+ const child = spawn(process.execPath, ["--input-type=module", "-e", ${JSON.stringify(`
444
+ ${processProbeSource(probe.port)}
445
+ probe.once("connect", () => process.send("ready"));
446
+ `)}], { stdio: ["ignore", "ignore", "ignore", "ipc"] });
447
+ ${wrapperExit === "success" ? 'child.once("message", () => process.exit(0));' : 'child.on("exit", () => process.exit(0));'}
448
+ `,
449
+ });
450
+ const runner = yield* ProcessRunner.ProcessRunner;
451
+ const fiber = yield* runner
452
+ .run({ command: helper, args: [], timeout: "1 second" })
453
+ .pipe(Effect.result, Effect.forkScoped);
454
+ yield* probe.started;
455
+
456
+ if (wrapperExit === "timeout") yield* TestClock.adjust("1 second");
457
+ yield* probe.exited;
458
+ const result = yield* Fiber.join(fiber);
459
+ if (wrapperExit === "timeout") {
460
+ expect(result).toMatchObject({
461
+ _tag: "Failure",
462
+ failure: { _tag: "ProcessTimeoutError" },
463
+ });
464
+ } else {
465
+ expect(result).toMatchObject({
466
+ _tag: "Success",
467
+ success: { code: 0, timedOut: false },
468
+ });
469
+ }
470
+ }).pipe(Effect.provide(ProcessRunner.layer.pipe(Layer.provideMerge(NodeServices.layer)))),
471
+ );
472
+ }
473
+
474
+ it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
475
+ "exits the backend and terminates stubborn helpers on graceful group shutdown",
476
+ () =>
477
+ Effect.gen(function* () {
478
+ const fs = yield* FileSystem.FileSystem;
479
+ const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-process-backend-" });
480
+ const probe = yield* makeProcessProbe;
481
+ const helper = writeFakeCli({
482
+ directory,
483
+ name: "helper",
484
+ source: `process.on("SIGTERM", () => {}); ${processProbeSource(probe.port)}`,
485
+ });
486
+ const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
487
+ const path = yield* Path.Path;
488
+ const backend = yield* spawner.spawn(
489
+ ChildProcess.make(
490
+ process.execPath,
491
+ [
492
+ "--experimental-strip-types",
493
+ "--input-type=module",
494
+ "-e",
495
+ `
496
+ import * as Effect from "effect/Effect";
497
+ import * as Layer from "effect/Layer";
498
+ import * as NodeServices from "@effect/platform-node/NodeServices";
499
+ import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
500
+ import * as ProcessRunner from ${JSON.stringify(new URL("./processRunner.ts", import.meta.url).href)};
501
+ NodeRuntime.runMain(Effect.gen(function* () {
502
+ const runner = yield* ProcessRunner.ProcessRunner;
503
+ yield* runner.run({ command: ${JSON.stringify(helper)}, args: [] });
504
+ }).pipe(Effect.provide(ProcessRunner.layer.pipe(Layer.provideMerge(NodeServices.layer)))));
505
+ `,
506
+ ],
507
+ {
508
+ detached: true,
509
+ forceKillAfter: "1 second",
510
+ cwd: yield* path.fromFileUrl(new URL("..", import.meta.url)),
511
+ },
512
+ ),
513
+ );
514
+ yield* probe.started;
515
+
516
+ // Desktop allows five seconds for NodeRuntime to finish scope finalizers.
517
+ process.kill(-backend.pid, "SIGTERM");
518
+ yield* Effect.all(
519
+ [backend.exitCode.pipe(Effect.timeout("4 seconds"), TestClock.withLive), probe.exited],
520
+ { concurrency: "unbounded" },
521
+ );
522
+ }).pipe(Effect.provide(NodeServices.layer)),
523
+ );
524
+
525
it.effect("returns a synthetic timed out result when timeoutBehavior is timedOutResult", () =>
526
Effect.gen(function* () {
527
const spawner = makeSpawner(() =>
apps/server/src/processRunner.ts
+3
@@ -146,6 +146,8 @@ export class ProcessRunner extends Context.Service<
146
>()("t3/processRunner") {}
147
148
const DEFAULT_TIMEOUT = "60 seconds";
149
+// Keep SIGTERM-ignoring helpers from blocking timeouts or graceful shutdown.
150
+const FORCE_KILL_AFTER = "1 second";
151
const DEFAULT_MAX_OUTPUT_BYTES = 8 * 1024 * 1024;
152
153
export { isWindowsCommandNotFound } from "@t3tools/provider-core/server/snapshotProbe";
@@ -304,6 +306,7 @@ const runProcessCore = Effect.fn("processRunner.runProcessCore")(function* (
306
}
307
: {}),
308
shell: spawnCommand.shell,
309
+ forceKillAfter: FORCE_KILL_AFTER,
310
}),
311
)
312
.pipe(
apps/server/src/testUtils/processProbe.ts
new
+73
@@ -0,0 +1,73 @@
1
+// @effect-diagnostics nodeBuiltinImport:off - a socket observes fixture processes after their parent exits.
2
+import * as NodeNet from "node:net";
3
+import * as Deferred from "effect/Deferred";
4
+import * as Effect from "effect/Effect";
5
+import * as TestClock from "effect/testing/TestClock";
6
+
7
+export const makeProcessProbe = Effect.gen(function* () {
8
+ const started = yield* Deferred.make<number>();
9
+ const exited = yield* Deferred.make<void>();
10
+ const sockets = new Set<NodeNet.Socket>();
11
+ const server = NodeNet.createServer((socket) => {
12
+ sockets.add(socket);
13
+ let output = "";
14
+ socket.on("data", (chunk) => {
15
+ output += chunk.toString();
16
+ if (output.includes("\n")) {
17
+ const pid = Number(output.trim());
18
+ if (Number.isSafeInteger(pid) && pid > 0) {
19
+ Deferred.doneUnsafe(started, Effect.succeed(pid));
20
+ }
21
+ }
22
+ });
23
+ socket.on("close", () => {
24
+ sockets.delete(socket);
25
+ Deferred.doneUnsafe(exited, Effect.void);
26
+ });
27
+ });
28
+ yield* Effect.acquireRelease(
29
+ Effect.callback<void, Error>((resume) => {
30
+ server.once("error", (error) => resume(Effect.fail(error)));
31
+ server.listen(0, "127.0.0.1", () => resume(Effect.void));
32
+ }),
33
+ () =>
34
+ Effect.sync(() => {
35
+ for (const socket of sockets) socket.destroy();
36
+ server.close();
37
+ }),
38
+ );
39
+ const address = server.address();
40
+ if (address === null || typeof address === "string") {
41
+ return yield* Effect.die("Missing process probe port");
42
+ }
43
+ return {
44
+ port: address.port,
45
+ started: Deferred.await(started).pipe(
46
+ Effect.timeout("4 seconds"),
47
+ TestClock.withLive,
48
+ Effect.tap((pid) =>
49
+ Effect.addFinalizer(() =>
50
+ Effect.gen(function* () {
51
+ if (yield* Deferred.isDone(exited)) return;
52
+ // Only a PID reported by this fixture is eligible for failure cleanup.
53
+ yield* Effect.sync(() => {
54
+ try {
55
+ process.kill(pid, "SIGKILL");
56
+ } catch (error) {
57
+ if ((error as NodeJS.ErrnoException).code !== "ESRCH") throw error;
58
+ }
59
+ });
60
+ }),
61
+ ),
62
+ ),
63
+ ),
64
+ exited: Deferred.await(exited).pipe(Effect.timeout("4 seconds"), TestClock.withLive),
65
+ };
66
+});
67
+
68
+export const processProbeSource = (port: number) => `
69
+ const { createConnection } = await import("node:net");
70
+ const probe = createConnection(${port}, "127.0.0.1", () => {
71
+ probe.write(process.pid + "\\n");
72
+ });
73
+`;
apps/server/src/textGeneration/ClaudeTextGeneration.test.ts
+35
-3
@@ -4,13 +4,16 @@ import { ClaudeSettings, ProviderInstanceId } from "@t3tools/contracts";
4
import * as HostProcess from "@t3tools/shared/HostProcess";
5
import { createModelSelection } from "@t3tools/shared/model";
6
import * as Effect from "effect/Effect";
7
+import * as Fiber from "effect/Fiber";
8
import * as FileSystem from "effect/FileSystem";
9
import * as Layer from "effect/Layer";
10
import * as Path from "effect/Path";
11
import * as Schema from "effect/Schema";
12
+import * as TestClock from "effect/testing/TestClock";
13
import { expect } from "vite-plus/test";
14
15
import * as ServerConfig from "../config.ts";
16
+import { makeProcessProbe, processProbeSource } from "../testUtils/processProbe.ts";
17
import {
18
SYNTHETIC_CLAUDE_CAPABLE_MODEL,
19
SYNTHETIC_CLAUDE_COLLIDING_ALIAS,
@@ -31,7 +34,7 @@ const layerClaudeTextGenerationTest = ServerConfig.ServerConfig.layerTest(proces
34
// The stub behaviour lives in Node so the same implementation runs on Windows,
35
// where a shebang file is not executable and would fall through to the real
36
// Claude CLI on PATH; `writeFakeCli` picks the launcher shape per host.
34
-function makeFakeClaudeBinary(dir: string) {
37
+function makeFakeClaudeBinary(dir: string, lifecyclePort?: number) {
38
return Effect.gen(function* () {
39
const path = yield* Path.Path;
40
const fs = yield* FileSystem.FileSystem;
@@ -45,7 +48,10 @@ function makeFakeClaudeBinary(dir: string) {
48
directory: binDir,
49
name: "claude",
50
platform,
48
- source,
51
+ source:
52
+ lifecyclePort === undefined
53
+ ? source
54
+ : `process.on("SIGTERM", () => {}); ${processProbeSource(lifecyclePort)}`,
55
});
56
return binDir;
57
});
@@ -54,6 +60,7 @@ function makeFakeClaudeBinary(dir: string) {
60
function withFakeClaudeEnv<A, E, R>(
61
input: {
62
output: string;
63
+ lifecyclePort?: number;
64
exitCode?: number;
65
stderr?: string;
66
argsMustContain?: string;
@@ -68,7 +75,7 @@ function withFakeClaudeEnv<A, E, R>(
75
return Effect.gen(function* () {
76
const fs = yield* FileSystem.FileSystem;
77
const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-claude-text-" });
71
- const binDir = yield* makeFakeClaudeBinary(tempDir);
78
+ const binDir = yield* makeFakeClaudeBinary(tempDir, input.lifecyclePort);
79
const pathDelimiter = (yield* HostProcess.isWindows) ? ";" : ":";
80
const previousPath = process.env.PATH;
81
const previousOutput = process.env.T3_FAKE_CLAUDE_OUTPUT;
@@ -192,6 +199,31 @@ function withFakeClaudeEnv<A, E, R>(
199
}
200
201
it.layer(layerClaudeTextGenerationTest)("ClaudeTextGeneration", (it) => {
202
+ it.effect("kills a stalled CLI when the request times out", () =>
203
+ Effect.gen(function* () {
204
+ const probe = yield* makeProcessProbe;
205
+ yield* withFakeClaudeEnv({ output: "", lifecyclePort: probe.port }, (textGeneration) =>
206
+ Effect.gen(function* () {
207
+ const fiber = yield* textGeneration
208
+ .generateThreadTitle({
209
+ cwd: process.cwd(),
210
+ message: "Name this thread",
211
+ modelSelection: {
212
+ instanceId: ProviderInstanceId.make("claudeAgent"),
213
+ model: SYNTHETIC_CLAUDE_STANDARD_MODEL,
214
+ },
215
+ })
216
+ .pipe(Effect.flip, Effect.forkScoped);
217
+ yield* probe.started;
218
+
219
+ yield* TestClock.adjust("180 seconds");
220
+ yield* probe.exited;
221
+ expect((yield* Fiber.join(fiber)).detail).toBe("Claude CLI request timed out.");
222
+ }),
223
+ );
224
+ }),
225
+ );
226
+
227
it.effect("forwards Claude thinking settings without passing unsupported effort", () =>
228
withFakeClaudeEnv(
229
{
apps/server/src/textGeneration/ClaudeTextGeneration.ts
+1
@@ -234,6 +234,7 @@ export const makeClaudeTextGeneration = Effect.fn("makeClaudeTextGeneration")(fu
234
stdin: {
235
stream: Stream.encodeText(Stream.make(prompt)),
236
},
237
+ forceKillAfter: "1 second",
238
});
239
240
const child = yield* commandSpawner
apps/server/src/textGeneration/CodexTextGeneration.test.ts
+43
-1
@@ -1,7 +1,9 @@
1
import * as NodeServices from "@effect/platform-node/NodeServices";
2
import { it } from "@effect/vitest";
3
+import * as Cause from "effect/Cause";
4
import * as Effect from "effect/Effect";
5
import * as Exit from "effect/Exit";
6
+import * as Fiber from "effect/Fiber";
7
import * as FileSystem from "effect/FileSystem";
8
import * as Layer from "effect/Layer";
9
import * as Path from "effect/Path";
@@ -14,6 +16,7 @@ import { expect } from "vite-plus/test";
16
import { CodexSettings, ProviderInstanceId, TextGenerationError } from "@t3tools/contracts";
17
18
import * as ServerConfig from "../config.ts";
19
+import { makeProcessProbe, processProbeSource } from "../testUtils/processProbe.ts";
20
import * as TextGeneration from "./TextGeneration.ts";
21
import { makeCodexTextGeneration } from "./CodexTextGeneration.ts";
22
import { writeFakeCli } from "@t3tools/provider-testing/fakeCli";
@@ -30,6 +33,7 @@ const layerCodexTextGenerationTest = ServerConfig.ServerConfig.layerTest(process
33
34
interface FakeCodexInput {
35
output: string;
36
+ lifecycle?: { port: number; command: "exec" | "mcp list" };
37
exitCode?: number;
38
stderr?: string;
39
requireImage?: boolean;
@@ -67,7 +71,17 @@ function makeFakeCodexBinary(dir: string, input: FakeCodexInput) {
71
'import * as NodeFS from "node:fs";',
72
`const check = ${check};`,
73
"const args = process.argv.slice(2);",
70
- 'if (args.includes("mcp") && args.includes("list")) {',
74
+ 'const isMcpList = args.includes("mcp") && args.includes("list");',
75
+ ...(input.lifecycle === undefined
76
+ ? []
77
+ : [
78
+ `if (isMcpList === ${input.lifecycle.command === "mcp list"}) {`,
79
+ 'process.on("SIGTERM", () => {});',
80
+ processProbeSource(input.lifecycle.port),
81
+ "await new Promise(() => {});",
82
+ "}",
83
+ ]),
84
+ "if (isMcpList) {",
85
' if (typeof check.mcpServers === "number") process.exit(check.mcpServers);',
86
" process.stdout.write(JSON.stringify(check.mcpServers));",
87
" process.exit(0);",
@@ -173,6 +187,34 @@ function withFakeCodexEnv<A, E, R>(
187
}
188
189
it.layer(layerCodexTextGenerationTest)("CodexTextGeneration", (it) => {
190
+ it.effect.each(["exec", "mcp list"] as const)(
191
+ "kills a stalled %s CLI when its request is interrupted",
192
+ (command) =>
193
+ Effect.gen(function* () {
194
+ const probe = yield* makeProcessProbe;
195
+ yield* withFakeCodexEnv(
196
+ { output: "", lifecycle: { port: probe.port, command } },
197
+ (textGeneration) =>
198
+ Effect.gen(function* () {
199
+ const fiber = yield* textGeneration
200
+ .generateThreadTitle({
201
+ cwd: process.cwd(),
202
+ message: "Name this thread",
203
+ modelSelection: DEFAULT_TEST_MODEL_SELECTION,
204
+ })
205
+ .pipe(Effect.forkScoped);
206
+ yield* probe.started;
207
+
208
+ const interrupted = yield* Fiber.interrupt(fiber).pipe(Effect.forkScoped);
209
+ yield* probe.exited;
210
+ yield* Fiber.join(interrupted);
211
+ const exit = yield* Fiber.await(fiber);
212
+ expect(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)).toBe(true);
213
+ }),
214
+ );
215
+ }),
216
+ );
217
+
218
it.effect.each(["gpt-5.6-luna", "openai.gpt-5.6-luna"])(
219
"dispatches the qualified live model for %s",
220
(selectedModel) =>
apps/server/src/textGeneration/CodexTextGeneration.ts
+2
@@ -178,6 +178,7 @@ export const makeCodexTextGeneration = Effect.fn("makeCodexTextGeneration")(func
178
env: input.env,
179
cwd: input.cwd,
180
shell: spawnCommand.shell,
181
+ forceKillAfter: "1 second",
182
}),
183
);
184
const [stdout, , exitCode] = yield* Effect.all(
@@ -319,6 +320,7 @@ export const makeCodexTextGeneration = Effect.fn("makeCodexTextGeneration")(func
320
stdin: {
321
stream: Stream.encodeText(Stream.make(prompt)),
322
},
323
+ forceKillAfter: "1 second",
324
});
325
326
const child = yield* commandSpawner