fix(ai-sandbox): end pollJournal cleanly when exec rejects on abort #2207
main fromrefs/pull/2207/head·merged by AlemTuzlak View on GitHubreadJournal 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#
pollJournalinpackages/ai-sandbox/src/journal-reader.tswrapshandle.process.exec(...)intry/catch. Ifoptions.signal?.abortedistrue, it returns. Otherwise it throws the error again.- Two unit tests in
packages/ai-sandbox/tests/journal-reader.test.ts: an abort whileexecruns, and anexecerror 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. Thenexecnever 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 (aDOMException,signal.reason, or a custom error). Checkingsignal.abortedcovers all of them.
Testing#
Commands run (reviewer, this session):
- An agent-written repro test (a fake handle whose
execrejects withsignal.reasonon 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).
- Clean
vitest run tests/journal-reader.test.ts --maxWorkers=2inpackages/ai-sandbox: 28 passed.pnpm run test:typesandpnpm run test:oxlintinpackages/ai-sandbox: pass.oxfmt --checkon the changed files: pass (after a format fix of the new test).- Not run locally: the full
pnpm test:prand E2E. CI runs them.
Manual test:
- On
main, read a journal withstrategy: 'poll'and a handle whoseexecrejects when its signal aborts. - Abort the signal while
execruns. Thefor awaitthrowsAbortError. - 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.
View your CI Pipeline Execution ↗ for commit c55426f0971f71e052a35b05ea35f445cd940059
nx run-many --targets=build --exclude=examples/...💡 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:13UTC