- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 3.5k
 
Description
Describe the bug
While integrating Tanstack Query with another library, I found queryClient.fetchQuery() to fail the first time during development. The promise was cancelled when some component was using the same query with useQuery - only during development, when using StrictMode.
But it can also happen in production. If an useQuery is unmounted, it cancels the query if it is the last one observing the query. Even if some other code is running queryClient.fetchQuery(). I would expect Tanstack Query to know that fetchQuery is running and not cancel the query.
Your minimal, reproducible example
https://codesandbox.io/p/sandbox/tanstack-query-issue-9798-fetchquery-cancelled-gkt869
Steps to reproduce
- Open the codesandbox
 - The useQueryResult will succesfully show the amount of stargazers, but the fetchQueryResult will contain a CancelledError.
 
Expected behavior
As a user, I expected the fetch not to be cancelled because fetchQuery was called. I would expect that unrendering a useQuery call would not in any way influence a call to fetchQuery anywhere in the application.
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
- OS: Linux, Mac
 - Browser: Edge, Chrome 141.0.7390.76
 
Tanstack Query adapter
react-query
TanStack Query version
v5.90.5
TypeScript version
No response
Additional context
No response