Skip to content

fix(query-core): Keep observer notifications stable - #11234

Merged
TkDodo merged 1 commit into
TanStack:mainfrom
scttcper:scttcper/observer-notification-test
Aug 20, 2026
Merged

fix(query-core): Keep observer notifications stable#11234
TkDodo merged 1 commit into
TanStack:mainfrom
scttcper:scttcper/observer-notification-test

Conversation

@scttcper

@scttcperscttcper commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

PR #11214 changed observer removal from filter() to splice(). That made removing lots of observers much faster, but it also allowed an observer to change the same array while a query was notifying it. If an observer unsubscribed synchronously, the next observer could be skipped and left with stale data.

This notifies a shallow copy of the observer array. The removal path still uses splice(), so the performance improvement from #11214 stays.

I ran a local Vitest benchmark that calls setQueryData on one query with all observers subscribed:

ObserversBeforeAfter
100.0027 ms0.0027 ms
500.0096 ms0.0101 ms
10,0002.6049 ms2.6032 ms

There isn't a meaningful change in notification time. The key change and unmount paths measured in #11214 are unchanged because this does not change observer removal.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed query updates so all remaining observers are notified correctly when another observer unsubscribes during notification.
    • Ensured observers receive the latest query data consistently during synchronous updates.
  • Tests

    • Added regression coverage for observer notifications during unsubscribe scenarios.
  • Release

    • Included in a patch release of @tanstack/query-core.

Removing an observer in place can shift the active notification loop and skip the next observer. Iterate over a shallow copy so synchronous unsubscriptions keep the old behavior without giving up the faster removal path from TanStack#11214.
Co-Authored-By: Codex <noreply@openai.com>
@coderabbitai

coderabbitaiBot commented Aug 20, 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: b133350a-a59a-4e5c-a4be-d39fc870d97e

📥 Commits

Reviewing files that changed from the base of the PR and between 64a8035 and 131504a.

📒 Files selected for processing (3)
  • .changeset/stable-observers-notify.md
  • packages/query-core/src/__tests__/query.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 path now snapshots observers before notification. A regression test verifies that remaining observers receive updated data after one observer unsubscribes synchronously. A patch changeset documents the fix.

Changes

Observer notification stability

Layer / File(s)Summary
Stable dispatch and regression coverage
packages/query-core/src/query.ts, packages/query-core/src/__tests__/query.test.tsx, .changeset/stable-observers-notify.md
Query.#dispatch iterates over a snapshot of observers. The regression test verifies notification of the remaining observer after synchronous unsubscription. The changeset declares a patch release.
Estimated code review effort: 2 (Simple)~10 minutes

Merge Risk:⚪ Minimal · up to 13150

This localized change stabilizes observer notifications while preserving the existing removal performance improvement, and no actionable merge-blocking risk remains after normal checks and review.

Possibly related PRs

Suggested reviewers:tkdodo

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check nameStatusExplanation
Title check✅ PassedThe title clearly describes the main change: stabilizing query observer notifications during synchronous unsubscription.
Description check✅ PassedThe description explains the bug, fix, performance impact, and benchmark results, but it omits the template checklist and release-impact headings.
Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check✅ PassedCheck skipped because no linked issues were found for this pull request.
Out of Scope Changes check✅ PassedCheck 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-cloudBot commented Aug 20, 2026

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 131504a

CommandStatusDurationResult
nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded7m 45sView ↗
nx run-many --target=build --exclude=examples/*...✅ Succeeded44sView ↗

☁️ Nx Cloud last updated this comment at 2026-08-20 08:07:42 UTC

@pkg-pr-new

Copy link
Copy Markdown
More templates

@tanstack/angular-query-experimental

npm i https://pkg.pr.new/@tanstack/angular-query-experimental@11234

@tanstack/eslint-plugin-query

npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@11234

@tanstack/lit-query

npm i https://pkg.pr.new/@tanstack/lit-query@11234

@tanstack/preact-query

npm i https://pkg.pr.new/@tanstack/preact-query@11234

@tanstack/preact-query-devtools

npm i https://pkg.pr.new/@tanstack/preact-query-devtools@11234

@tanstack/preact-query-persist-client

npm i https://pkg.pr.new/@tanstack/preact-query-persist-client@11234

@tanstack/query-async-storage-persister

npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@11234

@tanstack/query-broadcast-client-experimental

npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@11234

@tanstack/query-core

npm i https://pkg.pr.new/@tanstack/query-core@11234

@tanstack/query-devtools

npm i https://pkg.pr.new/@tanstack/query-devtools@11234

@tanstack/query-persist-client-core

npm i https://pkg.pr.new/@tanstack/query-persist-client-core@11234

@tanstack/query-sync-storage-persister

npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@11234

@tanstack/react-query

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

@tanstack/react-query-devtools

npm i https://pkg.pr.new/@tanstack/react-query-devtools@11234

@tanstack/react-query-next-experimental

npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@11234

@tanstack/react-query-persist-client

npm i https://pkg.pr.new/@tanstack/react-query-persist-client@11234

@tanstack/solid-query

npm i https://pkg.pr.new/@tanstack/solid-query@11234

@tanstack/solid-query-devtools

npm i https://pkg.pr.new/@tanstack/solid-query-devtools@11234

@tanstack/solid-query-persist-client

npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@11234

@tanstack/svelte-query

npm i https://pkg.pr.new/@tanstack/svelte-query@11234

@tanstack/svelte-query-devtools

npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@11234

@tanstack/svelte-query-persist-client

npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@11234

@tanstack/vue-query

npm i https://pkg.pr.new/@tanstack/vue-query@11234

@tanstack/vue-query-devtools

npm i https://pkg.pr.new/@tanstack/vue-query-devtools@11234

commit: 131504a

@TkDodo
TkDodo merged commit 9656dc4 into TanStack:mainAug 20, 2026
9 checks passed
@github-actionsgithub-actionsBot mentioned this pull request Aug 20, 2026
Sign up for freeto 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

@scttcper@TkDodo