query icon indicating copy to clipboard operation
query copied to clipboard

Infinite render loop when a select function throws in v5.105.2+

Open tmkx opened this issue 3 weeks ago • 1 comments

Describe the bug

Starting with @tanstack/react-query v5.105.2, an inline select function that throws can cause an infinite render loop when the query component renders a stateful UI component.

React repeatedly renders the component until it reports:

Maximum update depth exceeded

The selector exception itself is caused by application data in this reproduction: the response omits an optional result field, while the selector calls data.result.map(...).

I do not expect React Query to ignore the selector exception. I expect the query to enter an error state, or throw according to its error-handling options, without repeatedly updating the external-store snapshot.

The same reproduction is stable with v5.105.0 and v5.105.1. The loop starts with v5.105.2 and is still reproducible with v5.106.0.

Your minimal, reproducible example

https://stackblitz.com/edit/stackblitz-starters-ewemkxcc?file=matrix.mjs

Steps to reproduce

  1. Open the reproduction.
  2. Install the dependencies with npm install.
  3. Run npm run repro.
  4. Observe that v5.105.0 and v5.105.1 render a finite number of times.
  5. Observe that v5.105.2 and v5.106.0 continue rendering until React reports Maximum update depth exceeded, or until the reproduction's observation window ends.
  6. Observe that v5.106.0 no longer loops when the selector uses data.result ?? [].

Expected behavior

If a select function throws, React Query should expose the selector error through the query result and settle without repeatedly forcing React to render. An unstable inline selector may run again on a later render, but it should not create its own render loop.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

N/A

Tanstack Query adapter

react-query

TanStack Query version

5.106.0

TypeScript version

No response

Additional context

❯ npm run repro

> repro
> node matrix.mjs

5.105.0                 renders=6410   maximum-depth=false
5.105.1                 renders=6448   maximum-depth=false
5.105.2                 renders=108001 maximum-depth=true
5.106.0                 renders=108001 maximum-depth=true
5.106.0 + result guard  renders=4000   maximum-depth=false

tmkx avatar Nov 06 '26 01:11 tmkx

I'll work on this.

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