Skip to content

Commit 10ca568

Browse files
committed
fix(server): await Claude process exit before retrying startup
1 parent 034db3e commit 10ca568

2 files changed

Lines changed: 122 additions & 12 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -961,22 +961,45 @@ export const layerQueryRunner: Layer.Layer<
961961
const unit = launch?.unit;
962962
const stopScope =
963963
unit === undefined ? undefined : () => void runFork(agentScope.stop(unit));
964-
// Windows has no process groups, so there the SDK spawns the CLI itself.
965-
const spawnClaudeCodeProcess =
964+
const spawnProcess =
966965
options.spawnClaudeCodeProcess ??
967-
(platform === "win32"
968-
? undefined
969-
: (spawnOptions: ClaudeSpawnOptions) =>
970-
spawnClaudeProcessGroup(spawnOptions, stopScope));
966+
((spawnOptions: ClaudeSpawnOptions) => {
967+
if (platform !== "win32") return spawnClaudeProcessGroup(spawnOptions, stopScope);
968+
const child = NodeChildProcess.spawn(spawnOptions.command, spawnOptions.args, {
969+
cwd: spawnOptions.cwd,
970+
env: spawnOptions.env,
971+
signal: spawnOptions.signal,
972+
windowsHide: true,
973+
stdio: ["pipe", "pipe", "pipe"],
974+
});
975+
child.stderr
976+
.setEncoding("utf8")
977+
.on("data", (chunk: string) => options.stderr?.(chunk));
978+
return child;
979+
});
980+
let spawnedProcess: ClaudeSpawnedProcess | undefined;
981+
let processExited = false;
982+
let processExit: Promise<void> | undefined;
983+
const spawnClaudeCodeProcess = (spawnOptions: ClaudeSpawnOptions) => {
984+
const child = spawnProcess(spawnOptions);
985+
spawnedProcess = child;
986+
processExited = child.exitCode !== null || child.signalCode != null;
987+
processExit = new Promise<void>((resolve) => {
988+
if (processExited) resolve();
989+
else
990+
child.once("exit", () => {
991+
processExited = true;
992+
resolve();
993+
});
994+
});
995+
return child;
996+
};
971997
const queryRuntime = yield* Effect.uninterruptibleMask((restore) =>
972998
Effect.try({
973999
try: () =>
9741000
query({
9751001
prompt,
976-
options:
977-
spawnClaudeCodeProcess === undefined
978-
? options
979-
: { ...options, spawnClaudeCodeProcess },
1002+
options: { ...options, spawnClaudeCodeProcess },
9801003
}),
9811004
catch: (cause) => queryRunnerError(cause, "query"),
9821005
}).pipe(
@@ -1030,13 +1053,25 @@ export const layerQueryRunner: Layer.Layer<
10301053
).pipe(
10311054
Effect.onError(() =>
10321055
Queue.shutdown(promptQueue).pipe(
1056+
// SDK disposal can return before process exit; await the owned child's exit before retrying.
10331057
Effect.andThen(
1058+
Effect.tryPromise({
1059+
try: async () => {
1060+
if (spawnedProcess !== undefined && !processExited) {
1061+
spawnedProcess.kill("SIGKILL");
1062+
}
1063+
await processExit;
1064+
},
1065+
catch: (cause) => queryRunnerError(cause, "close"),
1066+
}).pipe(Effect.timeout("10 seconds")),
1067+
),
1068+
Effect.ensuring(
10341069
Effect.tryPromise({
10351070
try: () => queryRuntime[Symbol.asyncDispose](),
10361071
catch: (cause) => queryRunnerError(cause, "close"),
1037-
}),
1072+
}).pipe(Effect.orDie),
10381073
),
1039-
Effect.ignore,
1074+
Effect.orDie,
10401075
),
10411076
),
10421077
),

‎apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
// @effect-diagnostics nodeBuiltinImport:off -- the fake CLI and its leftover child are real processes.
22
import * as ClaudeSdk from "@anthropic-ai/claude-agent-sdk";
33
import * as NodeChildProcess from "node:child_process";
4+
import * as NodeEvents from "node:events";
45
import * as NodeFS from "node:fs";
56
import * as NodeOS from "node:os";
67
import * as NodePath from "node:path";
8+
import * as NodeStream from "node:stream";
79
import * as NodeServices from "@effect/platform-node/NodeServices";
810
import { assert, describe, it } from "@effect/vitest";
911
import { ProviderSessionId, ThreadId } from "@t3tools/contracts";
1012
import * as Effect from "effect/Effect";
13+
import * as Exit from "effect/Exit";
1114
import * as Deferred from "effect/Deferred";
1215
import * as Fiber from "effect/Fiber";
1316
import * as FileSystem from "effect/FileSystem";
@@ -199,6 +202,78 @@ it.effect("reopens a stalled MCP registration before delivering the resumed prom
199202
),
200203
);
201204

205+
it.effect.each([true, false])("only retries after confirmed process exit: %s", (exits) =>
206+
Effect.scoped(
207+
Effect.gen(function* () {
208+
const registrationStarted = Promise.withResolvers<void>();
209+
const cleanupProgress = Promise.withResolvers<"kill" | "retry">();
210+
const events = new NodeEvents.EventEmitter();
211+
const stdin = new NodeStream.PassThrough();
212+
const stdout = new NodeStream.PassThrough();
213+
const child = {
214+
stdin,
215+
stdout,
216+
killed: false,
217+
exitCode: null,
218+
kill: vi.fn(() => {
219+
cleanupProgress.resolve("kill");
220+
return true;
221+
}),
222+
on: events.on.bind(events),
223+
once: events.once.bind(events),
224+
off: events.off.bind(events),
225+
} satisfies ClaudeSdk.SpawnedProcess;
226+
const signal = yield* Effect.abortSignal;
227+
const disposed = vi.fn(async () => {});
228+
const querySpy = vi.spyOn(ClaudeSdk, "query").mockImplementation(({ options }) => {
229+
const first = querySpy.mock.calls.length === 1;
230+
if (first) {
231+
options?.spawnClaudeCodeProcess?.({ command: "claude", args: [], env: {}, signal });
232+
} else {
233+
cleanupProgress.resolve("retry");
234+
}
235+
return {
236+
setMcpServers: () => {
237+
registrationStarted.resolve();
238+
return first
239+
? new Promise<ClaudeSdk.McpSetServersResult>(() => {})
240+
: Promise.resolve({ added: ["t3-code"], removed: [], errors: {} });
241+
},
242+
close: vi.fn(),
243+
[Symbol.asyncDispose]: disposed,
244+
} as unknown as ClaudeSdk.Query;
245+
});
246+
yield* Effect.addFinalizer(() =>
247+
Effect.sync(() => {
248+
querySpy.mockRestore();
249+
stdin.destroy();
250+
stdout.destroy();
251+
}),
252+
);
253+
const runner = yield* ClaudeAgentSdkQueryRunner;
254+
const opening = yield* runner
255+
.open({
256+
...resumedMcpQueryInput,
257+
options: { ...resumedMcpQueryInput.options, spawnClaudeCodeProcess: () => child },
258+
})
259+
.pipe(Effect.exit, Effect.forkChild);
260+
yield* Effect.promise(() => registrationStarted.promise);
261+
yield* TestClock.adjust("90 seconds");
262+
263+
assert.equal(yield* Effect.promise(() => cleanupProgress.promise), "kill");
264+
assert.equal(querySpy.mock.calls.length, 1);
265+
assert.deepEqual(child.kill.mock.calls, [["SIGKILL"]]);
266+
if (exits) events.emit("exit", null, "SIGKILL");
267+
else yield* TestClock.adjust("10 seconds");
268+
const result = yield* Fiber.join(opening);
269+
assert.equal(Exit.isSuccess(result), exits);
270+
if (Exit.isSuccess(result)) yield* result.value.close;
271+
assert.equal(querySpy.mock.calls.length, exits ? 2 : 1);
272+
assert.equal(disposed.mock.calls.length, 1);
273+
}).pipe(Effect.provide(realQueryRunner)),
274+
),
275+
);
276+
202277
it.effect("stops retrying MCP registration after two timeouts and reports the cause", () =>
203278
Effect.scoped(
204279
Effect.gen(function* () {

0 commit comments

Comments
 (0)