t3code icon indicating copy to clipboard operation
t3code copied to clipboard

interruptAndAwaitTerminal busy-spins 1,000 SQLite reads via setImmediate and fails restarts prematurely

Open mateodelcon1 opened this issue 4 days ago • 1 comments

What happened

During a provider turn restart, ProviderTurnControlService.interruptAndAwaitTerminal executes up to 1,000 multi-table SQLite read queries back-to-back with only a setImmediate (0 ms) yield between iterations. On fast NVMe machines, all 1,000 iterations finish in roughly 150–500 ms — saturating a SQLite reader worker and throwing ProviderTurnControlError whenever a provider takes more than ~0.5 s to cancel a running tool call.

Diagnosis

In apps/server/src/orchestration-v2/ProviderTurnControlService.ts (lines 27–36 and 378–404):

  • interruptAndAwaitTerminal calls interruptTurn(...) and then runs a tight loop for (let remaining = 1_000; remaining > 0; remaining -= 1) calling projections.getProviderControlContext(...) followed only by yieldToRuntime (Effect.yieldNow + setImmediate).
  • Unlike interrupt() in the same file (lines 318–341), which waits via eventSink.stream({ threadId }) bounded by a wall-clock Effect.timeoutOption(...), interruptAndAwaitTerminal has no wall-clock budget and no event-driven subscription.
  • Consequently, faster hardware gets a shorter timeout window, hammers the SQLite WAL reader pool with 1,000 queries in a fraction of a second, and throws ProviderTurnControlError("Provider turn ... did not terminalize before restart.") — which in turn feeds into repeated provider-turn.restart outbox retries.

Steps to reproduce

  1. Run a provider turn that takes > 500 ms to acknowledge an interrupt (e.g., an active tool call or slower provider process) on a host with fast SSD/NVMe storage.
  2. Trigger a turn restart (provider-turn.restart), such as restarting a running turn with a model/selection change or restart send mode.
  3. Observe ProviderTurnControlService.interruptAndAwaitTerminal exhausting all 1,000 getProviderControlContext iterations in < 1 second and failing with ProviderTurnControlError.

Version

0.1.13 (also confirmed on main @ 9a66ff1e and c203d933)

Environment

Windows 10/11 (NT 10.0.26300), Node v26.13.1 (platform-independent server logic in apps/server)

Evidence

// apps/server/src/orchestration-v2/ProviderTurnControlService.ts (lines 27-36)
const yieldToRuntime = Effect.yieldNow.pipe(
  Effect.andThen(
    Effect.promise(
      () =>
        new Promise<void>((resolve) => {
          setImmediate(resolve);
        }),
    ),
  ),
);

// apps/server/src/orchestration-v2/ProviderTurnControlService.ts (lines 378-404)
for (let remaining = 1_000; remaining > 0; remaining -= 1) {
  const { providerTurn, attempt } = yield* projections.getProviderControlContext(
    input.threadId,
    {
      providerThreadId: input.providerThreadId,
      providerTurnId: input.providerTurnId,
      attemptId: input.interruptedAttemptId,
    },
  );
  if (
    providerTurn !== undefined &&
    providerTurn.status !== "running" &&
    attempt !== undefined &&
    attempt.status !== "running"
  ) {
    return;
  }
  // Yield through Node's event loop; detached ingestion cannot advance a deterministic Effect clock.
  yield* yieldToRuntime;
}
return yield* new ProviderTurnControlError({
  threadId: input.threadId,
  operation: "restart",
  providerTurnId: input.providerTurnId,
  cause: `Provider turn ${input.providerTurnId} did not terminalize before restart.`,
});

Related issues

  • #34271 (closed — Codex shared app-server death during steering restart; did not replace the setImmediate polling loop in interruptAndAwaitTerminal).

Fix applied or workaround

None applied to live install. Replacing the 1,000-iteration setImmediate busy-spin with the event-driven eventSink.stream({ threadId }) + wall-clock timeout pattern already used in ProviderTurnControlService.interrupt() eliminates both the SQLite reader spike and hardware-speed-dependent premature timeouts.

Filed by

Antigravity (gemini-4-neon) via t3 triage playbook

mateodelcon1 avatar Nov 27 '26 23:11 mateodelcon1

I tried to reproduce this on the current build and could not.

Commit 93955ac9bd (origin/main). I ran T3CODE_DEV_SEED=0 vp run dev in a fresh worktree with a fresh database, paired the web client, and used the Replay (dev) provider (the recorded ACP agent in the repo). I added a temporary log to interruptAndAwaitTerminal that recorded how many poll iterations ran and the wall time from the end of interruptTurn to exit. Steps: start a turn on the 'Long stream' replay model; after about 5 s, switch the composer model to 'Heavy turn' and press Steer. This is a model change mid-turn, so the server runs a provider-turn.restart effect. I repeated this 5 times on the same thread. In all 5 runs the loop exited as terminalized (provider turn 'interrupted', attempt 'superseded') after 6, 6, 7, 6 and 7 iterations, taking 13.8, 13.2, 11.5, 14.4 and 9.9 ms (median 13.2 ms, range 9.9 to 14.4 ms). That is about 1.4 to 2.4 ms per iteration, so the full 1,000-iteration budget is about 1.4 to 2.4 s of wall time on this Linux VM, not the 150 to 500 ms in the report. No 'did not terminalize before restart' error appeared, and every replacement turn started. The report says a provider slow to cancel a tool call (>0.5 s) runs out the loop. The code does not work that way: the poll starts only after session.interruptTurn returns, and the adapters I read wait for the cancel and finish the turn inside interruptTurn. Codex waits on its turn waiters with 10 s timeouts and then finalizes. Claude waits for the query to close and fails with ProviderAdapterInterruptTimeoutError otherwise. ACP calls finalizeTurn inside the call. A slow cancel therefore costs time inside interruptTurn, not loop iterations. The loop only covers the short delay until the terminal events are written to the projection. I found no real-provider path that fails early. The loop does use an iteration count instead of a time limit, and it polls instead of listening for events, unlike interrupt()/awaitSettled in the same file (changed in #35948). It could only fail if writing those events to the projection fell about 2 s behind.

Closing as not planned. If it still happens for you, reply with the exact steps and the version you are on and it will be looked at again.

juliusmarminge avatar Nov 28 '26 00:11 juliusmarminge