The best way to control Claude Code, Codex and more from any computer or phone
fix(server): a Claude agent stops when the server that started it dies (#23389)
Co-authored-by: PR Batch Tester <agent@local.test> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
shivam committed
Oct 22, 2026 at 13:46 UTC
5ffe04593166870d3ada2a83eb5fe55f0d192fc7
2 files changed
+64
-10
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+24
-9
@@ -314,26 +314,41 @@ const WORKFLOW_MEMBER_TRANSCRIPT_FLUSH_MS = 500;
314
// subagents to move to the background before it is sent.
315
const BACKGROUND_MOVE_TIMEOUT_MS = 5_000;
316
317
+const CLAUDE_GROUP_WATCH_SCRIPT = [
318
+ "{ trap '' TERM; read -r _ <&3; kill -TERM 0; sleep 5; kill -KILL 0; } </dev/null >/dev/null 2>&1 &",
319
+ 'exec "$@" 3<&-',
320
+].join("\n");
321
+
322
/**
323
* When the CLI exits it stops the MCP servers it started, but not their children: a plugin's
324
* `bun run start` wrapper dies and its `bun server.ts` keeps running, writing to a pipe nobody
325
* reads (#3089). Those stay in the CLI's group, so the rest of the group gets SIGKILL once the
326
* CLI exits for any reason.
327
+ *
328
+ * That cleanup needs this server alive, and the CLI finishes its turn after its pipes close. A
329
+ * server that crashes or is killed would leave the CLI working next to the session it resumes
330
+ * (#23370), so a watcher in the group reads a pipe only this server holds and stops the group at
331
+ * its end-of-file.
332
*/
323
-function spawnClaudeProcessGroup(options: ClaudeSpawnOptions): ClaudeSpawnedProcess {
324
- const child = NodeChildProcess.spawn(options.command, options.args, {
325
- cwd: options.cwd,
326
- env: options.env,
327
- signal: options.signal,
328
- stdio: ["pipe", "pipe", "ignore"],
329
- detached: true,
330
- });
333
+export function spawnClaudeProcessGroup(options: ClaudeSpawnOptions): ClaudeSpawnedProcess {
334
+ const child = NodeChildProcess.spawn(
335
+ "/bin/sh",
336
+ ["-c", CLAUDE_GROUP_WATCH_SCRIPT, "t3-claude", options.command, ...options.args],
337
+ {
338
+ cwd: options.cwd,
339
+ env: options.env,
340
+ signal: options.signal,
341
+ stdio: ["pipe", "pipe", "ignore", "pipe"],
342
+ detached: true,
343
+ },
344
+ );
345
child.once("exit", () => {
346
try {
347
signalProcessGroup(child.pid ?? 0, "SIGKILL");
348
} catch {}
349
});
336
- return child;
350
+ // Node's typings make stdin and stdout nullable once stdio has a fourth entry; both are pipes.
351
+ return child as ClaudeSpawnedProcess;
352
}
353
354
export const ClaudeProviderCapabilitiesV2 = {
apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts
+40
-1
@@ -15,7 +15,12 @@ import { vi } from "vite-plus/test";
15
16
import * as HostProcess from "@t3tools/shared/HostProcess";
17
import { ProviderEventLoggers } from "@t3tools/provider-core/server/ProviderEventLoggers";
18
-import { ClaudeAgentSdkQueryRunner, layerQueryRunner } from "./ClaudeAdapterV2.ts";
18
+import { signalProcessGroup } from "@t3tools/provider-core/server/processGroup";
19
+import {
20
+ ClaudeAgentSdkQueryRunner,
21
+ layerQueryRunner,
22
+ spawnClaudeProcessGroup,
23
+} from "./ClaudeAdapterV2.ts";
24
25
vi.mock("@anthropic-ai/claude-agent-sdk", { spy: true });
26
@@ -163,4 +168,38 @@ describe.skipIf(HostProcess.Platform.defaultValue() === "win32")("Claude CLI pro
168
yield* Effect.promise(() => leftoverStopped);
169
}).pipe(Effect.provide(realQueryRunner)),
170
);
171
+
172
+ // The CLI keeps running its turn after its pipes close, so a server that
173
+ // crashed would leave it working next to the session it resumes (#23370).
174
+ it.effect("stops the CLI when the server that started it goes away", () =>
175
+ Effect.gen(function* () {
176
+ const cli = fakeClaudeCli(['dir=$(dirname "$0")', 'sleep 600 > "$dir/alive"'].join("\n"));
177
+ const dir = NodePath.dirname(cli);
178
+ NodeChildProcess.execFileSync("mkfifo", [NodePath.join(dir, "alive")]);
179
+ const child = spawnClaudeProcessGroup({
180
+ command: cli,
181
+ args: [],
182
+ cwd: dir,
183
+ env: process.env,
184
+ signal: new AbortController().signal,
185
+ }) as NodeChildProcess.ChildProcess;
186
+ yield* Effect.addFinalizer(() =>
187
+ Effect.sync(() => {
188
+ try {
189
+ signalProcessGroup(child.pid ?? 0, "SIGKILL");
190
+ } catch {}
191
+ NodeFS.rmSync(dir, { recursive: true, force: true });
192
+ }),
193
+ );
194
+ const alive = NodeFS.createReadStream(NodePath.join(dir, "alive"));
195
+ const turnStarted = new Promise<void>((resolve) => alive.once("open", () => resolve()));
196
+ const turnStopped = new Promise<void>((resolve) => alive.once("close", () => resolve()));
197
+ alive.resume();
198
+ yield* Effect.promise(() => turnStarted);
199
+
200
+ // A dying server's descriptors close; the kernel would do this for it.
201
+ for (const pipe of child.stdio) pipe?.destroy();
202
+ yield* Effect.promise(() => turnStopped);
203
+ }),
204
+ );
205
});