Skip to content

fix(server): stop shared observables from skipping subscribers that tear down synchronously - #7741

Closed
MindOfqc wants to merge 2 commits into
trpc:mainfrom
MindOfqc:fix/shared-observable-skip-subscribers
Closed

MindOfqc wants to merge 2 commits into
trpc:mainfrom
MindOfqc:fix/shared-observable-skip-subscribers

Conversation

@MindOfqc

@MindOfqc MindOfqc commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Shared observables iterate their live subscriber list while delivering an event, but a subscriber tears itself down synchronously as soon as it receives one: observable() runs the teardown after error/complete, and a subscriber may also unsubscribe inside next (that is exactly what observableToPromise does). The teardown splices the subscriber out of the list, the for...of loop moves on to the element that shifted into the slot it already visited, and the next subscriber is skipped.

share()

https://github.com/trpc/trpc/blob/main/packages/server/src/observable/operators.ts#L52-L64

  • the source completes: the second subscriber never receives complete, so e.g. observableToPromise never settles,
  • the source errors: the second subscriber is never notified and stays subscribed forever,
  • one subscriber unsubscribes in next: every following subscriber silently misses that value.

share() backs TRPCUntypedClient.$request and dedupeLink, and it is public API via @trpc/server/observable.

behaviorSubject

https://github.com/trpc/trpc/blob/main/packages/server/src/observable/behaviorSubject.ts#L44-L48

Same root cause in next(): an observer that unsubscribes inside next causes its neighbour to miss the value. behaviorSubject carries the connection state in httpSubscriptionLink and the wsClient.

Fix

Iterate over a snapshot of the subscriber list in both places, so a synchronous teardown cannot shift the remaining subscribers out of the loop.

Tests

  • operators.test.ts: two subscribers on completion, two on error, and a subscriber that unsubscribes inside next.
  • behaviorSubject.test.ts: two observers, the first unsubscribes inside next.

Red (main sources + new tests): packages/server/src/observable/ β†’ Tests 4 failed | 11 passed (15) β€” e.g. expected "vi.fn()" to be called 1 times, but got 0 times and expected [ 0 ] to deeply equal [ 0, 1 ].

Green (patched): packages/server/src/observable/ + dedupeLink + clientInternals + links β†’ Tests 47 passed (47) across 6 files.

Prepared with an AI coding agent.

@MindOfqc
MindOfqc requested a review from a team as a code owner October 29, 2026 01:48
@vercel

vercel Bot commented Oct 29, 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 Oct 29, 2026 1:52am UTC
og-image Ready Ready Preview Oct 29, 2026 1:52am UTC

Request Review

@vercel

vercel Bot commented Oct 29, 2026

Copy link
Copy Markdown

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

A member of the Team first needs to authorize it.

@MindOfqc
MindOfqc force-pushed the fix/shared-observable-skip-subscribers branch from c556b1a to 60c358d Compare October 29, 2026 01:48
@MindOfqc MindOfqc changed the title fix(server): stop share() from skipping subscribers that tear down synchronously fix(server): stop shared observables from skipping subscribers that tear down synchronously Oct 29, 2026
@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack β†’

Caution

Review failed

The pull request is closed.

Finishing Touches
πŸ§ͺ Generate unit tests (beta)
  • 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

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

@MindOfqc

Copy link
Copy Markdown
Contributor Author

Superseded by #7729 for the share() part β€” upstream now snapshots the observer set and skips observers cancelled during an emission. I'll send the behaviorSubject half of this fix as its own PR against the current main.

@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 Oct 30, 2026

This branch was successfully deployed

5 active deployments
Preview – www β€” a6372873 Deployed Oct 29, 2026 by vercel[bot]
Preview – examples-todomvc β€” a6372873 Deployed Oct 29, 2026 by vercel[bot]
Preview – og-image β€” a6372873 Deployed Oct 29, 2026 by vercel[bot]
Preview – next-prisma-starter β€” a6372873 Deployed Oct 29, 2026 by vercel[bot]
Preview – trpc-next-app-dir β€” a6372873 Deployed Oct 29, 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.

1 participant