A call to fetchQuery is cancelled when React useQuery is unmounted #9798
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
which library are you trying to integrate with and is it open source?
I took a closer look at your sandbox and made a slight change here to track when things run https://codesandbox.io/p/devbox/tanstack-query-issue-9798-fetchquery-cancelled-forked-p93k2t
basically, the abort signal will always be false while there are no observers, so if you run fetch query and use the signal it will abort. however by default the signal isn't used and if it wasn't' using signal it would complete and populate the cache. (https://tanstack.com/query/latest/docs/framework/react/guides/query-cancellation#default-behavior)
so we'd have to change the semantics of that abort signal to make an exception for
fetchQueryif it's imperative. I'm not sure that's what we want.I don’t think that’s what we want, the behaviour is definitely on purpose. If you use
fetchQueryin a route loader, you will most likelyawaitit. If you don’t await it, it doesn’t matter that it fails.It’s also only relevant if you opt-in to the abort signal - the default behaviour is to not cancel the request, so this is fine.
I think I actually added a test-case lately to make sure the fetchQuery result resolves with a CancelledError here, because before that, we would just hang indefinitely:
https://github.com/TanStack/query/blob/e4f5ce8ec2167382e15d913ca129a05f646140e7/packages/query-core/src/tests/queryClient.test.tsx#L1036-L1063
TkDodo added labelsworks as designed
TkDodo closed this
which library are you trying to integrate with and is it open source?
I am using MUI X Tree View, following this example: https://mui.com/x/react-tree-view/rich-tree-view/lazy-loading/#using-react-query
however by default the signal isn't used and if it wasn't' using signal it would complete and populate the cache. It’s also only relevant if you opt-in to the abort signal - the default behaviour is to not cancel the request, so this is fine.
While that is of course true, in this case I am using Orval which auto-generates queries based on an openapi specification. It consumes the signal by default, which is of course what you want in 99% of the time.
I still think its very weird that doing a fetchQuery produces different results if some other component is using useQuery to fetch the same query. It took a long time of debugging before I figured out why my query was sometimes cancelled..
It consumes the signal by default, which is of course what you want in 99% of the time
If we’d agree that you would want it 99% of the time, we would also make it the default: https://tanstack.com/query/v5/docs/framework/react/guides/query-cancellation#default-behavior
if some other component is using useQuery to fetch the same query
I don’t think that’s happening. You get a
CancelledErrorif the query gets cancelled while you’re trying to fetch it. If that happens, what else should we do? The alternative is a never resolving promise. We can’t just ignore the cancellation because it can happen for two reasons:- The last observer that uses a query unmounts, which means the query is now unused and the AbortSignal is consumed.
- Someone calls
queryClient.cancelQueries()and cancels it manually.
You’re seeing the first scenario, and yeah this is a known
StrictMode+AbortSignalbehaviour where react unmounts an observer which triggers a cancellation. You would get the same behaviour if the user navigates away from the page that uses the query when the promise is still in-flight.Bottom line is the mui example code is just missing error handling inside
fetchTreeItems.
Sign in to join this conversation.
I have one additional example, which is not using strictmode. The query is cancelled after two seconds: https://codesandbox.io/p/sandbox/tanstack-query-issue-9798-fetchquery-cancelled-without-strictmode-2xcsfm