Skip to content

Commit 536774d

Browse files
axisrowclaude
andcommitted
fix(broker): make the EBROKERTIMEOUT direct-fallback reachable (cycle-review critical)
Codex adversarial review caught that the handshake timeout rejects inside CodexAppServerClient.connect() — before `client` is assigned in withAppServer — so the `client?.transport === "broker"` guard was always false for EBROKERTIMEOUT. The fallback clause was dead code: a wedged broker made every withAppServer-backed command throw after 5s instead of retrying directly. Tag initialization errors with the chosen transport in connect(), and let the fallback match on error.transport when client is still null. Adds an end-to-end test (wedged broker → withAppServer → direct fallback via fake-codex). Co-Authored-By: Claude <[email protected]>
1 parent 1832f1b commit 536774d

3 files changed

Lines changed: 57 additions & 3 deletions

File tree

‎plugins/codex/scripts/lib/app-server.mjs‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,18 @@ export class CodexAppServerClient {
393393
const client = brokerEndpoint
394394
? new BrokerCodexAppServerClient(cwd, { ...options, brokerEndpoint })
395395
: new SpawnedCodexAppServerClient(cwd, options);
396-
await client.initialize();
396+
try {
397+
await client.initialize();
398+
} catch (error) {
399+
// The handshake rejects before `connect()` returns, so callers like
400+
// withAppServer cannot read `client.transport` (client is never assigned
401+
// there). Tag the error with the transport so the caller can still tell
402+
// a broker-handshake failure (worth a direct fallback) from a spawned one.
403+
if (error && !error.transport) {
404+
error.transport = client.transport;
405+
}
406+
throw error;
407+
}
397408
return client;
398409
}
399410
}

‎plugins/codex/scripts/lib/codex.mjs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ async function captureTurn(client, threadId, startRequest, options = {}) {
679679
}
680680
}
681681

682-
async function withAppServer(cwd, fn, clientOptions = {}) {
682+
export async function withAppServer(cwd, fn, clientOptions = {}) {
683683
let client = null;
684684
try {
685685
client = await CodexAppServerClient.connect(cwd, clientOptions);
@@ -688,9 +688,11 @@ async function withAppServer(cwd, fn, clientOptions = {}) {
688688
return result;
689689
} catch (error) {
690690
const brokerRequested = client?.transport === "broker" || Boolean(process.env[BROKER_ENDPOINT_ENV]);
691+
const brokerHandshakeFailed =
692+
error?.code === BROKER_HANDSHAKE_TIMEOUT_CODE && (client?.transport === "broker" || error?.transport === "broker");
691693
const shouldRetryDirect =
692694
(client?.transport === "broker" && error?.rpcCode === BROKER_BUSY_RPC_CODE) ||
693-
(client?.transport === "broker" && error?.code === BROKER_HANDSHAKE_TIMEOUT_CODE) ||
695+
brokerHandshakeFailed ||
694696
(brokerRequested && (error?.code === "ENOENT" || error?.code === "ECONNREFUSED"));
695697

696698
if (client) {

‎tests/broker-client-initialize-timeout.test.mjs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import test from "node:test";
22
import assert from "node:assert/strict";
33

44
import { CodexAppServerClient } from "../plugins/codex/scripts/lib/app-server.mjs";
5+
import { withAppServer } from "../plugins/codex/scripts/lib/codex.mjs";
6+
import { buildEnv, installFakeCodex } from "./fake-codex-fixture.mjs";
57
import { makeTempDir } from "./helpers.mjs";
68
import { spawnWedgedServer } from "./broker-controller-helpers.mjs";
79

@@ -41,3 +43,42 @@ test("client does not hang on initialize when broker is wedged (#29/#509)", asyn
4143
`expected EBROKERTIMEOUT from wedged broker, got ${JSON.stringify(outcome)}`
4244
);
4345
});
46+
47+
// Drives the full withAppServer fallback path: a wedged broker makes the
48+
// broker-client handshake time out, and withAppServer must fall back to a
49+
// direct (spawned) app-server instead of propagating the timeout. Codex review
50+
// of PR #30 caught that the EBROKERTIMEOUT fallback was unreachable because the
51+
// handshake rejects before `client` is assigned, so client.transport is null.
52+
test("withAppServer falls back to a direct app-server when the broker wedges (#29/#509)", async () => {
53+
const wedged = await spawnWedgedServer();
54+
const cwd = makeTempDir();
55+
const binDir = makeTempDir();
56+
installFakeCodex(binDir);
57+
const env = buildEnv(binDir);
58+
59+
let calls = 0;
60+
try {
61+
const result = await Promise.race([
62+
withAppServer(
63+
cwd,
64+
async (client) => {
65+
calls += 1;
66+
return { transport: client.transport };
67+
},
68+
{
69+
brokerEndpoint: `unix:${wedged.socketPath}`,
70+
brokerConnectTimeoutMs: 300,
71+
brokerInitializeTimeoutMs: 500,
72+
env
73+
}
74+
),
75+
new Promise((resolve) => setTimeout(() => resolve({ HANG: true }), 5000))
76+
]);
77+
78+
assert.ok(!result.HANG, "withAppServer hung instead of falling back to direct");
79+
assert.equal(calls, 1, "the operation must run exactly once via the direct fallback");
80+
assert.equal(result.transport, "direct", "the operation must run on the direct transport after fallback");
81+
} finally {
82+
wedged.cleanup();
83+
}
84+
});

0 commit comments

Comments
 (0)