Skip to content

fix(query-core/queriesObserver): pass query hashes when combining in … - #12633

Open
yashhbeladiya wants to merge 1 commit into
TanStack:mainfrom
yashhbeladiya:fix/12626-stale-combine
Open

yashhbeladiya wants to merge 1 commit into
TanStack:mainfrom
yashhbeladiya:fix/12626-stale-combine

Conversation

@yashhbeladiya

@yashhbeladiya yashhbeladiya commented Nov 19, 2026 •

Copy link
Copy Markdown

…'#notify' so a stale combined result is not returned (#12626)

🎯 Changes

Fixes #12626

useQueries with a stable combine could hand back a combined result built from the previous query list. If a query from the old list updated between the render and the effect that calls setQueries, #notify re-ran combine on the old results but didn't pass any query hashes. So #lastQueryHashes still held the new list from the render. On the next render the hashes looked unchanged, #result === #lastResult, and the old (shorter) combined result was returned. That's how the reporter ended up with results[index] being undefined.

The fix is a one-liner: #notify now passes the hashes of the queries it's actually combining (from #observerMatches). The next render sees that the list changed and recomputes. It doesn't add extra combine calls, because #result has always just changed when #notify runs.

I added a test in queriesObserver.test.tsx that follows the same order as useQueries: subscribe with one query, render with two, let the first query resolve before setQueries runs, then render again. It fails on main (gets 1 result instead of 2) and passes with the fix. The useQueries tests in react, preact, vue, solid, svelte and lit still pass.

About test:pr: everything passes except the Next.js example and integration builds. Those fail the same way on main, because Next.js 16.4.4 refuses to build over a known security advisory (SecurityFatalError). That's unrelated to this change (the renovate PR #12558 bumps Next).

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with pnpm run test:pr, or these tests do not apply to this pull request.
  • I have followed the AI contribution policy and fully understand the code in this pull request, including any code generated with AI assistance.

🚀 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 an issue where combined query results could become stale when an existing query updated while the observed query list was changing. Combined results now continue to reflect the current set of queries, including newly added queries, before and after an existing query finishes updating.

…'#notify' so a stale combined result is not returned (TanStack#12626)
@changeset-bot

changeset-bot Bot commented Nov 19, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: aab2d96

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 26 packages
Name Type
@tanstack/query-core Patch
@tanstack/angular-query Patch
@tanstack/lit-query Patch
@tanstack/preact-query Patch
@tanstack/query-async-storage-persister Patch
@tanstack/query-broadcast-client-experimental Patch
@tanstack/query-devtools Patch
@tanstack/query-persist-client-core Patch
@tanstack/query-sync-storage-persister Patch
@tanstack/react-query Patch
@tanstack/solid-query Patch
@tanstack/svelte-query Patch
@tanstack/vue-query Patch
@tanstack/angular-query-devtools Patch
@tanstack/angular-query-persist-client Patch
@tanstack/preact-query-devtools Patch
@tanstack/preact-query-persist-client Patch
@tanstack/react-query-devtools Patch
@tanstack/solid-query-devtools Patch
@tanstack/svelte-query-devtools Patch
@tanstack/vue-query-devtools Patch
@tanstack/react-query-persist-client Patch
@tanstack/solid-query-persist-client Patch
@tanstack/svelte-query-persist-client Patch
@tanstack/react-query-next-experimental Patch
@tanstack/eslint-plugin-query Patch

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

@yashhbeladiya

Copy link
Copy Markdown
Author

Just noticed #12632 was opened a minute before this one with the same fix in #notify. Only the tests differ. Happy for you to go with whichever you prefer, and I'll close mine if you pick #12632.

@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

QueriesObserver.#notify now passes the matched queries’ hashes to #combineResult. A regression test checks the combined result when an existing query resolves before setQueries receives an expanded query list.

Changes: QueriesObserver combined results

Layer / File(s) Summary
Notification hash handling
packages/query-core/src/queriesObserver.ts, packages/query-core/src/__tests__/queriesObserver.test.tsx, .changeset/fix-queries-observer-stale-combine.md
#notify passes the matched query hashes to #combineResult. A regression test checks that the combined result still contains two queries after an existing query resolves. The changeset declares a patch for @tanstack/query-core.

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

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • TanStack/query#12632: Passes the notifying observer list’s hashes to #combineResult to address the same stale combined result.

Suggested reviewers: sukvvon, tkdodo

Merge Risk: Minimal · up to aab2d

This fix makes useQueries with a stable combine reflect the current query list instead of a stale shorter result. It is a small change with a regression test and no identified merge-blocking risk.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the fix in QueriesObserver (passing query hashes when combining), which matches the changed code in queriesObserver.ts. It is specific enough for a teammate to see the main change. It …
Description check Passed The description follows the repository template. It has the Changes section with a clear explanation of the root cause, the one-line fix, and a regression test. The checklist is filled in and the rele…
Linked Issues check Passed Issue #12626 requires that the combined result of useQueries with a stable combine reflect the queries of the current render. The root cause is that #notify calls #combineResult without queryHashes, s…
Out of Scope Changes check Passed The whole-PR diff against the merge base contains three changes: a three-line change in packages/query-core/src/queriesObserver.ts that passes query hashes in #notify, a regression test in packages/qu…
Full details: Title check

Explanation

The title names the fix in QueriesObserver (passing query hashes when combining), which matches the changed code in queriesObserver.ts. It is specific enough for a teammate to see the main change. It is truncated with a trailing 'in …', which weakens clarity but does not make it misleading.

Full details: Description check

Explanation

The description follows the repository template. It has the Changes section with a clear explanation of the root cause, the one-line fix, and a regression test. The checklist is filled in and the release impact is marked. It also reports test results and the unrelated Next.js build failure. The description starts with a stray fragment of the commit-style title ('…'#notify' so a stale combined result...'), which is a minor formatting issue.

Full details: Linked Issues check

Explanation

Issue #12626 requires that the combined result of useQueries with a stable combine reflect the queries of the current render. The root cause is that #notify calls #combineResult without queryHashes, so #lastQueryHashes keeps the new list while the cached result holds the old list. At the reviewed head, queriesObserver.ts #notify passes queryHashes built from this.#observerMatches (match.defaultedQueryOptions.queryHash) to #combineResult. The next render therefore detects the list change and recomputes. This matches the fix proposed in the issue. A regression test in queriesObserver.test.tsx reproduces the issue sequence: subscribe with one query, getOptimisticResult with two queries, let the existing query resolve, then re-render, and expects a count of 2 both times. The test is automated and covers the coding requirement. Tests were not executed in this review; the result relies on the diff and the PR's reported test runs.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against the merge base contains three changes: a three-line change in packages/query-core/src/queriesObserver.ts that passes query hashes in #notify, a regression test in packages/query-core/src/tests/queriesObserver.test.tsx for the same bug, and a patch changeset for @tanstack/query-core. Each change connects directly to #12626. The changeset is the release artifact the PR description says is required for published code. No unrelated changes were found.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 816a0aa and aab2d96.

Files selected for processing: 3

  • .changeset/fix-queries-observer-stale-combine.md
  • packages/query-core/src/__tests__/queriesObserver.test.tsx
  • packages/query-core/src/queriesObserver.ts

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

This branch has not been deployed

No deployments
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.

useQueries: stable combine returns a stale result when a query updates before setQueries runs

1 participant