@devries-cameron20 / t3code-1 / commits / 7769d709ce
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(codex): bound pending requests after app-server stdout closes (#24600)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 24, 2026 at 17:17 UTC 7769d709ce1983df0224c5f0e666b81ec61cac9f
2 files changed +61 -5
packages/effect-codex-app-server/src/protocol.test.ts
+46
@@ -5,6 +5,7 @@ import * as Queue from "effect/Queue";
5 import * as Schema from "effect/Schema";
6 import * as Stdio from "effect/Stdio";
7 import * as Stream from "effect/Stream";
8 +import * as TestClock from "effect/testing/TestClock";
9
10 import * as NodeServices from "@effect/platform-node/NodeServices";
11 import { assert, it } from "@effect/vitest";
@@ -644,4 +645,49 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => {
645 assert.notInclude(error.message, secret);
646 }),
647 );
648 +
649 + it.effect("fails pending requests when input ends while the process is still running", () =>
650 + Effect.gen(function* () {
651 + const { stdio, input, output } = yield* makeInMemoryStdio();
652 + const transport = yield* CodexProtocol.makeCodexAppServerPatchedProtocol({
653 + stdio,
654 + terminationError: Effect.never,
655 + });
656 + const pending = yield* transport.request("thread/read", {}).pipe(Effect.forkScoped);
657 + yield* Queue.take(output);
658 +
659 + yield* Queue.end(input);
660 + yield* TestClock.adjust("249 millis");
661 + assert.isUndefined(pending.pollUnsafe());
662 + yield* TestClock.adjust("1 millis");
663 +
664 + const failure = yield* Fiber.join(pending).pipe(
665 + Effect.match({
666 + onFailure: (error) => error,
667 + onSuccess: () => assert.fail("Expected the pending request to fail"),
668 + }),
669 + );
670 + assert.instanceOf(failure, CodexError.CodexAppServerInputStreamEndedError);
671 + }),
672 + );
673 +
674 + it.effect("reports a process exit that follows input end", () =>
675 + Effect.gen(function* () {
676 + const { stdio, input } = yield* makeInMemoryStdio();
677 + const exitError = new CodexError.CodexAppServerProcessExitedError({ code: 1, pid: 123 });
678 + const exited = yield* Deferred.make<CodexError.CodexAppServerError>();
679 + const termination = yield* Deferred.make<CodexError.CodexAppServerError>();
680 + yield* CodexProtocol.makeCodexAppServerPatchedProtocol({
681 + stdio,
682 + terminationError: Deferred.await(exited),
683 + onTermination: (error) => Deferred.succeed(termination, error).pipe(Effect.asVoid),
684 + });
685 +
686 + yield* Queue.end(input);
687 + yield* TestClock.adjust("100 millis");
688 + yield* Deferred.succeed(exited, exitError);
689 +
690 + assert.strictEqual(yield* Deferred.await(termination), exitError);
691 + }),
692 + );
693 });
packages/effect-codex-app-server/src/protocol.ts
+15 -5
@@ -18,6 +18,7 @@ const isCodexAppServerError = Schema.is(CodexError.CodexAppServerError);
18 const MAX_BUFFERED_RAW_MESSAGES = 32;
19 // Bound JSONL lines before joining and parsing; the LF delimiter is excluded.
20 const MAX_INCOMING_MESSAGE_BYTES = 128 * 1024 * 1024;
21 +const EXIT_STATUS_GRACE = "250 millis";
22
23 export interface CodexAppServerProtocolLogEvent {
24 readonly direction: "incoming" | "outgoing";
@@ -445,11 +446,20 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
446 Effect.matchEffect({
447 onFailure: (error) => handleTermination(() => Effect.succeed(error)),
448 onSuccess: () =>
448 - handleTermination(
449 - () =>
450 - options.terminationError ??
451 - Effect.succeed(new CodexError.CodexAppServerInputStreamEndedError({})),
452 - ),
449 + handleTermination(() => {
450 + const ended = new CodexError.CodexAppServerInputStreamEndedError({});
451 + // Node ends stdout before it reports the exit of a crashed process, so
452 + // wait briefly for the exit status. stdout can also close while the
453 + // process keeps running, so the wait must not hang pending requests.
454 + return options.terminationError === undefined
455 + ? Effect.succeed(ended)
456 + : options.terminationError.pipe(
457 + Effect.timeoutOrElse({
458 + duration: EXIT_STATUS_GRACE,
459 + orElse: () => Effect.succeed(ended),
460 + }),
461 + );
462 + }),
463 }),
464 ),
465 }),