Skip to content

fix(server): delete stale clientSubscriptions entry on subscription task rejection - #7401

Merged
Nick-Lucas merged 1 commit into
trpc:mainfrom
Adit-Jain-srm:fix/7400-stale-client-subscriptions
Sep 12, 2026
Merged

Nick-Lucas merged 1 commit into
trpc:mainfrom
Adit-Jain-srm:fix/7400-stale-client-subscriptions

Conversation

@Adit-Jain-srm

@Adit-Jain-srm Adit-Jain-srm commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #7400 β€” stale clientSubscriptions entry after subscription task rejection in WebSocket adapter.

Problem

In packages/server/src/adapters/ws.ts, when the subscription async task rejects (e.g. iterator.return() throws during await using disposal), the .catch() handler aborts the controller and sends an error response but never calls clientSubscriptions.delete(id). This leaves a stale AbortController in the Map for the connection's lifetime, preventing subscription ID reuse.

The happy path (line 407) correctly calls clientSubscriptions.delete(id) β€” only the error path is missing it.

Fix

Add clientSubscriptions.delete(id) as the first line of the .catch() handler, mirroring the cleanup on the happy path.

Changes

  • packages/server/src/adapters/ws.ts: +1 line in the subscription task .catch() handler

Summary by CodeRabbit

  • Bug Fixes
    • Improved WebSocket subscription cleanup to ensure resources are properly released even when errors occur during subscription processing.

@Adit-Jain-srm
Adit-Jain-srm requested a review from a team as a code owner June 1, 2026 19:23
@vercel

vercel Bot commented Jun 1, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
next-prisma-starter Ready Ready Preview Sep 12, 2026 1:14pm UTC
og-image Ready Ready Preview Sep 12, 2026 1:14pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

πŸ“ Walkthrough

Walkthrough

This PR fixes a resource leak in the WebSocket subscription adapter where stale entries could accumulate in the clientSubscriptions Map when subscription iteration or response handling fails. The fix adds a missing cleanup call to the error handler path.

Changes

Subscription Error Handler Cleanup

Layer / File(s) Summary
Subscription error handler cleanup
packages/server/src/adapters/ws.ts
Added clientSubscriptions.delete(id) as the first statement in the .catch() handler following the subscription streaming loop, mirroring the cleanup already present on the normal-exit path and ensuring entries are removed even when iterator disposal or response transmission rejects.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • KATT
  • Nick-Lucas
πŸš₯ Pre-merge checks | βœ… 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
βœ… Passed checks (4 passed)
Check name Status Explanation
Title check βœ… Passed The title clearly describes the main change: fixing a stale clientSubscriptions entry deletion issue on subscription task rejection in the server WebSocket adapter.
Description check βœ… Passed The PR description comprehensively covers the problem, root cause, and fix with clear section organization matching expected standards for a bug fix.
Linked Issues check βœ… Passed The code change directly addresses issue #7400 by adding clientSubscriptions.delete(id) in the .catch() handler, exactly mirroring the proposed fix and preventing stale Map entries.
Out of Scope Changes check βœ… Passed The single-line addition in the subscription task .catch() handler is narrowly scoped and directly addresses the linked issue with no extraneous changes.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
πŸ§ͺ Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented Jun 1, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@trpc/client

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

@trpc/next

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

@trpc/openapi

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

@trpc/react-query

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

@trpc/server

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

@trpc/tanstack-react-query

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

@trpc/upgrade

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

commit: f692a2a

@Adit-Jain-srm

Copy link
Copy Markdown
Contributor Author

The failing test is in packages/upgrade/test/__fixtures__/hooks/optimistic-update.spec.tsx (the @trpc/upgrade package) - unrelated to this PR's single-line change in packages/server/src/adapters/ws.ts.

The test expects Posts: 1initial but receives Posts: 2initialFoo, which looks like a timing/race condition in the optimistic update test (the mutation resolves before the assertion runs). This failure reproduces with retries (x2), suggesting it's a known flaky test rather than a regression from our change.

Could a maintainer re-run CI or confirm this is pre-existing? Happy to rebase if needed.

…ask rejection

When the subscription async task rejects (e.g. iterator.return() throws during

disposal), the .catch() handler aborts the controller and sends an error

response but never removes the entry from clientSubscriptions. This leaves a

stale AbortController in the Map for the connection's lifetime, preventing

subscription ID reuse.

Fixes trpc#7400
@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 Sep 13, 2026

This branch had an error being deployed

1 failed and 4 active deployments
Preview – www β€” f692a2aa Deployed Sep 12, 2026 by vercel[bot]
Preview – trpc-next-app-dir β€” f692a2aa Deployed Sep 12, 2026 by vercel[bot]
Preview – examples-todomvc β€” f692a2aa Deployed Sep 12, 2026 by vercel[bot]
Preview – next-prisma-starter β€” f692a2aa Deployed Sep 12, 2026 by vercel[bot]
Preview – og-image β€” f692a2aa Deployed Sep 12, 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.

ws: subscription task rejection can leave stale clientSubscriptions entry

2 participants