query icon indicating copy to clipboard operation
query copied to clipboard

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

Open swheel33 opened this issue 1 week ago • 2 comments

[!NOTE] 🤖 Claude Opus 5.5 responding on behalf of swheel33

Describe the bug

useQueries with a stable combine can return a combined result built from the previous query list, so its length no longer matches the queries passed in on that render. Code that indexes the combined result by the input list (results[index].data) then throws: we hit TypeError: Cannot read properties of undefined in our app.

This is a gap left by #9954 (the fix for #8781). #combineResult now detects a changed query list by comparing queryHashes with #lastQueryHashes, but only getOptimisticResult passes queryHashes. #notify calls #combineResult without them, so it recomputes from the observer's current results (the old list) while #lastQueryHashes still holds the new list from the last render. On the next render the hashes look unchanged, #result === #lastResult, and the stale combined result is returned.

Sequence:

  1. Render with queries A (3 queries). The effect calls setQueries(A).
  2. Render with queries B (5 queries). getOptimisticResult combines 5 results and sets #lastQueryHashes = B.
  3. Before the effect calls setQueries(B), a query in A updates. #onUpdate → #notify → #combineResult combines the 3 results of A and caches them, leaving #lastQueryHashes = B.
  4. The listener re-renders with B. #combineResult sees no change and returns the 3-item result.

Your minimal, reproducible example

https://gist.github.com/swheel33/2f38c4065d592f58a8046917c405252e

Steps to reproduce

npm i @tanstack/[email protected]
node repro.mjs

Output:

render with 3 queries -> 3
render with 5 queries -> 5
re-render with 5 queries -> 3 (expected 5)

The script drives QueriesObserver the way useQueries does: getOptimisticResult per render, setQueries in an effect.

Expected behavior

The combined result reflects the queries passed on the current render, so the last line prints 5.

How often does this bug happen?

Sometimes

Platform

  • @tanstack/query-core 5.108.0 (also seen on 5.103.0)
  • @tanstack/react-query 5.103.0, React 19
  • OS: macOS, Browser: Chrome

Tanstack Query adapter

react-query

TanStack Query version

5.108.0

TypeScript version

N/A (the repro is plain JS)

Additional context

In the browser this happens when the list of queries grows (in our case, one balance query per token, and the token list grows when a wallet connects) and an existing query resolves between the render and the useQueries effect.

We worked around it with a local patch that has #notify pass the hashes of the results it combines, so the next render sees the list changed:

this.#combineResult(
  this.#trackResult(this.#result, this.#observerMatches),
  this.#options?.combine,
  this.#observerMatches.map((match) => match.defaultedQueryOptions.queryHash),
)

swheel33 avatar Nov 18 '26 16:11 swheel33

I'll work on this.

mgarcia-improving avatar Nov 19 '26 14:11 mgarcia-improving

The root cause of the desynchronization is:

  1. During render, getOptimisticResult computes combined results for the new query list (say, N = 5) and updates #lastQueryHashes with the 5 query hashes.

  2. If an existing query updates before the React effect commits setQueries(newQueries), #onUpdate calls #notify.

  3. #notify currently invokes #combineResult without passing queryHashes:

    this.#combineResult(
      this.#trackResult(this.#result, this.#observerMatches),
      this.#options?.combine,
    )
    
  4. Since queryHashes isn't provided, #combineResult recomputes using the old results (N = 3) and updates #lastResult, while leaving #lastQueryHashes untouched. It therefore retains the 5 hashes from the optimistic render.

  5. On the subsequent re-render, getOptimisticResult checks whether the hashes have changed:

    replaceEqualDeep(this.#lastQueryHashes, queryHashes) === this.#lastQueryHashes
    

    Since the 5 hashes match #lastQueryHashes, it assumes the combine inputs haven't changed and returns the cached, stale #lastResult containing only 3 results.

    This leads to indexing errors when the component expects 5 results and accesses results[index].data.

Fix

Pass the active match hashes to #combineResult inside #notify:

this.#combineResult(
  this.#trackResult(this.#result, this.#observerMatches),
  this.#options?.combine,
  this.#observerMatches.map((match) => match.defaultedQueryOptions.queryHash),
)

This keeps #lastQueryHashes aligned with the result array being cached. Consequently, when the subsequent render receives a different query list, the hash comparison detects the change and triggers the required recomputation.

YK-03 avatar Nov 19 '26 19:11 YK-03