Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: honour a Stop that lands while the Codex thread is starting (#5649)

Adam Eivye120f73b61e7
diff --git a/server/services/codexAppServer.js b/server/services/codexAppServer.js
index 9f97cf6251..ae0edeeb04 100644
--- a/server/services/codexAppServer.js
+++ b/server/services/codexAppServer.js
@@ -794,6 +794,13 @@ export async function runCodexTextTurn({
// silently send this turn's frames to a child that has never heard of its
// thread id.
const owner = await connect();
+ // `connect()` can spawn and handshake a whole app-server child, which is the
+ // longest await in this function. Re-read the signal before paying for a
+ // thread the caller has already abandoned — the up-front check above ran
+ // several awaits ago, and no listener is registered yet.
+ if (signal?.aborted) {
+ throw codexError(CODEX_TURN_ERROR_CODES.turnInterrupted, 'The Codex turn was cancelled.', { status: 499 });
+ }
const started = await sendRequest(owner, CODEX_TURN_RPC.threadStart, {
...CODEX_TEXT_THREAD_CONFIG,
cwd,
@@ -829,7 +836,12 @@ export async function runCodexTextTurn({
timer.unref?.();
const onAbort = () => fail(codexError(CODEX_TURN_ERROR_CODES.turnInterrupted, 'The Codex turn was cancelled.', { status: 499 }));
- signal?.addEventListener('abort', onAbort, { once: true });
+ // An AbortSignal fires its 'abort' event exactly once; a listener added after
+ // the fact never runs. An abort raised during `connect()`/`thread/start` has
+ // to be honoured by re-reading the flag, or the turn streams for the whole
+ // deadline and bills for an answer nobody is waiting for.
+ if (signal?.aborted) onAbort();
+ else signal?.addEventListener('abort', onAbort, { once: true });
try {
const response = await sendRequest(owner, CODEX_TURN_RPC.turnStart, {
diff --git a/server/services/codexAppServer.turn.test.js b/server/services/codexAppServer.turn.test.js
index 32c9894cc8..b2e1e95272 100644
--- a/server/services/codexAppServer.turn.test.js
+++ b/server/services/codexAppServer.turn.test.js
@@ -168,6 +168,42 @@ describe('running a text turn', () => {
expect(child.lastRequest('turn/interrupt').params).toEqual({ threadId: 'thread-1', turnId: 'turn-1' });
});
+ it('abandons the turn when the caller aborts during the app-server cold start', async () => {
+ // `connect()` can spawn and handshake a whole child, so the up-front
+ // `signal.aborted` check is many awaits stale by the time it resolves and
+ // no listener is registered yet. Without a re-read, a Stop pressed here
+ // dispatched a full turn against the subscription.
+ const controller = new AbortController();
+ const promise = runCodexTextTurn({ prompt: 'long one', signal: controller.signal });
+
+ let frame = null;
+ await vi.waitFor(() => { frame = child.take('initialize'); expect(frame).toBeTruthy(); });
+ controller.abort();
+ child.reply(frame, {});
+
+ await expect(promise).rejects.toMatchObject({ code: CODEX_TURN_ERROR_CODES.turnInterrupted });
+ expect(child.lastRequest('thread/start')).toBeUndefined();
+ expect(child.lastRequest('turn/start')).toBeUndefined();
+ });
+
+ it('honours an abort that lands while the thread is starting, which fires no late event', async () => {
+ // An AbortSignal emits 'abort' exactly once: aborting before the listener
+ // is registered means the listener never runs, so the flag has to be
+ // re-read at registration time.
+ const controller = new AbortController();
+ const promise = runCodexTextTurn({ prompt: 'long one', signal: controller.signal });
+ await handshake();
+ await awaitRequest(child, 'thread/start', (frame) => {
+ controller.abort();
+ child.reply(frame, { thread: { id: 'thread-1' } });
+ });
+ await awaitRequest(child, 'turn/start', { turn: { id: 'turn-1', items: [], status: 'inProgress' } });
+
+ await expect(promise).rejects.toMatchObject({ code: CODEX_TURN_ERROR_CODES.turnInterrupted });
+ await vi.waitFor(() => expect(child.lastRequest('turn/interrupt')).toBeTruthy());
+ expect(child.lastRequest('turn/interrupt').params).toEqual({ threadId: 'thread-1', turnId: 'turn-1' });
+ });
+
it('carries a quota failure out as a usage-limit category', async () => {
const promise = runCodexTextTurn({ prompt: 'hi' });
await handshake();