Skip to content

fix(query-core): notify sibling observers when one unsubscribes during dispatch - #11231

Closed
contactjawad wants to merge 3 commits into
TanStack:mainfrom
contactjawad:fix-query-dispatch-observer-skip
Closed

fix(query-core): notify sibling observers when one unsubscribes during dispatch#11231
contactjawad wants to merge 3 commits into
TanStack:mainfrom
contactjawad:fix-query-dispatch-observer-skip

Conversation

@contactjawad

@contactjawad contactjawad commented Aug 19, 2026

Copy link
Copy Markdown

Summary

Query.#dispatch can skip a still-subscribed observer when another observer on the same query unsubscribes during notification, leaving it with a stale pending result after the query has already resolved.

Root cause

removeObserver mutates the observer array in place (this.observers.splice(index, 1)). #dispatch iterates that same live array with this.observers.forEach(...) and calls onQueryUpdate() synchronously inside the open notifyManager batch. If a listener unsubscribes a same-query observer during that loop, an element is spliced out mid-iteration and forEach skips the next observer.

Fix

Iterate over a snapshot ([...this.observers]) so an observer unsubscribing during notification can't cause a sibling to be skipped.

Test

Added a regression test in queryObserver.test.tsx: two observers subscribe to the same query; observer A unsubscribes itself when it first sees success, and the test asserts observer B still receives the resolved success data. It fails on main (expected 'pending' to be 'success') and passes with this change. A changeset is included.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed query updates being skipped for some observers when another observer unsubscribes during notification.
    • Ensured all still-subscribed observers receive successful query results reliably, including when subscriptions change during an update.
  • Tests

    • Added regression coverage for observer notifications during subscription changes and asynchronous query completion.

…g dispatch

Query.#dispatch iterated the live observers array while onQueryUpdate() could
synchronously splice it (via removeObserver). When one observer unsubscribed a
same-query sibling during notification, forEach skipped the next observer,
leaving a still-subscribed observer with a stale `pending` result after the
query had resolved. Iterate over a snapshot of the observers instead.
@coderabbitai

coderabbitai Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 2342084c-e7a6-4158-b876-40f872a43816

📥 Commits

Reviewing files that changed from the base of the PR and between 76bf8ef and 4f220aa.

📒 Files selected for processing (1)
  • packages/query-core/src/__tests__/queryObserver.test.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/query-core/src/tests/queryObserver.test.tsx

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


📝 Walkthrough

Walkthrough

The query dispatch loop now iterates over an observer snapshot. A regression test covers observer unsubscription during dispatch, and a patch changeset documents the fix.

Changes

Query observer dispatch

Layer / File(s) Summary
Snapshot-based observer notification
packages/query-core/src/query.ts, packages/query-core/src/__tests__/queryObserver.test.tsx, .changeset/fix-query-dispatch-observer-skip.md
Query.#dispatch notifies observers from a snapshot. The regression test verifies that a sibling observer receives successful data after another observer unsubscribes. The changeset records the patch release.

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

Merge Risk: ⚪ Minimal · up to 4f220

This change snapshots observers during query notifications so unsubscribing one observer cannot prevent another from receiving the resolved result; no actionable merge-blocking risk remains beyond normal checks and review.

Possibly related PRs

  • TanStack/query#11214: Both changes modify observer-array handling in packages/query-core/src/query.ts.

Suggested reviewers: tkdodo

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely identifies the observer notification bug and its fix.
Description check ✅ Passed The description clearly explains the problem, root cause, fix, regression test, and changeset, but omits the template checklist and release impact sections.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@nx-cloud

nx-cloud Bot commented Aug 19, 2026

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 3992b53

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ⛔ Cancelled 42s View ↗

☁️ Nx Cloud last updated this comment at 2026-08-19 19:20:44 UTC

@TkDodo

TkDodo commented Aug 20, 2026

Copy link
Copy Markdown
Collaborator

@TkDodo TkDodo closed this Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants