From f26b55dfbb93fe24a1765b4f775f587aba1ec8c3 Mon Sep 17 00:00:00 2001 From: Hannes Lenz Date: Sun, 20 Sep 2026 21:40:11 +0200 Subject: [PATCH 1/2] fix(codex): fail requests when stdout ends before process exit --- .../src/protocol.test.ts | 58 +++++++++++++++++++ .../effect-codex-app-server/src/protocol.ts | 7 ++- 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/packages/effect-codex-app-server/src/protocol.test.ts b/packages/effect-codex-app-server/src/protocol.test.ts index 7249afff10713..355d7fcacd2db 100644 --- a/packages/effect-codex-app-server/src/protocol.test.ts +++ b/packages/effect-codex-app-server/src/protocol.test.ts @@ -804,4 +804,62 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => { assert.equal("cause" in error, false); }), ); + + it.effect("does not wait for process exit before terminating after input EOF", () => + Effect.gen(function* () { + const { stdio, input, output } = yield* makeInMemoryStdio(); + const processExit = yield* Deferred.make(); + const terminated = yield* Deferred.make(); + const transport = yield* CodexProtocol.makeCodexAppServerPatchedProtocol({ + stdio, + terminationError: Deferred.await(processExit), + onTermination: (error) => Deferred.succeed(terminated, error).pipe(Effect.asVoid), + }); + const pending = yield* transport.request("thread/read", {}).pipe(Effect.forkScoped); + yield* Queue.take(output); + + yield* Queue.end(input); + + const failure = yield* Deferred.await(terminated); + assert.instanceOf(failure, CodexError.CodexAppServerInputStreamEndedError); + + const pendingFailure = yield* Fiber.join(pending).pipe( + Effect.match({ + onFailure: (error) => error, + onSuccess: () => assert.fail("Expected the pending request to fail"), + }), + ); + assert.strictEqual(pendingFailure, failure); + + const futureFailure = yield* transport.request("thread/read", {}).pipe( + Effect.match({ + onFailure: (error) => error, + onSuccess: () => assert.fail("Expected requests after EOF to fail"), + }), + ); + assert.strictEqual(futureFailure, failure); + }), + ); + + it.effect("retains an already available process exit error at input EOF", () => + Effect.gen(function* () { + const { stdio, input } = yield* makeInMemoryStdio(); + const processExitError = new CodexError.CodexAppServerProcessExitedError({ + code: 1, + pid: 123, + }); + const processExit = yield* Deferred.make(); + yield* Deferred.succeed(processExit, processExitError); + const terminated = yield* Deferred.make(); + yield* CodexProtocol.makeCodexAppServerPatchedProtocol({ + stdio, + terminationError: Deferred.await(processExit), + onTermination: (error) => Deferred.succeed(terminated, error).pipe(Effect.asVoid), + }); + + yield* Queue.end(input); + + assert.strictEqual(yield* Deferred.await(terminated), processExitError); + }), + ); }); diff --git a/packages/effect-codex-app-server/src/protocol.ts b/packages/effect-codex-app-server/src/protocol.ts index 4a32973a988bd..162cadf64968f 100644 --- a/packages/effect-codex-app-server/src/protocol.ts +++ b/packages/effect-codex-app-server/src/protocol.ts @@ -434,10 +434,11 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa Effect.matchEffect({ onFailure: (error) => handleTermination(() => Effect.succeed(error)), onSuccess: () => - handleTermination( - () => - options.terminationError ?? + handleTermination(() => + Effect.raceFirst( + options.terminationError ?? Effect.never, Effect.succeed(new CodexError.CodexAppServerInputStreamEndedError({})), + ), ), }), ), From bc0819570b5e822d5907b8009ee31ae9f249de45 Mon Sep 17 00:00:00 2001 From: Julius Marminge <51714798+juliusmarminge@users.noreply.github.com> Date: Sun, 18 Oct 2026 16:36:03 -0700 Subject: [PATCH 2/2] fix(codex): retain focused EOF regressions and clarify termination race --- .../src/protocol.test.ts | 39 +++++-------------- .../effect-codex-app-server/src/protocol.ts | 3 ++ 2 files changed, 13 insertions(+), 29 deletions(-) diff --git a/packages/effect-codex-app-server/src/protocol.test.ts b/packages/effect-codex-app-server/src/protocol.test.ts index 355d7fcacd2db..8b20eacd0deba 100644 --- a/packages/effect-codex-app-server/src/protocol.test.ts +++ b/packages/effect-codex-app-server/src/protocol.test.ts @@ -805,61 +805,42 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => { }), ); - it.effect("does not wait for process exit before terminating after input EOF", () => + it.effect("fails pending requests at input EOF while the process is still running", () => Effect.gen(function* () { const { stdio, input, output } = yield* makeInMemoryStdio(); - const processExit = yield* Deferred.make(); - const terminated = yield* Deferred.make(); const transport = yield* CodexProtocol.makeCodexAppServerPatchedProtocol({ stdio, - terminationError: Deferred.await(processExit), - onTermination: (error) => Deferred.succeed(terminated, error).pipe(Effect.asVoid), + terminationError: Effect.never, }); const pending = yield* transport.request("thread/read", {}).pipe(Effect.forkScoped); yield* Queue.take(output); yield* Queue.end(input); - const failure = yield* Deferred.await(terminated); - assert.instanceOf(failure, CodexError.CodexAppServerInputStreamEndedError); - - const pendingFailure = yield* Fiber.join(pending).pipe( + const error = yield* Fiber.join(pending).pipe( Effect.match({ onFailure: (error) => error, onSuccess: () => assert.fail("Expected the pending request to fail"), }), ); - assert.strictEqual(pendingFailure, failure); - - const futureFailure = yield* transport.request("thread/read", {}).pipe( - Effect.match({ - onFailure: (error) => error, - onSuccess: () => assert.fail("Expected requests after EOF to fail"), - }), - ); - assert.strictEqual(futureFailure, failure); + assert.instanceOf(error, CodexError.CodexAppServerInputStreamEndedError); }), ); - it.effect("retains an already available process exit error at input EOF", () => + it.effect("reports an already known process exit at input EOF", () => Effect.gen(function* () { const { stdio, input } = yield* makeInMemoryStdio(); - const processExitError = new CodexError.CodexAppServerProcessExitedError({ - code: 1, - pid: 123, - }); - const processExit = yield* Deferred.make(); - yield* Deferred.succeed(processExit, processExitError); - const terminated = yield* Deferred.make(); + const exitError = new CodexError.CodexAppServerProcessExitedError({ code: 1, pid: 123 }); + const termination = yield* Deferred.make(); yield* CodexProtocol.makeCodexAppServerPatchedProtocol({ stdio, - terminationError: Deferred.await(processExit), - onTermination: (error) => Deferred.succeed(terminated, error).pipe(Effect.asVoid), + terminationError: Effect.succeed(exitError), + onTermination: (error) => Deferred.succeed(termination, error).pipe(Effect.asVoid), }); yield* Queue.end(input); - assert.strictEqual(yield* Deferred.await(terminated), processExitError); + 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 162cadf64968f..eb18e4f577636 100644 --- a/packages/effect-codex-app-server/src/protocol.ts +++ b/packages/effect-codex-app-server/src/protocol.ts @@ -434,6 +434,9 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa Effect.matchEffect({ onFailure: (error) => handleTermination(() => Effect.succeed(error)), onSuccess: () => + // 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,