Skip to content

Commit 21d3b2e

Browse files
committed
fix(codex): freeze cancellation before attempt teardown
Close cancellation admission synchronously when cleanup takes ownership, including when finalization throws before freezing its terminal outcome. This keeps late aborts from replacing the joined cleanup promise during resource release or masking the original finalization error. Move the existing terminal freeze instead of adding another lifecycle flag. Verified the regression fails before the change; all 413 Codex tests pass.
1 parent 217b366 commit 21d3b2e

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎extensions/codex/src/app-server/run-attempt-cleanup.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ export async function cleanupCodexAttempt(
4545
// Exact-thread cron authority exists only while this creator turn owns the
4646
// live client/thread. Retained model callbacks must fail after cleanup begins.
4747
prompt.context.attemptTools.scheduledAppAuthoritySourceRef.current = undefined;
48+
// Finalization can throw before freezing. Close cancellation admission before
49+
// any teardown await so it cannot replace the cleanup promise being joined.
50+
freezeRunTerminalOutcome();
4851
// Join late cancellation before releasing the subscription, but do not let a
4952
// failed terminal RPC skip resource cleanup. Surface that failure below.
5053
await state.abortCleanup.catch(() => undefined);
@@ -198,7 +201,6 @@ export async function cleanupCodexAttempt(
198201
runAbortController.signal.removeEventListener("abort", abortListener);
199202
});
200203
await runCleanupStep("codex-steering-cancel", () => steeringQueueRef.current?.cancel());
201-
await runCleanupStep("codex-terminal-freeze", freezeRunTerminalOutcome);
202204
await runCleanupStep("codex-reply-backend-detach", () =>
203205
params.replyOperation?.detachBackend(handle),
204206
);

‎extensions/codex/src/app-server/run-attempt-thread-cleanup.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { EmbeddedRunAttemptParamsV2 as EmbeddedRunAttemptParams } from "ope
44
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
55
import { readAttemptTerminal } from "./attempt-terminal.test-helper.js";
66
import { CodexAppServerClient } from "./client.js";
7+
import { CodexAppServerEventProjector } from "./event-projector.js";
78
import type { CodexServerNotification } from "./protocol.js";
89
import {
910
createParams as createSharedParams,
@@ -756,6 +757,44 @@ describe("Codex app-server main thread cleanup", () => {
756757
},
757758
);
758759

760+
it("rejects late cancellation after failed finalization enters cleanup", async () => {
761+
const harness = createClientHarness();
762+
vi.spyOn(CodexAppServerClient, "start").mockReturnValueOnce(harness.client);
763+
const abort = new AbortController();
764+
const params = createParams(
765+
path.join(tempDir, "failed-finalization.jsonl"),
766+
path.join(tempDir, "failed-finalization-workspace"),
767+
);
768+
params.abortSignal = abort.signal;
769+
const projectionError = new Error("terminal projection failed");
770+
const run = runCodexAppServerAttempt(params, {
771+
bindingStore: testCodexAppServerBindingStore,
772+
});
773+
const failure = run.catch((error: unknown) => error);
774+
const initialize = await waitForHarnessRequest(harness, "initialize");
775+
harness.send({
776+
id: initialize.id,
777+
result: { userAgent: `openclaw/${CODEX_APP_SERVER_VERSION} (macOS; test)` },
778+
});
779+
const threadStart = await waitForHarnessRequest(harness, "thread/start");
780+
harness.send({ id: threadStart.id, result: threadStartResult() });
781+
const turnStart = await waitForHarnessRequest(harness, "turn/start");
782+
harness.send({ id: turnStart.id, result: turnStartResult() });
783+
vi.spyOn(CodexAppServerEventProjector.prototype, "buildResult").mockImplementationOnce(() => {
784+
throw projectionError;
785+
});
786+
harness.send({
787+
method: "turn/completed",
788+
params: { threadId: "thread-1", turn: { id: "turn-1", status: "completed" } },
789+
});
790+
const unsubscribe = await waitForHarnessRequest(harness, "thread/unsubscribe");
791+
abort.abort("cancelled during cleanup");
792+
const methods = harness.writes.map((write) => JSON.parse(write).method);
793+
harness.send({ id: unsubscribe.id, result: {} });
794+
expect(methods).not.toContain("turn/interrupt");
795+
expect(await failure).toBe(projectionError);
796+
});
797+
759798
it("gracefully retires a shared Codex client when a failed turn cannot unsubscribe", async () => {
760799
const sessionFile = path.join(tempDir, "session.jsonl");
761800
const workspaceDir = path.join(tempDir, "workspace");

0 commit comments

Comments
 (0)