`isPaused` stays `true` when resuming mutations from the cache #8999
Send to my agentDescribe the bug#
Resuming a mutation from the cache does not set isPaused to true.
I suspect that it could come from the this.#retryer?.continue() not being set yet -> https://github.com/TanStack/query/blob/main/packages/query-core/src/mutation.ts#L156
Your minimal, reproducible example#
https://stackblitz.com/github/RaphaelGimenez/tanstackquery-ispaused-issue?file=README.md
Steps to reproduce#
The example is running a json server and timeout all !GET request.
- Open Stackblitz project, start script is set to
preview - Go to Stackblitz port
4173 - Toggle online by clicking the following button and delete post
- Mutation is paused
- Refresh the preview -> mutation is resumed but
isPaused = true - Toggle offline / online -> mutation is correctly unpaused
Expected behavior#
Resumed mutations from the persister should correctly set isPaused to false.
How often does this bug happen?#
Every time
Screenshots or Videos#
https://github.com/user-attachments/assets/6f8dcf9d-7fd4-4af3-83cd-9ee493909f07
Platform#
- OS: Fedora
- Browser: Firefox, Chromium
Tanstack Query adapter#
react-query
TanStack Query version#
5.72.2
TypeScript version#
No response
Additional context#
No response
sorry, I need a “better” example. Can you try trimming things down to the bare essentials please to make the issue appear?
I’m running into issues while debugging because when I follow the steps to reproduction, the mutation definitely does continue for me. It starts to error and I get weird popups from Stackblitz saying:
This project requires a connection to your localhost.Really, there’s no need to work with actual requests in an issue reproduction. Just do
Promise.resolve()orPromise.reject()with some mock data and a bunch of timeouts to make it take a while and it should be much easier. But as it stands, I can’t debug this. Thanks.I suspect that it could come from the this.#retryer?.continue() not being set yet
continuing on the retryer is only necessary when a mutation has been “paused” in the same session. When you restore it with a persister plugin, it’s on purpose that we call
.execute()on the mutation to initiate the retryer.@HanhNguyenHong1802 I'm resuming the mutations only once the persister loaded the cache:
<PersistQueryClientProvider client={queryClient} persistOptions={{ persister, }} onSuccess={() => { queryClient.resumePausedMutations().then(() => { queryClient.invalidateQueries(); }); }} >If you open the devtools and go to the mutaitons on refresh you can see that the mutation is shown as paused when it's actually running. I don't think this should be the case. A running mutation should not be in the paused state.
@TkDodo
sorry, I need a “better” example. Can you try trimming things down to the bare essentials please to make the issue appear?
My bad, I tried trimming it up, delete and getAll are now using Promises. Same stackblitz. The
previewscript does not seem to work on chrome while it's working in firefox. My bad for not checking I fixed the stackblitz url to use defaultdevscript.continuing on the retryer is only necessary when a mutation has been “paused” in the same session. When you restore it with a persister plugin, it’s on purpose that we call .execute()on the mutation to initiate the retryer.
Yes this is what I understood. But the issue I see here is that since we directly call
.executewe do not dispatch thecontinueaction, which seems to be the part settingisPaused: false-> https://github.com/TanStack/query/blob/main/packages/query-core/src/mutation.ts#L289 Thus, when resuming a paused mutation from the persister, while it's correctly running the mutation,state.isPausedstaystrue.
I may be misunderstanding the logic here, but if I resume a paused mutation after the persisted state is loaded, its
isPausedstate should befalsesince the mutation is currently running.As I understand it a running mutation should not be in the paused state, which appears to be the case here.
we do not dispatch the continue action
I think you are right with that. What needs to happen imo is that we additionally dispatch the
continueeven whenisRestoringis set:ideally, we would also extract it from here:
to an
onContinuefunction so that we make sure we always call the same things.So this kinda becomes:
if(isRestored) { onContinue() } else { ... }Where
onContinuewould be:const onContinue = () => { this.#dispatch({ type: 'continue' }) }and we would also pass that to
createRetryer.If you’re up for it, please file a PR with that fix, but we’d also need a test case that fails on main and works with the fix. Thanks 🙏
TkDodo added labelsbug
TkDodo added labelspackage: query-core
Ok, I'll try to work on a PR :)
TkDodo closed this
Sign in to join this conversation.
I observed the demo changes, and I saw after refreshing the app,
resumePausedMutations()will run later. IMO, theisPausedstate isn't cleared immediately on refresh because you are using persister, which is designed to retain the application's state across reloads. TheresumePausedMutations()function is the mechanism to handle these paused mutations after the persisted state is loaded. If you want a different behavior, you need to modify how you use persistence.