fix(query-core): fix queriesObserver result and observer length mismatch #9973
main fromrefs/pull/9973/head·merged by TkDodo View on GitHub🎯 Changes#
In 1b54538, setOptions was changed to call notifyListeners which eventually leads to QueriesObserver.setQueries
- Updating
#observerMatches - Calling
setOptionswhich leads to#notifybeing called - Updating
#result
This is an issue when #notify is called before the #result is updated, as #trackResult assumes these two are the same length, which may not be the case if the number of queries changed. When the length increases, observerResult becomes undefined and the function throws an exception.
Fix by moving the #observerMatches assignment later in the function, next to the #observers and #result assignment. I don't know the full nuances of this code so please advise if there's a better solution.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested this code locally with
pnpm run test:pr.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Summary by CodeRabbit#
- Bug Fixes
- Fixed a race condition in useQueries that occurred when the number of queries changed, ensuring proper observer state updates.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: query-core
Walkthrough#
This PR patches
@tanstack/query-coreto fix a race condition in useQueries when the number of queries changes. The fix modifies whenobserverMatchesis updated in queriesObserver.ts—shifting from unconditional assignment to conditional assignment only during structural changes.Changes#
Cohort / File(s) Change Summary Changeset documentation
.changeset/orange-flowers-post.mdAdded patch version bump entry for @tanstack/query-coredescribing race condition fix in useQueries (#9971)Core observer fix
packages/query-core/src/queriesObserver.tsMoved observerMatchesupdate from unconditional assignment to conditional assignment within structural-change branch, ensuring observer matches only updates during actual structural changesEstimated code review effort#
🎯 2 (Simple) | ⏱️ ~15 minutes
- The code change itself is focused and minimal (one assignment relocation)
- Verify the race condition scenario is properly addressed by the conditional timing change
- Ensure no regressions in non-structural-change update paths
Possibly related PRs#
- TanStack/query#9639: Also modifies queriesObserver.ts to adjust observer matching and structural-change detection logic timing
Suggested labels#
package: query-coreSuggested reviewers#
- TkDodo
Poem#
🐰 Queries raced ahead with hearts so wild, Timing stumbled, observers beguiled, But now they sync when structure does sway, No more race conditions will ruin the day! ✨
Pre-merge checks and finishing touches#
✅ Passed checks (3 passed)
Check name Status Explanation Title check ✅ Passed The title accurately summarizes the main fix: addressing a mismatch between queriesObserver result and observer length. Description check ✅ Passed The description follows the template with complete sections: detailed explanation of the issue, full checklist completion, and proper release impact marking. Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. ✨ Finishing touches
- 📝 Generate docstrings
🧪 Generate unit tests (beta)
- Create PR with unit tests
- Post copyable unit tests in a comment
📜 Recent review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📥 Commits
Reviewing files that changed from the base of the PR and between f15b7fcc01e995ab8835f1b1cc82ebb472c1ff64 and 13e69b0faa5a8b4905e2cde5fd3a754d40f722be.
📒 Files selected for processing (2)
.changeset/orange-flowers-post.md(1 hunks)packages/query-core/src/queriesObserver.ts(1 hunks)
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: sukvvon Repo: TanStack/query PR: 9892 File: packages/solid-query-persist-client/src/__tests__/PersistQueryClientProvider.test.tsx:331-335 Timestamp: 2025-11-22T09:06:05.219Z Learning: In TanStack/query test files, when a queryFn contains side effects (e.g., setting flags for test verification), prefer async/await syntax for clarity; when there are no side effects, prefer the .then() pattern for conciseness.Learnt from: TkDodo Repo: TanStack/query PR: 9612 File: packages/query-async-storage-persister/src/asyncThrottle.ts:0-0 Timestamp: 2025-09-02T17:57:33.184Z Learning: When importing from tanstack/query-core in other TanStack Query packages like query-async-storage-persister, a workspace dependency "tanstack/query-core": "workspace:*" needs to be added to the package.json.📚 Learning: 2025-11-22T09:06:05.219Z
Learnt from: sukvvon Repo: TanStack/query PR: 9892 File: packages/solid-query-persist-client/src/__tests__/PersistQueryClientProvider.test.tsx:331-335 Timestamp: 2025-11-22T09:06:05.219Z Learning: In TanStack/query test files, when a queryFn contains side effects (e.g., setting flags for test verification), prefer async/await syntax for clarity; when there are no side effects, prefer the .then() pattern for conciseness.Applied to files:
.changeset/orange-flowers-post.md
📚 Learning: 2025-09-02T17:57:33.184Z
Learnt from: TkDodo Repo: TanStack/query PR: 9612 File: packages/query-async-storage-persister/src/asyncThrottle.ts:0-0 Timestamp: 2025-09-02T17:57:33.184Z Learning: When importing from tanstack/query-core in other TanStack Query packages like query-async-storage-persister, a workspace dependency "tanstack/query-core": "workspace:*" needs to be added to the package.json.Applied to files:
.changeset/orange-flowers-post.md
🔇 Additional comments (2)
.changeset/orange-flowers-post.md (1)
1-5: LGTM!The changeset correctly describes the fix and appropriately uses a patch version bump for this bug fix.
packages/query-core/src/queriesObserver.ts (1)
136-136: Fix correctly addresses the race condition.Moving the
#observerMatchesassignment inside thehasStructuralChangeblock ensures that whensetOptions(line 112) triggers an early notify via a listener, both#observerMatchesand#resultstill have matching lengths (both reflecting the old state). This prevents the index-out-of-bounds error in#trackResultthat occurred when#observerMatcheswas updated unconditionally before thesetOptionscalls.
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.
Comment `@coderabbitai help` to get the list of available commands and usage tips.View your CI Pipeline Execution ↗ for commit c3fc4d80bcbc201e4ab2fdda3df6a2a338262a58
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 1m 5s View ↗
☁️ Nx Cloud last updated this comment at
2025-12-30 14:28:22UTCMore templates
- @tanstack/query-example-angular-auto-refetching
- @tanstack/query-example-angular-basic
- @tanstack/query-example-angular-basic-persister
- @tanstack/query-example-angular-devtools-panel
- @tanstack/query-example-angular-infinite-query-with-max-pages
- @tanstack/query-example-angular-optimistic-updates
- @tanstack/query-example-angular-pagination
- @tanstack/query-example-angular-query-options-from-a-service
- @tanstack/query-example-angular-router
- @tanstack/query-example-angular-rxjs
- @tanstack/query-example-angular-simple
- @tanstack/query-example-react-algolia
- @tanstack/query-example-react-auto-refetching
- @tanstack/query-example-react-basic
- @tanstack/query-example-react-basic-graphql-request
- @tanstack/query-example-chat
- @tanstack/query-example-react-default-query-function
- @tanstack/query-example-react-devtools-panel
- @tanstack/query-example-eslint-legacy
- @tanstack/query-example-react-infinite-query-with-max-pages
- @tanstack/query-example-react-load-more-infinite-scroll
- @tanstack/query-example-react-nextjs
- @tanstack/query-example-react-nextjs-app-prefetching
- @tanstack/query-example-nextjs-suspense-streaming
- @tanstack/query-example-react-offline
- @tanstack/query-example-react-optimistic-updates-cache
- @tanstack/query-example-react-optimistic-updates-ui
- @tanstack/query-example-react-pagination
- @tanstack/query-example-react-playground
- @tanstack/query-example-react-prefetching
- @tanstack/query-example-react-react-native
- @tanstack/query-example-react-router
- @tanstack/query-example-react-rick-morty
- @tanstack/query-example-react-shadow-dom
- @tanstack/query-example-react-simple
- @tanstack/query-example-react-star-wars
- @tanstack/query-example-react-suspense
- @tanstack/query-example-solid-astro
- @tanstack/query-example-solid-basic
- @tanstack/query-example-solid-basic-graphql-request
- @tanstack/query-example-solid-default-query-function
- @tanstack/query-example-solid-simple
- @tanstack/query-example-solid-start-streaming
- @tanstack/query-example-vue-2.6-basic
- @tanstack/query-example-vue-2.7-basic
- @tanstack/query-example-vue-basic
- @tanstack/query-example-vue-dependent-queries
- @tanstack/query-example-vue-nuxt3
- @tanstack/query-example-vue-persister
- @tanstack/query-example-vue-simple
- @tanstack/query-example-svelte-auto-refetching
- @tanstack/query-example-svelte-basic
- @tanstack/query-example-svelte-load-more-infinite-scroll
- @tanstack/query-example-svelte-optimistic-updates
- @tanstack/query-example-svelte-playground
- @tanstack/query-example-svelte-simple
- @tanstack/query-example-svelte-ssr
- @tanstack/query-example-svelte-star-wars
@tanstack/angular-query-experimental
npm i https://pkg.pr.new/@tanstack/angular-query-experimental@9973@tanstack/eslint-plugin-query
npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@9973@tanstack/query-async-storage-persister
npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@9973@tanstack/query-broadcast-client-experimental
npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@9973@tanstack/query-core
npm i https://pkg.pr.new/@tanstack/query-core@9973@tanstack/query-devtools
npm i https://pkg.pr.new/@tanstack/query-devtools@9973@tanstack/query-persist-client-core
npm i https://pkg.pr.new/@tanstack/query-persist-client-core@9973@tanstack/query-sync-storage-persister
npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@9973@tanstack/react-query
npm i https://pkg.pr.new/@tanstack/react-query@9973@tanstack/react-query-devtools
npm i https://pkg.pr.new/@tanstack/react-query-devtools@9973@tanstack/react-query-next-experimental
npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@9973@tanstack/react-query-persist-client
npm i https://pkg.pr.new/@tanstack/react-query-persist-client@9973@tanstack/solid-query
npm i https://pkg.pr.new/@tanstack/solid-query@9973@tanstack/solid-query-devtools
npm i https://pkg.pr.new/@tanstack/solid-query-devtools@9973@tanstack/solid-query-persist-client
npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@9973@tanstack/svelte-query
npm i https://pkg.pr.new/@tanstack/svelte-query@9973@tanstack/svelte-query-devtools
npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@9973@tanstack/svelte-query-persist-client
npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@9973@tanstack/vue-query
npm i https://pkg.pr.new/@tanstack/vue-query@9973@tanstack/vue-query-devtools
npm i https://pkg.pr.new/@tanstack/vue-query-devtools@9973commit:
c3fc4d8Codecov Report#
:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 59.57%. Comparing base (
0a1e3e0) to head (c3fc4d8). :warning: Report is 1 commits behind head on main.Additional details and impacted files
@@ Coverage Diff @@ ## main #9973 +/- ## =========================================== + Coverage 45.79% 59.57% +13.77% =========================================== Files 200 129 -71 Lines 8520 5734 -2786 Branches 1975 1582 -393 =========================================== - Hits 3902 3416 -486 + Misses 4158 2001 -2157 + Partials 460 317 -143- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
TkDodo merged this7f47906
TkDodo closed this
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: c3fc4d80bcbc201e4ab2fdda3df6a2a338262a58
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 19 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR