Skip to content

Commit 7769d70

Browse files
fix(codex): bound pending requests after app-server stdout closes (pingdotgg#24600)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 249b6ae commit 7769d70

2 files changed

Lines changed: 61 additions & 5 deletions

File tree

‎packages/effect-codex-app-server/src/protocol.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as Queue from "effect/Queue";
55
import * as Schema from "effect/Schema";
66
import * as Stdio from "effect/Stdio";
77
import * as Stream from "effect/Stream";
8+
import * as TestClock from "effect/testing/TestClock";
89

910
import * as NodeServices from "@effect/platform-node/NodeServices";
1011
import { assert, it } from "@effect/vitest";
@@ -644,4 +645,49 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => {
644645
assert.notInclude(error.message, secret);
645646
}),
646647
);
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+
);
647693
});

‎packages/effect-codex-app-server/src/protocol.ts‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const isCodexAppServerError = Schema.is(CodexError.CodexAppServerError);
1818
const MAX_BUFFERED_RAW_MESSAGES = 32;
1919
// Bound JSONL lines before joining and parsing; the LF delimiter is excluded.
2020
const MAX_INCOMING_MESSAGE_BYTES = 128 * 1024 * 1024;
21+
const EXIT_STATUS_GRACE = "250 millis";
2122

2223
export interface CodexAppServerProtocolLogEvent {
2324
readonly direction: "incoming" | "outgoing";
@@ -445,11 +446,20 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
445446
Effect.matchEffect({
446447
onFailure: (error) => handleTermination(() => Effect.succeed(error)),
447448
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+
}),
453463
}),
454464
),
455465
}),

0 commit comments

Comments
 (0)