diff --git a/packages/effect-codex-app-server/src/protocol.test.ts b/packages/effect-codex-app-server/src/protocol.test.ts index 7249afff10713..8b20eacd0deba 100644 --- a/packages/effect-codex-app-server/src/protocol.test.ts +++ b/packages/effect-codex-app-server/src/protocol.test.ts @@ -804,4 +804,43 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => { assert.equal("cause" in error, false); }), ); + + it.effect("fails pending requests at input EOF while the process is still running", () => + Effect.gen(function* () { + const { stdio, input, output } = yield* makeInMemoryStdio(); + const transport = yield* CodexProtocol.makeCodexAppServerPatchedProtocol({ + stdio, + terminationError: Effect.never, + }); + const pending = yield* transport.request("thread/read", {}).pipe(Effect.forkScoped); + yield* Queue.take(output); + + yield* Queue.end(input); + + const error = yield* Fiber.join(pending).pipe( + Effect.match({ + onFailure: (error) => error, + onSuccess: () => assert.fail("Expected the pending request to fail"), + }), + ); + assert.instanceOf(error, CodexError.CodexAppServerInputStreamEndedError); + }), + ); + + it.effect("reports an already known process exit at input EOF", () => + Effect.gen(function* () { + const { stdio, input } = yield* makeInMemoryStdio(); + const exitError = new CodexError.CodexAppServerProcessExitedError({ code: 1, pid: 123 }); + const termination = yield* Deferred.make(); + yield* CodexProtocol.makeCodexAppServerPatchedProtocol({ + stdio, + terminationError: Effect.succeed(exitError), + onTermination: (error) => Deferred.succeed(termination, error).pipe(Effect.asVoid), + }); + + yield* Queue.end(input); + + assert.strictEqual(yield* Deferred.await(termination), exitError); + }), + ); }); diff --git a/packages/effect-codex-app-server/src/protocol.ts b/packages/effect-codex-app-server/src/protocol.ts index 4a32973a988bd..eb18e4f577636 100644 --- a/packages/effect-codex-app-server/src/protocol.ts +++ b/packages/effect-codex-app-server/src/protocol.ts @@ -434,10 +434,14 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa Effect.matchEffect({ onFailure: (error) => handleTermination(() => Effect.succeed(error)), onSuccess: () => - handleTermination( - () => - options.terminationError ?? + // stdout can close while the process is still alive, so waiting + // for the exit status here would hang every pending request. An + // exit status that is already known still wins the race. + handleTermination(() => + Effect.raceFirst( + options.terminationError ?? Effect.never, Effect.succeed(new CodexError.CodexAppServerInputStreamEndedError({})), + ), ), }), ),