Skip to content
Source
Sign inSign up
TanStack/ai

fix(ai-sandbox): end pollJournal cleanly when exec rejects on abort #2207

Mergedwillnie9(GitHub profile, opens in a new tab)merged 3 commits intomain fromrefs/pull/2207/headmerged by AlemTuzlak View on GitHub

willnie9(GitHub profile, opens in a new tab)commented

readJournal with the poll strategy threw the provider's abort error when the signal aborted while a poll exec was running. Now the iteration ends with no error, the same as an abort at any other point in the loop. Errors that do not come from the abort still throw.

Closes #2203

🎯 Changes

  • pollJournal in packages/ai-sandbox/src/journal-reader.ts wraps handle.process.exec(...) in try/catch. If options.signal?.aborted is true, it returns. Otherwise it throws the error again.
  • Two unit tests in packages/ai-sandbox/tests/journal-reader.test.ts: an abort while exec runs, and an exec error with no abort.
  • A patch changeset for @tanstack/ai-sandbox.
  • No docs change. The fix makes the poll strategy do what the follow strategy already does on abort.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with pnpm run test:pr, or these tests do not apply to this pull request. (Not run. Only the light checks in Testing ran. CI runs the rest.)
  • I fully understand the code in this pull request, including any code generated with AI assistance.
  • Docs: I updated docs/ for this change, or this change is not user-facing.
  • Changeset: I added a changeset (pnpm changeset), or this PR does not change a published package.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Root cause

Issue. A consumer of readJournal(handle, { strategy: 'poll', signal }) aborts the signal. If the abort occurs while a poll exec runs, and the provider's exec rejects on abort, the for await loop throws an AbortError. Cause. pollJournal treats an abort as a normal end at the while check, after the yield, and in sleep. The await handle.process.exec(...) call has no such guard, so the rejection goes out of the generator. Fix. A try/catch around exec returns when the signal is aborted, and throws the error again when it is not.

Possible alternatives

  • Do not pass the signal to exec. Then exec never rejects on abort. But a poll could then run until it ends by itself after the abort, so the abort becomes slower.
  • Catch only errors with name === 'AbortError'. Providers reject with different errors (a DOMException, signal.reason, or a custom error). Checking signal.aborted covers all of them.

Testing

Commands run (reviewer, this session):

  • An agent-written repro test (a fake handle whose exec rejects with signal.reason on abort, abort after 20 ms), the same file on both sides:
    • Clean main (7fbfa5d6c): × repro #2207: abort during an in-flight poll exec ends iteration silently → AbortError: This operation was aborted, Tests 1 failed (1).
    • This PR: Tests 1 passed (1).
  • vitest run tests/journal-reader.test.ts --maxWorkers=2 in packages/ai-sandbox: 28 passed.
  • pnpm run test:types and pnpm run test:oxlint in packages/ai-sandbox: pass.
  • oxfmt --check on the changed files: pass (after a format fix of the new test).
  • Not run locally: the full pnpm test:pr and E2E. CI runs them.

Manual test:

  1. On main, read a journal with strategy: 'poll' and a handle whose exec rejects when its signal aborts.
  2. Abort the signal while exec runs. The for await throws AbortError.
  3. On this branch, do the same steps. The loop ends and throws nothing.

How this PR makes testing easy: two unit tests in packages/ai-sandbox/tests/journal-reader.test.ts. There is no E2E test.

Risk / rollback

Low risk. The change is in one await in the poll loop, and only the abort path changes. To undo, revert this PR.

Summary by CodeRabbit

  • Bug Fixes
    • Journal reads now stop quietly when an in-progress read is cancelled, rather than surfacing an error.
    • Errors from journal reads that are not cancelled continue to be reported.
    1. Pushedae0cf5f

    2. Pushedc55426f

    3. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit c55426f0971f71e052a35b05ea35f445cd940059

      CommandStatusDurationResult
      nx run-many --targets=build --exclude=examples/...✅ Succeeded8sView ↗

      💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗


      ☁️ Nx Cloud last updated this comment at 2026-11-30 09:26:13 UTC

    4. AlemTuzlak merged this59dbea4

    5. AlemTuzlak closed this

Sign in to join this conversation.