Skip to content

fix(client): stop a finished query from holding a streamed batch open - #8194

Merged
trpc-bot merged 1 commit into
trpc:mainfrom
pengzh1:fix/http-batch-stream-link-settled-abort
Nov 24, 2026
Merged

trpc-bot merged 1 commit into
trpc:mainfrom
pengzh1:fix/http-batch-stream-link-settled-abort

Conversation

@pengzh1

@pengzh1 pengzh1 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Why

  • A page sends a plain query and a streamed query in one httpBatchStreamLink batch, then the streamed query is cancelled (the user navigates away, cancelQueries). The query cancels, but the request stays open and the server keeps running its generator.

What

  • A query that settles with nothing left to stream aborts its own signal, as a failed one does.
  • A result that still holds a stream keeps its signal.
  • Failed and rejected operations are unchanged.

Links

Alternatives considered

Testing

  • Regression check: cancels a batch holding a settled query and a streamed query in packages/client/src/links/httpAbortCleanup.test.ts fails on main: the stream runs to tick 10 after the cancel.
  • stops a streamed query cancelled before it streams, in a batch with a settled query fails on main too: the server's signal never aborts.

AI model

  • glm-5.4

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

Summary by CodeRabbit

  • Bug Fixes
    • Improved cancellation handling for streamed queries in batched requests. Cancelling a streamed query now also cancels its server-side work, while other queries in the batch can still complete successfully.

`httpBatchStreamLink` aborts a batch's request once every operation's
signal has aborted. trpc#8056 made a failed operation abort its own signal,
but a successful one never did: `isDone` only kept the teardown from
aborting it.

So with a settled query in the batch, cancelling a streamed query left
the request open, and the server kept running its generator. A query
cancelled before its stream arrived kept it open until the server closed.

A successful operation now aborts its own signal when its result has
nothing left to stream, by the same test the server makes before it
refuses a stream in a non-streaming request. A result that still holds a
stream keeps its signal. The failed and rejected paths are unchanged.
@pengzh1
pengzh1 requested a review from a team as a code owner November 24, 2026 06:56
@vercel

vercel Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
examples-todomvc Ready Ready Preview Nov 24, 2026 6:59am UTC
next-prisma-starter Ready Ready Preview Nov 24, 2026 6:59am UTC
trpc-next-app-dir Ready Ready Preview Nov 24, 2026 6:59am UTC
www Ready Ready Preview Nov 24, 2026 6:59am UTC

Request Review

@vercel

vercel Bot commented Nov 24, 2026

Copy link
Copy Markdown

@pengzh1 is attempting to deploy a commit to the trpc Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

httpBatchStreamLink now aborts a successful operation when its result data is not stream-capable. Tests cover cancellation of streamed queries batched with settled queries.

Changes: Abort cleanup

Layer / File(s) Summary
Result check and cancellation tests
packages/client/src/links/httpBatchStreamLink.ts, packages/client/src/links/httpAbortCleanup.test.ts
The link checks whether result data is an async iterable or contains a promise or async iterable. If not, it aborts the operation controller. Tests check server-side abort signals when a streamed query is cancelled in two batched-query cases.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related PRs

  • trpc/trpc#8056: Changes abort-signal cleanup behavior in httpBatchStreamLink, which this PR extends to successful operations with non-stream results.

Suggested reviewers: katt, taham8875

Merge Risk: Minimal · up to 1c8ac

The change aborts a finished query's own signal so it no longer holds a streamed batch open after another query in the batch is cancelled. No concrete merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the fix and its scope: a finished query no longer keeps a streamed batch open. The raw summary shows the change adds an isDataStream check in httpBatchStreamLink.ts that aborts the con…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title names the fix and its scope: a finished query no longer keeps a streamed batch open. The raw summary shows the change adds an isDataStream check in httpBatchStreamLink.ts that aborts the controller after a successful result without stream-capable data, and adds tests in httpAbortCleanup.test.ts for batches mixing a settled query with a streamed query. The title matches the main change, so the check passes.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • Create a new PR
  • 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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: trpc/trpc
  • Branch: main
  • File: .coderabbit.yaml
  • Commit: d3882f3
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a9f19433-314e-4c62-889e-8913ab322b48
Commits

Reviewing files that changed from the base of the PR and between d3882f3 and 1c8ac3f.

Files selected for processing: 2

  • packages/client/src/links/httpAbortCleanup.test.ts
  • packages/client/src/links/httpBatchStreamLink.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@trpc-bot trpc-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the leak, and the tests fail without it. A call that returns { a: Promise } still holds the batch open until its promise settles; we'll handle that in a follow-up.

@trpc-bot
trpc-bot added this pull request to the merge queue Nov 24, 2026
Merged via the queue into trpc:main with commit 3b6387e Nov 24, 2026
34 of 38 checks passed
opensource-fork-sync Bot pushed a commit to cla7aye15I4nd/opensource-trpc__trpc that referenced this pull request Nov 24, 2026
…ettle (trpc#8195)

## Why

- `httpBatchStreamLink` cancels a batch only after every call's signal
aborts.
- After trpc#8194, a call whose result holds deferred values (`{ a: Promise
}`, a nested async iterable) never released its signal, even once they
settled. So cancelling a streamed sibling left the request open and the
server running.
- Follows up trpc#8194 by @pengzh1.

## What

- The JSONL consumer notes which head key (call) each chunk belongs to.
It takes a new optional `onSettled({ key })`, called once every chunk
under a key has closed.
- The link aborts that call's signal then.
- This also covers plain results, so it replaces trpc#8194's `isDataStream`
check.
- A part that's still pending keeps its call's signal, so it isn't cut
off.

## Links

None

## Alternatives considered

- Walk the result in the link and await its promises: misses nested
values and async iterables, and duplicates what the consumer already
tracks.

## Testing

Regression check: `cancels a batch holding a settled query with deferred
values and a streamed query` and `keeps a batch open for a pending
deferred value after a streamed query is cancelled` in
`packages/client/src/links/httpAbortCleanup.test.ts`. Without the fix,
the server never sees the cancellation.

## AI model

Claude Opus 5.5

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- codesmith:footer -->
---
<a
href="https://dashboardbackend.blacksmith.sh/track/run-proof?expires=1798113206&installation_model_id=443568&pr_number=8195&ref=codesmith_pr_footer&repository=trpc%2Ftrpc&return_to=https%3A%2F%2Fgithub.com%2Ftrpc%2Ftrpc%2Fpull%2F8195&variant=proof_this_pr&signature=5006697306b944d3ecd54ec37862c82457d98afdcba901b4f1e60b0f45cdedf6"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://pr-comments-assets.blacksmith.sh/codesmith/proof-proof-this-pr-v2-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://pr-comments-assets.blacksmith.sh/codesmith/proof-proof-this-pr-v2-light.svg"><img
alt="Proof this PR"
src="https://pr-comments-assets.blacksmith.sh/codesmith/proof-proof-this-pr-v2-dark-3x.png"
width="121" height="28"></picture></a>
<sup><a
href="https://app.blacksmith.sh/trpc/codesmith/trpc/pr/8195?autoLogin=true&ref=codesmith_pr_footer">Open
in [code]smith</a> · Need help on this PR? Tag
<code>@codesmith-bot</code> with what you need.</sup>

<!-- codesmith:autofix:disabled -->
<!-- /codesmith:footer -->

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Bug Fixes**
* Improved handling of cancelled streams that share a batch with
deferred results. Completed results now settle independently, while
batches remain open for pending results until they finish. This helps
streamed queries and deferred responses complete more reliably when a
stream is cancelled, without prematurely ending work that is still
pending.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
KATT added a commit that referenced this pull request Nov 24, 2026
…ts result is there

Since #8194/#8195, an operation of a streamed batch lets go of it once
its own chunks close. A call's signal ended when the call settled, or
when its load stopped listening, which for a call for every item is
before its runs have streamed. So the request was cut off under them.

Now `httpBatchStreamLink`'s `settle()` ends a call's signal, as it does
an operation's, and a call that settled no longer ends it on teardown.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
KATT added a commit that referenced this pull request Nov 24, 2026
Brings in main up to #8195 (v11.31.2), with main's other
commits since the last sync: automatic releases, the App Router
examples, the /kitchen-sink showcase and the server and client fixes.

- Workflows: main.yml, lint.yml, release.yml and subtree.yml are main's,
  reapplied to the v11-* copies with v12's paths, working-directory and
  setup action. v11-ci and v11-lint keep their push trigger on v12, which
  has no PRs. The setup action takes main's `example` input, installing
  ./v11, ./v11/packages/* and the example.
- tooling/release: main's new git calls get --relative and <rev>:./ as
  before; pnpm-workspace.yaml is read as the repo root's.
- Catalogs merge in place. oxlint stays at v12's 1.90.0: main's 1.92
  can't load v12/vite.config.ts through Vite+ 1.1.0. v11's fastify
  importers resolve 5.12.8, as on main, so the v11 Fastify docs sample
  type-checks against one copy now that v11 emits with TypeScript 7.1.
- www: both kitchen sinks stay. unindexedPaths is /v12 and
  /kitchen-sink; prerenderFilter keeps v12's /v12/docs exclusion; the
  root keeps v12's banner and home-version script beside main's
  announcement bar. package-links.test.ts gets a 30 s timeout: main's
  new v11 sources take it past 5 s beside the site's other tests.
- Root package.json, AGENTS.md, .gitignore and the PR template stay
  v12's; main's changes go to v11/'s. Lint rules go to both
  .oxlintrc.json files.
- v11/ paths that differ from main on purpose: the packages' prepack
  path, the sponsor image URLs, sse.ts's hand-driven iterator,
  handles.ts's inferFeatureFlags, and tooling/release's git paths.

v12/ ports, in later commits:
- #8195, #8194: a batched call's deferred values outlive a sibling's
  abort, and aborting a streamed call in a batch stops it.
- #8027: localLink's createContext gets the call's abort signal.
- #7935, #7936: an SSE error event or errorFormatter that throws ends
  the stream and is reported, in the v11 handler and native SSE.
- #8146: the v11 handler's SSE writes `data:` for undefined.
- #8126: FormData conversion errors in actions go through onError, and
  the bracket parser replaces a string leaf under an array index.
- #8139: an empty pre-parsed JSON body is no input.
- #8152, #8141: OpenAPI tuples with rest or optional elements.
Left for a human: #8150 (retry a failed lazy load) reverses
lazy-routers-on-effect.md; #7927 (an unreadable wsLink frame) would
replace v12's skip-and-continue.
Not needed: the rest, including the four security advisories, which
v12's bracket parser, input handling and upgrade path already cover.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
KATT added a commit that referenced this pull request Nov 24, 2026
Aborting a streamed call after its head was ignored in a batch: its
values kept streaming and the server kept running its generator, since
the batch's request only closed when every caller stopped waiting before
an answer.

A batch head now says `deferred: true` when chunks follow, and the server
ends each such call with `{ i }`, as sockets' `chunkEnd`. A call holds
the batch until it has nothing left to stream or its caller aborts, which
fails its open values with CLIENT_CLOSED_REQUEST. Once a caller has left
and every call is released, the request aborts, so a sibling's deferred
values still stream to the end first.

Port of #8194 and #8195.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

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 25, 2026

This branch was successfully deployed

4 active deployments
Preview – www — 1c8ac3f3 Deployed Nov 24, 2026 by vercel[bot]
Preview – trpc-next-app-dir — 1c8ac3f3 Deployed Nov 24, 2026 by vercel[bot]
Preview – next-prisma-starter — 1c8ac3f3 Deployed Nov 24, 2026 by vercel[bot]
Preview – examples-todomvc — 1c8ac3f3 Deployed Nov 24, 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