Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions packages/effect-codex-app-server/src/protocol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CodexError.CodexAppServerError>();
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);
}),
);
});
10 changes: 7 additions & 3 deletions packages/effect-codex-app-server/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({})),
),
),
}),
),
Expand Down
Loading