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/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); 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* () { 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);