Skip to content

fix(client): don't flush a later WebSocket request onto a connecting socket - #7911

Merged
KATT merged 1 commit into
mainfrom
cursor/wsclient-flush-after-batch-failure-3334
Nov 4, 2026
Merged

KATT merged 1 commit into
mainfrom
cursor/wsclient-flush-after-batch-failure-3334

Conversation

@KATT

@KATT KATT commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Why

What

  • batchSend: after sleep(0), if the socket isn't open and this run's own request was already sent, return without flushing. Each queued request's own run sends it once the new socket opens.
  • New RequestManager.isOutgoing(message) checks whether a message is still queued.
  • Batching on an open socket is unchanged. No public API or wire format changes.

Links

None

Alternatives considered

  • Flush only when the run's own request is still queued: it changes batching on an open socket.
  • Return whenever the socket isn't open: in lazy mode, a batch whose socket closed during the sleep would hang.

Testing

Both tests in websockets.flushAfterBatchFailure.test.ts fail on main 472e6ba with Active connection is not open in 30/30 runs, and pass with the fix.

AI model

Claude Opus 5.5


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 WebSocket request handling when a connection closes during a batch. Requests affected by the interruption now fail cleanly, while requests made after reconnection can proceed successfully.
    • Added coverage for connection failures and recovery across concurrent requests.

…socket

After a batch fails because its socket closed, a sibling request's flush
can still be waiting on its sleep(0) timer. When it fires, it sent requests
queued for the new socket while that socket was still connecting, failing
them with "Active connection is not open". A run whose own request was
already sent now leaves the queue alone when the socket isn't open; each
queued request's own run sends it once the socket opens.

Co-authored-by: Alex / KATT <[email protected]>
@KATT
KATT requested a review from a team as a code owner November 4, 2026 18:29
@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 4, 2026 6:30pm UTC
examples-todomvc Ready Ready Preview Nov 4, 2026 6:30pm UTC
next-prisma-starter Ready Ready Preview Nov 4, 2026 6:30pm UTC
next-prisma-websockets-starter Ready Ready Preview Nov 4, 2026 6:30pm UTC
tanstack-start-sse-chat Ready Ready Preview Nov 4, 2026 6:30pm UTC
trpc-next-app-dir Ready Ready Preview Nov 4, 2026 6:30pm UTC
www Ready Ready Preview Nov 4, 2026 6:30pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The WebSocket client now checks whether a message remains in the outgoing queue after an asynchronous yield. New tests cover socket termination, rejected queries, reconnection, and successful subsequent queries.

Changes: WebSocket batch failure handling

Layer / File(s) Summary
Outgoing batch guard and regression coverage
packages/client/src/links/wsLink/wsClient/requestManager.ts, packages/client/src/links/wsLink/wsClient/wsClient.ts, packages/tests/server/websockets.flushAfterBatchFailure.test.ts
RequestManager.isOutgoing checks queue membership by message identity. After yielding, batchSend returns if the connection is closed and the message is no longer queued. Tests cover query failures and subsequent queries after reconnection.

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

Change: Bug fix

Possibly related PRs

  • trpc/trpc#7725: Both PRs change WebSocket batch sending. This PR handles a message that another batch run has already sent; #7725 addresses encoding and transport send failures.
  • trpc/trpc#7889: Both PRs use exact message identity in RequestManager to distinguish queued messages.

Suggested reviewers: juliusmarminge

Merge Risk: Minimal · up to 93afc

The guard prevents stale flush runs from failing newer queued requests. No concrete 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 names the main change: a fix in the client that stops a later WebSocket request from being flushed onto a socket that is still connecting. The raw summary shows the change in wsClient.batchS…
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: Title check

Explanation

The title names the main change: a fix in the client that stops a later WebSocket request from being flushed onto a socket that is still connecting. The raw summary shows the change in wsClient.batchSend and RequestManager.isOutgoing, and the objectives describe the same race. The title is specific and matches the changeset.

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 3 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 472e6ba and 93afc4c.

Files selected for processing: 3
  • packages/client/src/links/wsLink/wsClient/requestManager.ts
  • packages/client/src/links/wsLink/wsClient/wsClient.ts
  • packages/tests/server/websockets.flushAfterBatchFailure.test.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.

@pkg-pr-new

pkg-pr-new Bot commented Nov 4, 2026

Copy link
Copy Markdown

Open in StackBlitz

@trpc/client

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

@trpc/next

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

@trpc/openapi

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

@trpc/react-query

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

@trpc/server

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

@trpc/tanstack-react-query

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

@trpc/upgrade

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

commit: 93afc4c

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

🤖 Approved; Alex approved this fix.

@KATT
KATT added this pull request to the merge queue Nov 4, 2026
Merged via the queue into main with commit 8aa4487 Nov 4, 2026
53 of 54 checks passed
@KATT
KATT deleted the cursor/wsclient-flush-after-batch-failure-3334 branch November 4, 2026 18:51
@github-actions

github-actions Bot commented Nov 6, 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 6, 2026

This branch was successfully deployed

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

3 participants