Skip to content

fix(v12): report a subscription teardown that fails after an HTTP client leaves - #7908

Closed
KATT wants to merge 1 commit into
v12from
cursor/v12-report-http-teardown-errors-6664
Closed

KATT wants to merge 1 commit into
v12from
cursor/v12-report-http-teardown-errors-6664

Conversation

@KATT

@KATT KATT commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Why

What

  • reportTeardown(stream, report) in internal/effects.ts:
    • It runs the stream in a producer fiber of its own, feeding a bounded Queue that the reader reads back. When the reader's scope closes, an Effect.addFinalizer interrupts the producer and reads its Exit. The Cause without its interrupts is the teardown failure.
    • It reports only when the reader never saw the failure: the scope closed successfully (an early end such as take) or was interrupted only. A failure the reader did get isn't reported again.
    • The producer's exit is forwarded unchanged. A stream that interrupts itself still just ends, so the existing "one that interrupts itself just ends" test keeps passing.
    • fix(trpc): an aborted subscription yields what it received, and a failing teardown reaches the loop instead of crashing #7906's endOnAbort now shares the same produce helper. Its behaviour is unchanged.
  • returnUnawaited(iterable, onLateFailure): what the iterator throws after the stream stopped waiting goes to onLateFailure. That covers both the next() that was in flight and return()'s rejection. Without a callback, it behaves as before.
  • SubscriptionTeardown, an internal Context.Reference that defaults to undefined:
    • toEventStream reads it from the call's services. When it is set, it wraps Stream subscriptions in reportTeardown and passes the callback to returnUnawaited.
    • reportSubscriptionTeardown(report) builds the services. It skips AbortError, because that is how a source aborted by the call's signal ends, and runs the failure through normalizeCause.
  • The RPC subscription handler (server/handler/subscription.ts) and the OpenAPI handler (for subscriptions) pass these services to core.run. The report calls onError with type: 'subscription' and the path.
  • Nothing else changes:
  • Bundle: the minimal and progressive-JSON clients grow 0.1 kB gzip, because returnUnawaited is shared. .agent-docs/notes/benchmarks.md is updated.

Alternatives considered

  • Stream.onExit on the subscription stream. It passes for RPC but fails for OpenAPI. Probes showed that when the reader is interrupted while handling a value, the stream's own exit is a plain interruption and the finalizer's defect is missing. OpenAPI's keep-alive Stream.merge hits exactly that timing. The fiber that runs the stream still gets the defect in its Exit, which is why the producer fiber is needed.
  • Re-raising the failure to the reader instead of reporting it. The HTTP reader has left, so it would be lost again.

Testing

  • New regression tests. Both fail on the base with expected [] to have a length of 1 and pass after the change:
    • server/handler/fetch-edges.test.ts: RPC SSE.
    • openapi/openapi.test.ts: OpenAPI.
    • Each cancels the response body after the first event. It covers a Stream whose finalizer dies and an async function* that throws from finally once its signal aborts, and checks that onError gets exactly one teardown error with its path and type: 'subscription'.
  • internal/effects.test.ts, four new unit tests:
    • reportTeardown reports when its reader is interrupted mid-value, which is the case Stream.onExit misses.
    • It reports when the reader ends the stream early.
    • It doesn't report a failure the reader got.
    • returnUnawaited hands a finally throw to onLateFailure.
  • No existing tests were modified. pnpm fix, pnpm typecheck, pnpm test (1,971 passed) and pnpm coverage all pass. After rebasing onto the latest v12, typecheck and the 656 tests under internal, server, openapi and client pass again.
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.

@KATT
KATT requested a review from a team as a code owner November 4, 2026 16:24
@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:50pm 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
cursor Bot changed the base branch from cursor/v12-async-iterable-abort-teardown-6664 to v12 November 4, 2026 16:36
@cursor
cursor Bot force-pushed the cursor/v12-report-http-teardown-errors-6664 branch from 1b153d2 to c1650ff Compare November 4, 2026 16:36
…ent leaves

RPC SSE and OpenAPI subscriptions now hand a finalizer that fails as the
stream is interrupted, or a generator that throws from finally after the
client disconnects, to onError instead of dropping it.
@cursor
cursor Bot force-pushed the cursor/v12-report-http-teardown-errors-6664 branch from c1650ff to 1f0dea9 Compare November 4, 2026 16:40
cursor Bot pushed a commit that referenced this pull request Nov 4, 2026
…ent leaves (#7908)

RPC SSE and OpenAPI subscriptions now hand a finalizer that fails as the
stream is interrupted, or a generator that throws from finally after the
client disconnects, to onError instead of dropping it.
@cursor

cursor Bot commented Nov 4, 2026

Copy link
Copy Markdown

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

@KATT KATT closed this Nov 4, 2026
cursor Bot pushed a commit that referenced this pull request Nov 4, 2026
…ort of #7888)

A subscription's iterator runs on after the client stops it or its
socket closes, and what it throws then (a generator's `finally` once its
signal aborts, a `return()` that rejects) was dropped on both socket
handlers. v11's adapter reports it to `onError` since #7888.

#7908 gave the HTTP subscription handlers `reportSubscriptionTeardown`;
the v11 interop socket and the native socket now pass it in a
subscription call's services too, so the same failures reach `onError`,
except an `AbortError`, which is how an aborted call ends.

Co-authored-by: Alex / KATT <[email protected]>
@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 successfully deployed

1 active deployment
Preview – www — 1f0dea98 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