Skip to content
Source
Sign inSign up
TanStack/query

fix(query-core): fix queriesObserver result and observer length mismatch #9973

Mergednwrahnema(GitHub profile, opens in a new tab)merged 2 commits intomain fromrefs/pull/9973/headmerged by TkDodo View on GitHub

🎯 Changes

In 1b54538, setOptions was changed to call notifyListeners which eventually leads to QueriesObserver.setQueries

  1. Updating #observerMatches
  2. Calling setOptions which leads to #notify being called
  3. 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.
✏️ Tip: You can customize this high-level summary in your review settings.
    1. 🦋 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
      NameType
      @tanstack/query-corePatch
      @tanstack/angular-query-experimentalPatch
      @tanstack/query-async-storage-persisterPatch
      @tanstack/query-broadcast-client-experimentalPatch
      @tanstack/query-persist-client-corePatch
      @tanstack/query-sync-storage-persisterPatch
      @tanstack/react-queryPatch
      @tanstack/solid-queryPatch
      @tanstack/svelte-queryPatch
      @tanstack/vue-queryPatch
      @tanstack/angular-query-persist-clientPatch
      @tanstack/react-query-persist-clientPatch
      @tanstack/solid-query-persist-clientPatch
      @tanstack/svelte-query-persist-clientPatch
      @tanstack/react-query-devtoolsPatch
      @tanstack/react-query-next-experimentalPatch
      @tanstack/solid-query-devtoolsPatch
      @tanstack/svelte-query-devtoolsPatch
      @tanstack/vue-query-devtoolsPatch

      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

    2. github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: query-core

    3. Walkthrough

      This PR patches @tanstack/query-core to fix a race condition in useQueries when the number of queries changes. The fix modifies when observerMatches is 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.md
      Added patch version bump entry for @tanstack/query-core describing race condition fix in useQueries (#9971)
      Core observer fix
      packages/query-core/src/queriesObserver.ts
      Moved observerMatches update from unconditional assignment to conditional assignment within structural-change branch, ensuring observer matches only updates during actual structural changes

      Estimated 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
      • TanStack/query#9639: Also modifies queriesObserver.ts to adjust observer matching and structural-change detection logic timing

      Suggested labels

      package: query-core

      Suggested 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 nameStatusExplanation
      Title check✅ PassedThe title accurately summarizes the main fix: addressing a mismatch between queriesObserver result and observer length.
      Description check✅ PassedThe description follows the template with complete sections: detailed explanation of the issue, full checklist completion, and proper release impact marking.
      Docstring Coverage✅ PassedNo 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 #observerMatches assignment inside the hasStructuralChange block ensures that when setOptions (line 112) triggers an early notify via a listener, both #observerMatches and #result still have matching lengths (both reflecting the old state). This prevents the index-out-of-bounds error in #trackResult that occurred when #observerMatches was updated unconditionally before the setOptions calls.


      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 and usage tips.
    4. View your CI Pipeline Execution ↗ for commit c3fc4d80bcbc201e4ab2fdda3df6a2a338262a58

      CommandStatusDurationResult
      nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded1m 5sView ↗

      ☁️ Nx Cloud last updated this comment at 2025-12-30 14:28:22 UTC

    5. More templates

      @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@9973

      commit: c3fc4d8

    6. Codecov 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

      Impacted file tree graph

      @@             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     
      ComponentsCoverage Δ
      @tanstack/angular-query-experimental93.85% <ø> (ø)
      @tanstack/eslint-plugin-query∅ <ø> (∅)
      @tanstack/query-async-storage-persister43.85% <ø> (ø)
      @tanstack/query-broadcast-client-experimental24.39% <ø> (ø)
      @tanstack/query-codemods∅ <ø> (∅)
      @tanstack/query-core97.37% <100.00%> (ø)
      @tanstack/query-devtools3.38% <ø> (ø)
      @tanstack/query-persist-client-core80.00% <ø> (ø)
      @tanstack/query-sync-storage-persister84.61% <ø> (ø)
      @tanstack/query-test-utils∅ <ø> (∅)
      @tanstack/react-query96.34% <ø> (ø)
      @tanstack/react-query-devtools9.25% <ø> (ø)
      @tanstack/react-query-next-experimental∅ <ø> (∅)
      @tanstack/react-query-persist-client100.00% <ø> (ø)
      @tanstack/solid-query77.81% <ø> (ø)
      @tanstack/solid-query-devtools64.17% <ø> (ø)
      @tanstack/solid-query-persist-client100.00% <ø> (ø)
      @tanstack/svelte-query∅ <ø> (∅)
      @tanstack/svelte-query-devtools∅ <ø> (∅)
      @tanstack/svelte-query-persist-client∅ <ø> (∅)
      @tanstack/vue-query71.91% <ø> (ø)
      @tanstack/vue-query-devtools∅ <ø> (∅)
      :rocket: New features to boost your workflow:
      • :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

  • 🎉 This PR has been released!

    Thank you for your contribution!

  • Sign in to join this conversation.