@devries-cameron20 / t3code-1 / commits / f3b43efd9a
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): a hung Codex app-server no longer blocks every later Codex run (#26555)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Mahir Abbas committed Nov 14, 2026 at 10:17 UTC f3b43efd9a4eae018d966b24210d4b66450a4b5d
2 files changed +113 -4
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
+84
@@ -1146,6 +1146,7 @@ describe("CodexAdapterV2 session initialize", () => {
1146 runtimePolicy: CODEX_TEST_RUNTIME_POLICY,
1147 }),
1148 initializeRequests: () => initializeRequests,
1149 + events: runtime.events,
1150 };
1151 });
1152
@@ -1293,6 +1294,89 @@ describe("CodexAdapterV2 session initialize", () => {
1294 ),
1295 );
1296
1297 + it.effect("fails the open when initialize never answers during open", () =>
1298 + Effect.gen(function* () {
1299 + const initializeAwaitingResponse = yield* Deferred.make<void>();
1300 + const opened = yield* openReplaySession(
1301 + makeCodexReplayTranscript({
1302 + scenario: "initialize-hung-open",
1303 + entries: replayPreamble("initialize-hung-open").slice(0, 2),
1304 + }),
1305 + (entry) =>
1306 + entry.label === "initialize"
1307 + ? Deferred.succeed(initializeAwaitingResponse, undefined).pipe(
1308 + Effect.andThen(Effect.never),
1309 + )
1310 + : Effect.void,
1311 + () => Effect.die("initialize never answers"),
1312 + ).pipe(Effect.flip, Effect.forkChild({ startImmediately: true }));
1313 + yield* Deferred.await(initializeAwaitingResponse);
1314 + yield* TestClock.adjust("30 seconds");
1315 +
1316 + // The session manager discards a session whose open fails.
1317 + assert.instanceOf(yield* Fiber.join(opened), ProviderAdapter.ProviderAdapterOpenSessionError);
1318 + }).pipe(
1319 + Effect.scoped,
1320 + Effect.provide(
1321 + Layer.mergeAll(
1322 + IdAllocator.layer,
1323 + McpProviderSessions.layer,
1324 + NodeServices.layer,
1325 + NodeCrypto.layer,
1326 + ),
1327 + ),
1328 + ),
1329 + );
1330 +
1331 + it.effect("retires the shared session when initialize never answers", () =>
1332 + Effect.gen(function* () {
1333 + const initializeAwaitingResponse = yield* Deferred.make<void>();
1334 + const session = yield* openReplaySession(
1335 + makeCodexReplayTranscript({
1336 + scenario: "initialize-hung",
1337 + entries: replayPreamble("initialize-hung").slice(0, 2),
1338 + }),
1339 + (entry) =>
1340 + entry.label === "initialize"
1341 + ? Deferred.succeed(initializeAwaitingResponse, undefined).pipe(
1342 + Effect.andThen(Effect.never),
1343 + )
1344 + : Effect.void,
1345 + );
1346 + const eventsExit = yield* Stream.runDrain(session.events).pipe(
1347 + Effect.exit,
1348 + Effect.forkChild({ startImmediately: true }),
1349 + );
1350 +
1351 + const ensured = yield* session
1352 + .ensureThread("thread-initialize-hung")
1353 + .pipe(Effect.flip, Effect.forkChild({ startImmediately: true }));
1354 + yield* Deferred.await(initializeAwaitingResponse);
1355 + const waiting = yield* session
1356 + .ensureThread("thread-initialize-hung-waiting")
1357 + .pipe(Effect.flip, Effect.forkChild({ startImmediately: true }));
1358 + yield* TestClock.adjust("30 seconds");
1359 +
1360 + assert.equal((yield* Fiber.join(ensured))._tag, "ProviderAdapterEnsureThreadError");
1361 + // A waiter fails at once instead of re-sending initialize to the hung app-server.
1362 + assert.equal((yield* Fiber.join(waiting))._tag, "ProviderAdapterEnsureThreadError");
1363 + assert.equal(session.initializeRequests(), 1);
1364 + // The session manager retires a session whose event stream fails.
1365 + const exit = yield* Fiber.join(eventsExit);
1366 + assert.isTrue(Exit.isFailure(exit));
1367 + }).pipe(
1368 + Effect.scoped,
1369 + Effect.provide(
1370 + Layer.mergeAll(
1371 + IdAllocator.layer,
1372 + McpProviderSessions.layer,
1373 + NodeServices.layer,
1374 + NodeCrypto.layer,
1375 + ),
1376 + ),
1377 + ),
1378 + );
1379 +
1380 it.effect("completes the handshake after a caller is interrupted mid-initialize", () =>
1381 Effect.gen(function* () {
1382 const initializeAwaitingResponse = yield* Deferred.make<void>();
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
+29 -4
@@ -245,6 +245,8 @@ const decodeCodexBackgroundTerminalTerminateResponse = Schema.decodeUnknownEffec
245 const decodeCodexBackgroundTerminalsListResponse = Schema.decodeUnknownEffect(
246 CodexBackgroundTerminalsListResponse,
247 );
248 +// Initialize normally answers in under a second; leave room for a slow first launch.
249 +const CODEX_INITIALIZE_TIMEOUT = "30 seconds";
250 const CodexThreadSampleResponse = Schema.Struct({ text: Schema.NullOr(Schema.String) });
251 const decodeCodexThreadSampleResponse = Schema.decodeUnknownEffect(CodexThreadSampleResponse);
252 export const CODEX_PROMPT_SUGGESTION_PROMPT =
@@ -1929,7 +1931,15 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
1931 }),
1932 ),
1933 );
1934 + const events = yield* Queue.unbounded<
1935 + ProviderAdapter.ProviderAdapterV2Event,
1936 + ProviderAdapter.ProviderAdapterEventStreamError
1937 + >();
1938 const initialized = yield* Ref.make(false);
1939 + // After a timeout the session is retiring; waiters must not re-send to the hung app-server.
1940 + const initializeTimeout = yield* Ref.make<
1941 + ProviderAdapter.ProviderAdapterEventStreamError | undefined
1942 + >(undefined);
1943 // Codex rejects a second initialize on the shared app-server.
1944 const initializePermit = yield* Semaphore.make(1);
1945 const ensureInitialized = initializePermit.withPermit(
@@ -1938,6 +1948,8 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
1948 if (alreadyInitialized) {
1949 return;
1950 }
1951 + const timedOut = yield* Ref.get(initializeTimeout);
1952 + if (timedOut !== undefined) return yield* timedOut;
1953
1954 const userAgent = yield* client
1955 .request("initialize", {
@@ -1953,6 +1965,23 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
1965 ? Effect.succeed(null)
1966 : Effect.fail(error),
1967 }),
1968 + // A hung app-server never answers, and every thread on this shared
1969 + // session waits behind the permit. Failing the event stream retires
1970 + // the session, so the next turn spawns a fresh app-server.
1971 + Effect.timeoutOrElse({
1972 + duration: CODEX_INITIALIZE_TIMEOUT,
1973 + orElse: () => {
1974 + const error = new ProviderAdapter.ProviderAdapterEventStreamError({
1975 + driver: CODEX_PROVIDER,
1976 + providerSessionId: input.providerSessionId,
1977 + cause: `Codex app-server did not answer initialize within ${CODEX_INITIALIZE_TIMEOUT}.`,
1978 + });
1979 + return Ref.set(initializeTimeout, error).pipe(
1980 + Effect.andThen(Queue.fail(events, error)),
1981 + Effect.andThen(Effect.fail(error)),
1982 + );
1983 + },
1984 + }),
1985 );
1986 const version = userAgent && /\/(\S+)/u.exec(userAgent)?.[1];
1987 const advisory =
@@ -1982,10 +2011,6 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
2011 model: input.modelSelection.model,
2012 now,
2013 });
1985 - const events = yield* Queue.unbounded<
1986 - ProviderAdapter.ProviderAdapterV2Event,
1987 - ProviderAdapter.ProviderAdapterEventStreamError
1988 - >();
2014 // Drain final notifications before the session manager retires the dead process.
2015 // Queue.fail lets readers take items already buffered before they see the failure.
2016 yield* client.awaitTermination.pipe(