Repository navigation
Conversation
…ling teardown reaches the loop instead of crashing Ports the intent of v11's #7867 and #7884 to toAsyncIterable, which client subscriptions and the OpenAPI handler's SSE use. A fiber reads the stream into a bounded queue; aborting interrupts it, so the source's finalizers run at once, and the values already queued are still read. A finalizer that fails rejects the next next() once, or return() if nothing read it, instead of an unhandled rejection from `void iterator.return()`.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
…ling teardown reaches the loop instead of crashing (#7906) Ports the intent of v11's #7867 and #7884 to toAsyncIterable: a producer fiber reads the stream into a bounded Queue, abort interrupts it, the values already received are still yielded, and a teardown failure rejects the next read (or return() on break) once instead of going unhandled.
|
🤖 CI was green, so this landed squashed on |
|
This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you. |
Why
observableToAsyncIterable's abort and teardown fixes). v12 has no observables. Its equivalent istoAsyncIterableininternal/effects.ts, which wrapsStream.toAsyncIterableand is used by clientsubscribe()and the OpenAPI handler's SSE body.next()settles whenreturn()interrupts it, and a stream can't emit after it closes. Two v11 bug classes were still there:toAsyncIterablecalledvoid iterator.return(). When a stream finalizer fails, for example a link whoseStream.ensuringdies, that promise rejects and nothing handles it. With nonext()pending, that's an unhandled rejection, which crashes Node.next()rejected again; v11 rejects once, then is done.What
toAsyncIterable(stream, signal)keeps its signature and callers. With a signal, it is nowStream.toAsyncIterable(endOnAbort(stream, signal)).endOnAbort, all in Effect:Effect.forkScoped) reads the stream into a boundedQueueofbackpressure.bufferSize(16). A slow reader still holds the source back.Effect.callback, forked in the same scope. When it fires, it interrupts the producer, so the source's finalizers run at once, not on the next pull.Effect.onExitturns itsExitinto how the queue ends. Interruption alone ends it, so the reader gets the queued values and then{ done: true }. A teardown failure (theCausewithout its interrupts) fails it after those values, so the nextnext()rejects with it once.Effect.addFinalizeron the scope: onreturn()(break), it interrupts the producer and checks itsExit. If the scope closed successfully, nothing has read a teardown failure yet, so the finalizer fails with it andreturn()rejects. If the scope closed with a failure, the reader already saw it, so it isn't raised again.{ done: true }. A teardown failure rejects the nextnext()after them, once.break/return(): queued values are dropped. A teardown failure rejectsreturn(), so thefor awaitthrows it.next()after the queued values, once.QueueandFiber). The table in.agent-docs/notes/benchmarks.mdis updated.Links
None. No docs change: client subscriptions' documented abort behaviour (the loop ends) is unchanged.
Alternatives considered
void iterator.return().catch(() => {}). This stops the crash but loses the teardown error, and abort still drops values already received.Stream.interruptWhen(aborted)orStream.haltWhen. A probe showed both drop a dying finalizer's defect without a trace, even onbreak. They also stop the source only when the next pull happens.Stream.runIntoQueuefor the producer. It writes the interruption itself into the queue, so the reader would see an interrupt cause instead of the end.next()calls). Effect'sQueueandStream.toAsyncIterablealready provide both.Testing
internal/effects.test.tshas 8 new tests. Two fail onv12before this change:an abort still yields the values already received:expected { done: true } to deeply equal { done: false, value: 1 }.a finalizer that fails on abort rejects the next() after the queued values, once: two unhandled rejections (Error: teardown).next()rejected by a teardown failure,return()after an unread teardown failure,breakthrowing a teardown failure,breakdropping queued values, a source error rejecting once, and finalizers running at abort.client/links/links.test.tsadds one end-to-end test throughcreateUntypedClient().subscribe()with a link whose teardown dies. Before:expected [ 1 ] to deeply equal [ 1, 2 ]. After: the loop gets both values and then rejects with the teardown error.dedupe.test.ts's listener-count test spies onremoveEventListener, so the abort listener removes itself explicitly instead of relying on{ once: true }.pnpm test packages/trpc: 94 files, 1083 tests pass.pnpm typecheckandpnpm fix(oxlint and oxfmt) pass.pnpm bench budgetnumbers are above.AI model
Claude Opus 5.5
Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.