From 92aa0ecb053492c5b7b0aca318fd0bf18869d85a Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 15 Nov 2026 02:55:06 +0000 Subject: [PATCH] fix(codex): an app-server that exits before initialize reports its exit, not EPIPE Writing to a Codex app-server that already exited fails with EPIPE. Since #31192 that writer failure ends the transport, and it could beat the stdout EOF path that waits for the exit status, so the request failed with a bare write-output-stream transport error instead of the process exit and its stderr. The writer failure now waits briefly for the exit status too, the same way input end does. Co-Authored-By: Claude Opus 5.5 --- .../src/protocol.test.ts | 32 +++++++++++ .../effect-codex-app-server/src/protocol.ts | 53 ++++++++++--------- 2 files changed, 61 insertions(+), 24 deletions(-) diff --git a/packages/effect-codex-app-server/src/protocol.test.ts b/packages/effect-codex-app-server/src/protocol.test.ts index c7503d7979829..7d2118939fbb1 100644 --- a/packages/effect-codex-app-server/src/protocol.test.ts +++ b/packages/effect-codex-app-server/src/protocol.test.ts @@ -992,6 +992,38 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => { }), ); + it.effect("reports the process exit when a write fails because the process exited", () => + Effect.gen(function* () { + const { stdio: baseStdio } = yield* makeInMemoryStdio(); + const exitError = new CodexError.CodexAppServerProcessExitedError({ code: 1, pid: 123 }); + const transport = yield* CodexProtocol.makeCodexAppServerPatchedProtocol({ + stdio: Stdio.make({ + ...baseStdio, + stdout: () => + Sink.forEach(() => + Effect.fail( + PlatformError.systemError({ + _tag: "Unknown", + module: "Stdio", + method: "stdout", + cause: new Error("EPIPE"), + }), + ), + ), + }), + terminationError: Effect.succeed(exitError), + }); + + const failure = yield* transport.request("initialize", {}).pipe( + Effect.match({ + onFailure: (error) => error, + onSuccess: () => assert.fail("Expected the exited process to fail the request"), + }), + ); + assert.strictEqual(failure, exitError); + }), + ); + it.effect("reports a process exit that follows input end", () => Effect.gen(function* () { const { stdio, input } = yield* makeInMemoryStdio(); diff --git a/packages/effect-codex-app-server/src/protocol.ts b/packages/effect-codex-app-server/src/protocol.ts index cdfc0c6a00977..0faf9e44e4b44 100644 --- a/packages/effect-codex-app-server/src/protocol.ts +++ b/packages/effect-codex-app-server/src/protocol.ts @@ -229,6 +229,26 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa ] as const; }).pipe(Effect.flatten); + // Node ends stdout before it reports the exit of a crashed process, so wait briefly + // for the exit status. Pipes can also close while the process keeps running, so the + // wait must not hang pending requests. + const exitStatusOr = ( + fallback: CodexError.CodexAppServerError, + operation: "read-input-stream" | "write-output-stream", + ) => + options.terminationError === undefined + ? Effect.succeed(fallback) + : options.terminationError.pipe( + Effect.timeoutOrElse({ + duration: EXIT_STATUS_GRACE, + orElse: () => Effect.succeed(fallback), + }), + // A defect here would leave termination marked handled but unfinished. + Effect.catchDefect((cause) => + Effect.succeed(new CodexError.CodexAppServerTransportError({ operation, cause })), + ), + ); + const offerOutgoing = /* Every protocol write reaches this helper; request spans cover the operation. */ Effect.fnUntraced(function* (message: Record) { @@ -474,29 +494,12 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa Effect.matchEffect({ onFailure: (error) => handleTermination(() => Effect.succeed(error)), onSuccess: () => - handleTermination(() => { - const ended = new CodexError.CodexAppServerInputStreamEndedError({}); - // Node ends stdout before it reports the exit of a crashed process, so - // wait briefly for the exit status. stdout can also close while the - // process keeps running, so the wait must not hang pending requests. - return options.terminationError === undefined - ? Effect.succeed(ended) - : options.terminationError.pipe( - Effect.timeoutOrElse({ - duration: EXIT_STATUS_GRACE, - orElse: () => Effect.succeed(ended), - }), - // A defect here would leave termination marked handled but unfinished. - Effect.catchDefect((cause) => - Effect.succeed( - new CodexError.CodexAppServerTransportError({ - operation: "read-input-stream", - cause, - }), - ), - ), - ); - }), + handleTermination(() => + exitStatusOr( + new CodexError.CodexAppServerInputStreamEndedError({}), + "read-input-stream", + ), + ), }), ), }), @@ -517,11 +520,13 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa Stream.run(options.stdio.stdout()), Effect.catchCause((cause) => handleTermination(() => - Effect.succeed( + // Writing to a process that already exited fails with EPIPE; report the exit. + exitStatusOr( new CodexError.CodexAppServerTransportError({ operation: "write-output-stream", cause: Cause.squash(cause), }), + "write-output-stream", ), ), ),