Infinite loop with useSuspenseQueries and short gcTime, slow queries when one query is already fetched #9454
Send to my agentDescribe the bug#
There are two React components. One only fetches query1, the other one fetches query1 and query2 When we load either component from a fresh page load everything works as expected. When we first load the first component and wait for query1 to complete before switching to the second component, the app gets stuck in an infinite loop where:
- Component 2 tries to fetch query2 (query1 data is already cached)
- During query2's 2-second fetch, query1 gets garbage collected (after 1 second of being unused)
- When query2 completes, TanStack Query notices query1 is missing and re-fetches it
- During query1's 2-second fetch, query2 gets garbage collected
- Repeat indefinitely
I think what happens under the hood is:
When loading the second component after query1 successfully completed, tanstack-query will try to only fetch query2. But since this will take longer than the configured gcTime, and since during the fetch no component is rendered that actually uses query1, it will be garbage-collected. Once query 2 completes tanstack-query notices the missing query1 and re-fetches it.
It would be nice if tanstack-query would not garbage-collect queries that are currently being observed by a query "associated" with a rendered suspense-boundary.
But maybe this is what I deserve for configuring a gcTime that is shorter than typical query latency.
Your minimal, reproducible example#
https://codesandbox.io/p/github/fbieler/tanstack-query-suspense-with-short-gc-time/main?import=true
Steps to reproduce#
Start App in codesandbox (it should listen on port 3100)
- Click button "Query1"
- Wait until response appears
- Click button "Query 1 & 2"
- Observe that responses never load
- Optionally open browser console to observe log messages of simulated queries.
Expected behavior#
I would like to see the responses of both queries.
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
OS: Linux Browsers: Chromium 138.0.7204.100, Firefox 140.0.4
Tanstack Query adapter#
react-query
TanStack Query version#
5.83.0
TypeScript version#
No response
Additional context#
You can also locally test this bug. The git repo of the codesandbox.io link above is here: https://github.com/fbieler/tanstack-query-suspense-with-short-gc-time
TkDodo closed this
TkDodo added labelshas workaround
TkDodo added labelsknown limitation
Sign in to join this conversation.
This is a good find, but I don’t think there’s anything we can do about it.
Suspense triggers a fetch by throwing a promise during rendering, so this happens way before the QueryObserver subscribes to the cache, which means your Query2 is never seen as “observed”.
Also, if the suspense boundary unmounts the component that renders query1, we don’t know that suspense did that and we also don’t know if that query is coming back to be observed.
The only thing you can do is not set a gcTime that is this low, or set your suspense boundaries in a granular way that loading query2 does not unmount query1.