Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ function executeCli(binary: string, command: CliCommand): Promise<CliResult> {
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;
Expand All @@ -86,7 +89,6 @@ function executeCli(binary: string, command: CliCommand): Promise<CliResult> {
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"));
Expand Down
10 changes: 6 additions & 4 deletions apps/server/src/environmentControl/namespaceInstances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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<unknown> = [];
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<typeof NodeChildProcess>("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* () {
Expand Down
5 changes: 4 additions & 1 deletion apps/server/src/provider/ClaudeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
5 changes: 3 additions & 2 deletions apps/server/src/serviceLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>((resolve, reject) => {
const onError = (error: Error) => reject(error);
child.once("error", onError);
Expand Down
Loading