Skip to content

Commit 34cfd24

Browse files
trevoedwardsclaude
andauthored
fix(codex): a failed write to the app-server no longer leaves requests hanging (#27924)
Co-authored-by: Trevor Edwards <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent dabab45 commit 34cfd24

3 files changed

Lines changed: 54 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ export type CodexAppServerProtocolParseOperation = typeof CodexAppServerProtocol
109109

110110
export const CodexAppServerTransportOperation = Schema.Literals([
111111
"read-input-stream",
112+
"write-output-stream",
112113
"read-process-exit-status",
113114
]);
114115
export type CodexAppServerTransportOperation = typeof CodexAppServerTransportOperation.Type;

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import * as Deferred from "effect/Deferred";
22
import * as Effect from "effect/Effect";
33
import * as Fiber from "effect/Fiber";
4+
import * as PlatformError from "effect/PlatformError";
45
import * as Queue from "effect/Queue";
56
import * as Schema from "effect/Schema";
7+
import * as Sink from "effect/Sink";
68
import * as Stdio from "effect/Stdio";
79
import * as Stream from "effect/Stream";
810
import * as TestClock from "effect/testing/TestClock";
@@ -740,6 +742,43 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => {
740742
}),
741743
);
742744

745+
it.effect("fails pending requests when a write fails while input stays open", () =>
746+
Effect.gen(function* () {
747+
const { stdio: baseStdio } = yield* makeInMemoryStdio();
748+
const writeFailure = PlatformError.systemError({
749+
_tag: "Unknown",
750+
module: "Stdio",
751+
method: "stdout",
752+
cause: new Error("EPIPE"),
753+
});
754+
const terminated = yield* Deferred.make<CodexError.CodexAppServerError>();
755+
const transport = yield* CodexProtocol.makeCodexAppServerPatchedProtocol({
756+
stdio: Stdio.make({
757+
args: baseStdio.args,
758+
stdin: baseStdio.stdin,
759+
stdout: () => Sink.forEach(() => Effect.fail(writeFailure)),
760+
stderr: baseStdio.stderr,
761+
}),
762+
onTermination: (error) => Deferred.succeed(terminated, error).pipe(Effect.asVoid),
763+
});
764+
const pending = yield* transport.request("thread/read", {}).pipe(Effect.forkScoped);
765+
766+
const failure = yield* Deferred.await(terminated);
767+
assert.instanceOf(failure, CodexError.CodexAppServerTransportError);
768+
assert.equal(failure.operation, "write-output-stream");
769+
assert.strictEqual(failure.cause, writeFailure);
770+
for (const request of [Fiber.join(pending), transport.request("thread/read", {})]) {
771+
const requestFailure = yield* request.pipe(
772+
Effect.match({
773+
onFailure: (error) => error,
774+
onSuccess: () => assert.fail("Expected the broken transport to reject the request"),
775+
}),
776+
);
777+
assert.strictEqual(requestFailure, failure);
778+
}
779+
}),
780+
);
781+
743782
it.effect("reports a process exit that follows input end", () =>
744783
Effect.gen(function* () {
745784
const { stdio, input } = yield* makeInMemoryStdio();

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,20 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
479479
Effect.forkScoped,
480480
);
481481

482-
yield* Stream.fromQueue(outgoing).pipe(Stream.run(options.stdio.stdout()), Effect.forkScoped);
482+
yield* Stream.fromQueue(outgoing).pipe(
483+
Stream.run(options.stdio.stdout()),
484+
Effect.catch((cause) =>
485+
handleTermination(() =>
486+
Effect.succeed(
487+
new CodexError.CodexAppServerTransportError({
488+
operation: "write-output-stream",
489+
cause,
490+
}),
491+
),
492+
),
493+
),
494+
Effect.forkScoped,
495+
);
483496

484497
const request = Effect.fn("CodexAppServerProtocol.request")(function* (
485498
method: string,

0 commit comments

Comments
 (0)