`ai-sandbox`: `pollJournal` throws when the signal aborts during a poll, instead of ending the iteration #2203
Send to my agentTanStack AI version#
1.7.0
Framework/Library version#
React 19.3.1, @tanstack/ai-sandbox
Describe the bug and the steps to reproduce it#
TanStack AI version: @tanstack/ai-sandbox 1.4.0
Describe the bug and the steps to reproduce it
pollJournal in packages/ai-sandbox/src/journal-reader.ts passes the caller's signal to handle.process.exec and awaits it without guarding the abort case:
while (!options.signal?.aborted) {
const result = await handle.process.exec(
journalReadCommand(options.paths, position),
processOptions(options),
)
// ...
if (options.signal?.aborted) return
await sleep(intervalMs, options.signal)
}
The loop treats an abort as a normal end of iteration everywhere else: the while condition, the check after the lines are yielded, and the abortable sleep. The follow strategy does the same through untilAborted. The one exception is an abort that lands while exec is in flight. A provider whose exec rejects on abort makes the generator throw, so the consumer sees an error for a cancellation it requested itself.
Steps to reproduce:
- Read a journal with the polling strategy on a provider whose
process.execrejects when its signal aborts. - Abort the signal while a poll is in flight.
- The
for awaitover the journal throws the provider's abort error instead of completing.
Minimal reproduction
No sandbox needed: a fake handle whose exec rejects on abort is enough. Save both files, then npm install && npm start.
package.json
{
"name": "repro-poll-journal-abort",
"private": true,
"type": "module",
"scripts": { "start": "node repro.mjs" },
"dependencies": { "@tanstack/ai-sandbox": "1.4.0" }
}
repro.mjs
import { readJournal } from '@tanstack/ai-sandbox'
// A provider whose `exec` takes a while and rejects when its signal aborts,
// the way a fetch-backed provider does.
const handle = {
process: {
exec: (_command, options) =>
new Promise((resolve, reject) => {
const timer = setTimeout(
() => resolve({ stdout: '', stderr: '', exitCode: 0 }),
1_000,
)
options?.signal?.addEventListener('abort', () => {
clearTimeout(timer)
reject(options.signal.reason)
})
}),
},
}
const paths = {
dir: '/tmp/journals',
journal: '/tmp/journals/run.ndjson',
stderr: '/tmp/journals/run.err',
nonce: 'nonce',
}
const controller = new AbortController()
// Abort while the first poll's `exec` is still in flight.
setTimeout(() => controller.abort(), 100)
try {
for await (const line of readJournal(handle, {
paths,
strategy: 'poll',
signal: controller.signal,
})) {
console.log('line', line)
}
console.log('OK: the iteration ended silently on abort')
} catch (error) {
console.log('BUG: the iteration threw on abort ->', String(error))
process.exitCode = 1
}
Output on 1.4.0:
BUG: the iteration threw on abort -> AbortError: This operation was aborted
With the fix below applied to dist/esm/journal-reader.js, the same script prints OK: the iteration ended silently on abort.
Expected behaviour
Aborting ends the iteration silently, whichever point of the loop the abort lands on.
Suggested fix
let result
try {
result = await handle.process.exec(
journalReadCommand(options.paths, position),
processOptions(options),
)
} catch (error) {
if (options.signal?.aborted) return
throw error
}
Errors that are not caused by the abort still propagate. We have been running this change as a local patch on 1.3.0 and 1.4.0.
Your Minimal, Reproducible Example - (Sandbox Highly Recommended)#
Typescript
Screenshots or Videos (Optional)#
No response
Do you intend to try to help solve this bug with your own PR?#
None
Terms & Code of Conduct#
- I agree to follow this project's Code of Conduct
- I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
tanstack-source added labelsbug
github-actions[bot](GitHub profile, opens in a new tab) assignedAlemTuzlak
github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer
Sign in to join this conversation.