From 9389d8ee286441245546a2f3574f5ca563197970 Mon Sep 17 00:00:00 2001 From: andrew Date: Sat, 21 Nov 2026 22:41:36 -0500 Subject: [PATCH 1/3] test(server): failed Claude CLI spawn must not emit an unhandled 'error' Co-Authored-By: Claude Opus 5.5 --- .../ClaudeAgentSdkQueryRunner.test.ts | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts index 2bf2763c58c6..62d9e79f85a0 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts @@ -36,6 +36,7 @@ import { } from "./ClaudeAdapterV2.ts"; vi.mock("@anthropic-ai/claude-agent-sdk", { spy: true }); +vi.mock("node:child_process", { spy: true }); const FORK_SESSION_ID = "11111111-1111-4111-8111-111111111111"; @@ -953,6 +954,68 @@ describe.skipIf(HostProcess.Platform.defaultValue() === "win32")("Claude CLI pro }), ); + // Node emits a failed spawn's 'error' on a later tick; with no listener that kills the server. + const uncaughtAfterNextTick = Effect.fnUntraced(function* (run: () => unknown) { + const uncaught: Array = []; + const record = (error: unknown) => uncaught.push(error); + process.on("uncaughtException", record); + yield* Effect.addFinalizer(() => Effect.sync(() => process.off("uncaughtException", record))); + run(); + yield* Effect.promise(() => new Promise((resolve) => process.nextTick(resolve))); + return uncaught; + }); + + it.effect("a spawn that fails throws its error code and leaves no unhandled 'error' event", () => + Effect.gen(function* () { + const { spawn } = yield* Effect.promise(() => + vi.importActual("node:child_process"), + ); + // ENFILE and EMFILE fail the same way: no pid, no stdio, an 'error' event on the next tick. + const spawnMock = vi + .spyOn(NodeChildProcess, "spawn") + .mockImplementationOnce((_command, _args, options) => + spawn("/nonexistent/t3-shell", [], options as NodeChildProcess.SpawnOptions), + ); + yield* Effect.addFinalizer(() => Effect.sync(() => spawnMock.mockRestore())); + let thrown: unknown; + const uncaught = yield* uncaughtAfterNextTick(() => { + try { + spawnClaudeProcessGroup({ + command: "claude", + args: [], + env: process.env, + signal: new AbortController().signal, + }); + } catch (error) { + thrown = error; + } + }); + assert.deepEqual(uncaught, []); + assert.instanceOf(thrown, Error); + assert.equal((thrown as NodeJS.ErrnoException).code, "ENOENT"); + }), + ); + + it.effect("an already-aborted signal leaves no unhandled 'error' event", () => + Effect.gen(function* () { + const cli = fakeClaudeCli("exit 0"); + const dir = NodePath.dirname(cli); + yield* Effect.addFinalizer(() => + Effect.sync(() => NodeFS.rmSync(dir, { recursive: true, force: true })), + ); + const uncaught = yield* uncaughtAfterNextTick(() => + spawnClaudeProcessGroup({ + command: cli, + args: [], + cwd: dir, + env: process.env, + signal: AbortSignal.abort(), + }), + ); + assert.deepEqual(uncaught, []); + }), + ); + // A native cwd makes libuv wait for chdir, which a hung mount stalls with the whole server. it.effect("enters the cwd in the child, so a missing one fails the CLI and not the spawn", () => Effect.gen(function* () { From 4aad27c920543371c65e7a69594250f3bdff9286 Mon Sep 17 00:00:00 2001 From: andrew Date: Sat, 21 Nov 2026 22:43:42 -0500 Subject: [PATCH 2/3] fix(server): a failed Claude CLI spawn no longer crashes the server spawnClaudeProcessGroup attached no 'error' listener and read child.stdio[4] on a child that never started, so ENFILE/EMFILE threw a TypeError and then Node emitted an unhandled 'error' that killed the process. Listen first and throw a clear error when the spawn has no pid. Co-Authored-By: Claude Opus 5.5 --- .../src/orchestration-v2/Adapters/ClaudeAdapterV2.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index b7acf70c5f42..937fbf78c18c 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -431,6 +431,18 @@ export function spawnClaudeProcessGroup( detached: true, }, ); + // Node emits a failed spawn's 'error' on the next tick, and with no listener that kills the server. + let spawnFailure: Error | undefined; + child.on("error", (error: NodeJS.ErrnoException) => { + if (spawnFailure !== undefined) Object.assign(spawnFailure, { code: error.code, cause: error }); + }); + if (child.pid === undefined) { + // EMFILE and ENFILE leave no stdio either. The errno only arrives with the event above. + spawnFailure = new Error( + "Failed to start the Claude CLI: the operating system refused to spawn it.", + ); + throw spawnFailure; + } let forget: (() => void) | undefined; // Always drained, so a chatty CLI never blocks on a full stderr pipe. child.stderr?.setEncoding("utf8").on("data", onStderr).once("close", onStderrClose); From a227d2e3bee1e6e320c283e626f8c415a0ea47c9 Mon Sep 17 00:00:00 2001 From: andrew Date: Sat, 21 Nov 2026 22:46:56 -0500 Subject: [PATCH 3/3] fix(server): attach spawn error listeners before touching a child's stdio Same fault as the Claude CLI spawn: with EMFILE/ENFILE a child has no stdio, so code that reads it before listening throws and then the child's 'error' goes unhandled. Covers the Claude probe, the Namespace CLI runners and the service launcher's runtime spawn. Co-Authored-By: Claude Opus 5.5 --- .../environmentControl/NamespaceProvisionRuntime.ts | 4 +++- .../src/environmentControl/namespaceInstances.ts | 10 ++++++---- apps/server/src/provider/ClaudeProvider.ts | 5 ++++- apps/server/src/serviceLauncher.ts | 5 +++-- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/apps/server/src/environmentControl/NamespaceProvisionRuntime.ts b/apps/server/src/environmentControl/NamespaceProvisionRuntime.ts index 6b8737e6119e..9da575a18d6c 100644 --- a/apps/server/src/environmentControl/NamespaceProvisionRuntime.ts +++ b/apps/server/src/environmentControl/NamespaceProvisionRuntime.ts @@ -68,6 +68,9 @@ function executeCli(binary: string, command: CliCommand): Promise { env: command.env, stdio: ["ignore", "pipe", "pipe"], }); + child.once("error", reject); + // A failed spawn has no stdio; its error event rejects this call. + if (child.pid === undefined) return; let aborted = false; const abort = () => { if (aborted || child.exitCode !== null || child.signalCode !== null) return; @@ -86,7 +89,6 @@ function executeCli(binary: string, command: CliCommand): Promise { child.stderr.setEncoding("utf8").on("data", (data: string) => { stderr += data; }); - child.once("error", reject); child.once("close", (code) => { command.signal?.removeEventListener("abort", abort); if (aborted) reject(new Error("Namespace CLI command aborted or timed out")); diff --git a/apps/server/src/environmentControl/namespaceInstances.ts b/apps/server/src/environmentControl/namespaceInstances.ts index 5650fa3bc174..7bbe07c1573b 100644 --- a/apps/server/src/environmentControl/namespaceInstances.ts +++ b/apps/server/src/environmentControl/namespaceInstances.ts @@ -71,6 +71,12 @@ export function spawnNsc( child.stderr.destroy(); }; deadline?.addEventListener("abort", expire, { once: true }); + child.once("error", (error) => { + deadline?.removeEventListener("abort", expire); + reject(error); + }); + // A failed spawn has no stdio; its error event rejects this call. + if (child.pid === undefined) return; let stdout = ""; let stderr = ""; child.stdout.setEncoding("utf8").on("data", (data: string) => { @@ -79,10 +85,6 @@ export function spawnNsc( child.stderr.setEncoding("utf8").on("data", (data: string) => { stderr += data; }); - child.once("error", (error) => { - deadline?.removeEventListener("abort", expire); - reject(error); - }); child.once("close", (code) => { deadline?.removeEventListener("abort", expire); if (timedOut) diff --git a/apps/server/src/provider/ClaudeProvider.ts b/apps/server/src/provider/ClaudeProvider.ts index 8605aabeece7..bae4c17a3a4a 100644 --- a/apps/server/src/provider/ClaudeProvider.ts +++ b/apps/server/src/provider/ClaudeProvider.ts @@ -252,13 +252,16 @@ export function spawnClaudeProbeThroughEnv({ signal, }: ClaudeSpawnOptions) { const launch = envTrampoline(command, args, cwd) ?? { command, args, cwd }; - return NodeChildProcess.spawn(launch.command, launch.args, { + const child = NodeChildProcess.spawn(launch.command, launch.args, { ...("cwd" in launch ? { cwd: launch.cwd } : {}), env, signal, // The probe ignores stderr. stdio: ["pipe", "pipe", "ignore"], }); + // A failed spawn has no stdin, so the SDK throws before it listens; its 'error' must not go unhandled. + child.on("error", () => {}); + return child; } /** Windows runs CreateProcess on the calling thread, so the probe launches from a launcher thread. */ diff --git a/apps/server/src/serviceLauncher.ts b/apps/server/src/serviceLauncher.ts index 8dcd7886fce5..fb669686b4fa 100644 --- a/apps/server/src/serviceLauncher.ts +++ b/apps/server/src/serviceLauncher.ts @@ -1224,8 +1224,9 @@ export class Launcher { ? ["inherit", appendStdout ? "pipe" : "inherit", appendStderr ? "pipe" : "inherit", "ipc"] : ["ignore", this.#output, this.#output, "ipc"], }); - if (child.stdout !== null) appendOutput(child.stdout, 1); - if (child.stderr !== null) appendOutput(child.stderr, 2); + // A failed spawn leaves these undefined and reports through its 'error' event below. + if (child.stdout) appendOutput(child.stdout, 1); + if (child.stderr) appendOutput(child.stderr, 2); await new Promise((resolve, reject) => { const onError = (error: Error) => reject(error); child.once("error", onError);