t3code icon indicating copy to clipboard operation
t3code copied to clipboard

[Bug]: A crashed Codex app-server usually reports a bare transport error instead of its exit code and stderr

Open Andrey170170 opened this issue 3 weeks ago • 1 comments

Before submitting

  • [x] I searched existing issues and did not find a duplicate.
  • [x] I included enough detail to reproduce or investigate the problem.

Area

apps/server

Steps to reproduce

The defect is in packages/effect-codex-app-server, which apps/server uses for every Codex session. This script spawns true as the app-server (it exits at once, like a crashed launcher) through CodexClient.layerChildProcess 200 times, and counts which error awaitTermination reports:

// Run with Node 24+ from a directory whose node_modules resolves effect and @effect/platform-node.
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Layer from "effect/Layer";
import * as Scope from "effect/Scope";
import { ChildProcess, ChildProcessSpawner } from "effect/process";
import * as NodeServices from "@effect/platform-node/NodeServices";
import * as CodexClient from "<repo>/packages/effect-codex-app-server/src/client.ts";

const withRequest = process.argv[2] === "request";
const once = Effect.gen(function* () {
  const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
  const scope = yield* Scope.make();
  const handle = yield* spawner.spawn(ChildProcess.make("true", ["app-server"])).pipe(Scope.provide(scope));
  const context = yield* Layer.buildWithScope(CodexClient.layerChildProcess(handle), scope);
  const client = yield* CodexClient.CodexAppServerClient.pipe(Effect.provide(context));
  if (withRequest) {
    yield* client
      .request("initialize", {
        clientInfo: { name: "repro", title: "repro", version: "0" },
        capabilities: { experimentalApi: true, optOutNotificationMethods: null },
      })
      .pipe(Effect.ignore);
  }
  const error: any = yield* client.awaitTermination.pipe(Effect.flip);
  yield* Scope.close(scope, Exit.void);
  return `${error._tag}${error.operation ? `:${error.operation}` : ""}`;
});

Effect.runPromise(
  Effect.gen(function* () {
    const counts: Record<string, number> = {};
    for (let i = 0; i < 200; i++) {
      const key = yield* once;
      counts[key] = (counts[key] ?? 0) + 1;
    }
    console.log(counts);
  }).pipe(Effect.provide(NodeServices.layer)),
);
  1. Run it as node repro.ts request (a request is in flight when the process dies).
  2. Run it as node repro.ts (nothing written yet).

Expected behavior

Every run reports CodexAppServerProcessExitedError, with the exit code and the stderr excerpt that #23938 added (for example "Missing optional dependency @openai/codex-…. Reinstall Codex …").

Actual behavior

Most runs report a bare CodexAppServerTransportError from the write side, so the exit code and stderr never reach the thread or the provider card:

node repro.ts request
{ 'CodexAppServerTransportError:write-output-stream': 198, CodexAppServerProcessExitedError: 2 }

node repro.ts
{ 'CodexAppServerTransportError:write-output-stream': 86, CodexAppServerProcessExitedError: 114 }

The causes are EPIPE when a write was pending, and ERR_STREAM_PREMATURE_CLOSE otherwise: the stdin sink fails as soon as the child closes its end, even with nothing to write.

Cause: makeCodexAppServerPatchedProtocol sees the death on two paths, and whichever reaches handleTermination first decides the error. When stdout ends, it waits up to EXIT_STATUS_GRACE (250 ms) for the exit status (protocol.ts#L464). When the stdin write fails, it reports a transport error at once (protocol.ts#L488), so it usually wins. That write-failure path came from #27924. Its point, failing pending requests when a write breaks while the process lives, still needs to hold.

Impact

Minor bug or occasional failure

Version or commit

main @ 9e85538535 (v0.1.8-nightly.20261104.4179)

Environment

Debian 13 (Linux 6.12), Node 26.12.1. Not platform-specific: it is a race between two Node stream events.

Logs or stack traces

AssertionError: expected 'CodexAppServerTransportError' to equal 'CodexAppServerProcessExitedError'

This is from a test in our fork that spawns true through the Codex adapter's client factory. It fails only when the test suite runs under load.

Workaround

None in the UI; the stderr excerpt is lost when the transport error wins.

Andrey170170 avatar Nov 04 '26 16:11 Andrey170170

This is the same problem as #37029, so I'm closing it in favour of that one. If it is different, say how and it will be reopened.

juliusmarminge avatar Nov 19 '26 21:11 juliusmarminge