Skip to content

fix(trpc): an aborted subscription yields what it received, and a failing teardown reaches the loop instead of crashing - #7906

Closed
KATT wants to merge 2 commits into
v12from
cursor/v12-async-iterable-abort-teardown-6664
Closed

KATT wants to merge 2 commits into
v12from
cursor/v12-async-iterable-abort-teardown-6664

Conversation

@KATT

@KATT KATT commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Why

What

  • toAsyncIterable(stream, signal) keeps its signature and callers. With a signal, it is now Stream.toAsyncIterable(endOnAbort(stream, signal)).
  • endOnAbort, all in Effect:
    • A fiber (Effect.forkScoped) reads the stream into a bounded Queue of backpressure.bufferSize (16). A slow reader still holds the source back.
    • The abort signal becomes an 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.
    • The producer's Effect.onExit turns its Exit into how the queue ends. Interruption alone ends it, so the reader gets the queued values and then { done: true }. A teardown failure (the Cause without its interrupts) fails it after those values, so the next next() rejects with it once.
    • Effect.addFinalizer on the scope: on return() (break), it interrupts the producer and checks its Exit. If the scope closed successfully, nothing has read a teardown failure yet, so the finalizer fails with it and return() rejects. If the scope closed with a failure, the reader already saw it, so it isn't raised again.
  • Semantics, as on v11 after fix(server): settle pending next() on abort and ignore teardown emissions in observableToAsyncIterable #7867 and fix(server): don't crash when a subscription's teardown throws on abort #7884:
    • Abort: the source stops now. Values already received are still yielded, then { done: true }. A teardown failure rejects the next next() after them, once.
    • break / return(): queued values are dropped. A teardown failure rejects return(), so the for await throws it.
    • A failure from the source rejects the next() after the queued values, once.
    • An already aborted signal never starts the stream.
  • Bundle: the three clients grow 0.3–0.4 kB gzip (Queue and Fiber). The table in .agent-docs/notes/benchmarks.md is 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) or Stream.haltWhen. A probe showed both drop a dying finalizer's defect without a trace, even on break. They also stop the source only when the next pull happens.
  • Stream.runIntoQueue for the producer. It writes the interruption itself into the queue, so the reader would see an interrupt cause instead of the end.
  • v11's two hand-rolled queues (values and pending next() calls). Effect's Queue and Stream.toAsyncIterable already provide both.

Testing

  • internal/effects.test.ts has 8 new tests. Two fail on v12 before 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).
  • The other six pin the semantics and pass both before and after: a pending next() rejected by a teardown failure, return() after an unread teardown failure, break throwing a teardown failure, break dropping queued values, a source error rejecting once, and finalizers running at abort.
  • client/links/links.test.ts adds one end-to-end test through createUntypedClient().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.
  • No existing test changed. dedupe.test.ts's listener-count test spies on removeEventListener, so the abort listener removes itself explicitly instead of relying on { once: true }.
  • pnpm test packages/trpc: 94 files, 1083 tests pass. pnpm typecheck and pnpm fix (oxlint and oxfmt) pass. pnpm bench budget numbers are above.

AI model

Claude Opus 5.5

Open in Web Open in Cursor 

Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

…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()`.
@KATT
KATT requested a review from a team as a code owner November 4, 2026 16:07
@vercel

vercel Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
www Ready Ready Preview Nov 4, 2026 4:14pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

cursor Bot pushed a commit that referenced this pull request Nov 4, 2026
…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.
@cursor

cursor Bot commented Nov 4, 2026

Copy link
Copy Markdown

🤖 CI was green, so this landed squashed on v12 as 93fb95a, via pnpm land per next/AGENTS.md. I'm closing the PR.

@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

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.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Nov 5, 2026

This branch was previously deployed

1 inactive deployment
Preview – www — d6db30c9 Deployed Nov 4, 2026 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants