Skip to content

fix(server): let mergeAsyncIterables release the last buffered value - #7932

Merged
KATT merged 2 commits into
mainfrom
cursor/merge-async-iterables-release-b082
Nov 18, 2026
Merged

KATT merged 2 commits into
mainfrom
cursor/merge-async-iterables-release-b082

Conversation

@KATT

@KATT KATT commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Why

  • mergeAsyncIterables, used by JSONL streaming (httpBatchStreamLink, streamed responses), keeps the last value it yielded alive until the next one arrives. While the generator awaits, V8 keeps the loop body's last const alive, so a large final chunk stays reachable as long as the stream is idle.

What

  • The buffer entry is held in a let outside the loop and cleared once the consumer has taken the value. Behaviour is otherwise unchanged.

Stack

Each PR is based on the one below it, so its diff shows only its own commits. Merge from the bottom up.

  1. fix(server): let mergeAsyncIterables release the last buffered value #7932 (this PR): mergeAsyncIterables lets go of its last value, based on main
  2. chore(server): add a pure state machine primitive to unstable internals #7930: the state machine primitives
  3. refactor(client): rewrite the wsLink client as state machines #7925: the wsLink client rewritten as state machines

Links

None

Alternatives considered

None

Testing

  • Regression check: mergeAsyncIterables.test.ts "lets go of the last yielded value while it waits for the next" fails on main (expected false to be true: a 50 MB value is still reachable after gc()) and passes with this change.

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.

Summary by CodeRabbit

  • Bug Fixes
    • Improved memory handling in merged streams so consumed buffer data can be released while the iterator waits for another value.
    • Errors encountered during iteration continue to be propagated, and waiting iterators can resume with subsequent values.

@KATT
KATT requested a review from a team as a code owner November 4, 2026 23:57
@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
examples-next-minimal-starter Ready Ready Preview Nov 11, 2026 2:01pm UTC
examples-todomvc Ready Ready Preview Nov 11, 2026 2:01pm UTC
next-prisma-starter Ready Ready Preview Nov 11, 2026 2:01pm UTC
next-prisma-websockets-starter Ready Ready Preview Nov 11, 2026 2:01pm UTC
tanstack-start-sse-chat Ready Ready Preview Nov 11, 2026 2:01pm UTC
trpc-next-app-dir Ready Ready Preview Nov 11, 2026 2:01pm UTC
www Ready Ready Preview Nov 11, 2026 2:01pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration

Commits

Reviewing files that changed from the base of the PR and between a9f89a6 and 7e80272.

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

No actionable comments were generated in the recent review.

Walkthrough

The merge loop now clears a yielded item reference after the consumer resumes and throws buffered errors before accessing yielded values. A new test checks that a large yielded array can be collected while the merged iterator waits for another value.

Changes: Merge reference cleanup

Layer / File(s) Summary
Handle and release buffered items
packages/server/src/unstable-core-do-not-import/stream/utils/mergeAsyncIterables.ts, packages/server/src/unstable-core-do-not-import/stream/utils/mergeAsyncIterables.test.ts
The merge loop checks buffered errors before yielding, pulls the source iterator after resumption, and clears the item reference. A test checks collection of a large yielded array while the merged iterator waits.

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

Change: Bug fix

Suggested reviewers: nick-lucas

Merge Risk: Minimal · previous review 04229

The change releases the previously yielded reference while awaiting another value and adds regression coverage. No merge-blocking issue remains established.

Pre-merge checks: Passed 5

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: allowing mergeAsyncIterables to release the last buffered value.
Docstring Coverage Passed No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
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.
Full details: Docstring Coverage

Explanation

No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 2 files.

Finishing Touches

Generate docstrings

  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)

  • Commit to this branch
  • 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

Commits

Reviewing files that changed from the base of the PR and between 33ee2ba and 0422982.

Files selected for processing: 2

  • packages/server/src/unstable-core-do-not-import/stream/utils/mergeAsyncIterables.test.ts
  • packages/server/src/unstable-core-do-not-import/stream/utils/mergeAsyncIterables.ts

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

@pkg-pr-new

pkg-pr-new Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@trpc/client

npm i https://pkg.pr.new/@trpc/client@7932

@trpc/next

npm i https://pkg.pr.new/@trpc/next@7932

@trpc/openapi

npm i https://pkg.pr.new/@trpc/openapi@7932

@trpc/react-query

npm i https://pkg.pr.new/@trpc/react-query@7932

@trpc/server

npm i https://pkg.pr.new/@trpc/server@7932

@trpc/tanstack-react-query

npm i https://pkg.pr.new/@trpc/tanstack-react-query@7932

@trpc/upgrade

npm i https://pkg.pr.new/@trpc/upgrade@7932

commit: 7e80272

While the merged generator waits for the next value, V8 keeps the const
destructured from the last buffer entry alive, and with it the last value
yielded. Hold the entry in a let outside the loop and clear it after use.

Co-authored-by: Cursor Agent <[email protected]>
Co-authored-by: alexander <[email protected]>

@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.

Holding the buffer entry in a let and clearing it after use lets the last yielded value be collected while the stream waits. Regression test fails on main, passes here. CI green.

@KATT
KATT added this pull request to the merge queue Nov 17, 2026
Merged via the queue into main with commit a670737 Nov 18, 2026
52 checks passed
@KATT
KATT deleted the cursor/merge-async-iterables-release-b082 branch November 18, 2026 00:24
@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 19, 2026

This branch was successfully deployed

5 active deployments
Preview – www — 7e80272b Deployed Nov 11, 2026 by vercel[bot]
Preview – next-prisma-starter — 7e80272b Deployed Nov 11, 2026 by vercel[bot]
Preview – examples-todomvc — 7e80272b Deployed Nov 11, 2026 by vercel[bot]
Preview – examples-next-minimal-starter — 7e80272b Deployed Nov 11, 2026 by vercel[bot]
Preview – trpc-next-app-dir — 7e80272b Deployed Nov 11, 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.

4 participants