Skip to content

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

Open
contactjawad wants to merge 1 commit into
TanStack:mainfrom
contactjawad:fix-query-dispatch-observer-skip
Open

fix(query-core): notify sibling observers when one unsubscribes during dispatch#11231
contactjawad wants to merge 1 commit 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.
  • Tests

    • Added regression coverage for observer notifications during subscription changes.

…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: 0f3c8a02-30f4-4300-b0f2-b1e45ac56bfd

📥 Commits

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

📒 Files selected for processing (3)
  • .changeset/fix-query-dispatch-observer-skip.md
  • packages/query-core/src/__tests__/queryObserver.test.tsx
  • packages/query-core/src/query.ts

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 76bf8

The change prevents a sibling observer from being skipped when another observer unsubscribes during notification, with a focused regression test covering 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.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the change, root cause, fix, and regression test, but it omits the required Checklist and Release Impact sections. Add the required Checklist and Release Impact sections, and record the applicable checklist selections.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the query-core fix for notifying sibling observers during dispatch.
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.

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.

1 participant