query: resumePausedMutations are not running after switching from offline to online after version 4.24.3 up to 4.32.6(latest)
Describe the bug
https://github.com/TanStack/query/commit/8991d7c0193c91fb17a97998bdbcda4363943f35 this is the commit where it breaks (4.24.3)
Not sure if this is correct behaviour. But based on the commit above i assume it is a bug indeed. Paused mutations get stuck when rehydrating(loading) mutations in offline mode and switching back online.
Basically, when you reenter the exited app in offline mode it restores the mutations successfully, i see paused mutations in logs as a result of resumePausedMutations.
But when you get back online, paused mutations do not run. This happens in latest 4.32.6. When i reload(React Native) my javascript it runs again. But, i got to the version where it works and triaged this. basically this is the commit where it starts breaking https://github.com/TanStack/query/commit/8991d7c0193c91fb17a97998bdbcda4363943f35
Shortly, 4.24.3 and upper version causes this bug
Your minimal, reproducible example
Run https://github.com/TanStack/query/tree/main/examples/react/offline
Steps to reproduce
- Go to any movie
- Go offline/ turn off your wife
- Add comment (to verify, add it three times)
- Refresh the page
- ENABLE your WIFI and then wait
- Nothing happens, but expected this mutations to get unpaused
- Refresh the page and you see updates are applied now sequentially.
This happens in my React Native setup too.
- Go offline
- Add todo (fire any mutation which will be paused)
- Exit from app then enter again (both RN debug/release mode)
- enable internet
- At the end i see 1 paused mutation pending in my logs. So it stucks/waits here:
queryClient.resumePausedMutations()
Expected behavior
As a user, i am expecting all mutations to start running after resume, but no mutations are running in that case. In logs they are all paused
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
React Native 0.70.6
Tanstack Query adapter
react-query
TanStack Query version
4.32.6 (but started in 4.24.3!!)
TypeScript version
No response
Additional context
If i will reload my javascript after 5th step, then it resumes mutations again and runs them correctly
Also, if you have internet enabled before entering the app, it works as expected.
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 9
- Comments: 31 (3 by maintainers)
lool, was readin my serious description over there to refresh my mind: 2. turn off your wife
š
Sorry, that might take some time for me to write testcase now, but i already found the commit which is causing this issue, hope someone will help here. Will try to reproduce this with test case in case if i will have a time
thanks for this, I think I figured it out a bit:
when we resume paused mutations after youāve reloaded the page, there is no āretryerā that can pick up the mutations from where they left, so we just always execute them:
https://github.com/TanStack/query/blob/13b17ee45de3299dd3d3a2dfc3d23ad2f024882a/packages/query-core/src/mutation.ts#L162
This will then create the retryer and return a pending promise, because we start in
pausedstate. This is technically fine, however, since we started this process viaresumePausedMutations, we have now stored that promise in the mutationCache inthis.resuming:https://github.com/TanStack/query/blob/13b17ee45de3299dd3d3a2dfc3d23ad2f024882a/packages/query-core/src/mutationCache.ts#L164
This was the fix that made sure that resuming paused mutations will always run in serial.
Now when you go online again, we call
resumePausedMutationsagain, but it doesnāt actually resume anything, because it already thinks it is resuming. This works when we have a retyer, becauseretryer.continue()resolves a Promise immediately if we are not online (another part of the aforementioned fix):https://github.com/TanStack/query/blob/6fcfc9bfba0b56d580952c2c22e91ca45c45cd17/packages/query-core/src/retryer.ts#L212-L215
but it doesnāt work if you are offline when the page loads.
I need to think a bit how to tackle this holistically, but what you can do on your end if to simply wrap the call to
resumePausedMutationsinto another online check:We should probably do this internally, but I have to think about where exactly without breaking things. Also, this should be significantly less relevant with v5 because the
onlineManagerwill always start inonlinemode.I was laughing hard at that one, too š¤£
@TkDodo thanks for the investigation & explanation, that all makes sense! i can confirm the workaround works with the latest v5 beta
[edit] The workaround works in the repro, but iām still having problems when using the beta in React Native. I donāt understand the difference but iāll update this thread if i figure it out.
@TkDodo in the meantime iāve create a repro here, see README for instructions: https://github.com/badsyntax/react-query-mutation-resume-bug
iāll see what i can do with replay.io
Iām having the same issue in a react-native/react-native-web + react-query project using react-query version 4.29.7. Tried rolling back to 4.24.2 and I confirm that the issue is not present in that version.
@badsyntax Ok, You were right š This was good direction, basically I had to check if Iām online using NetInfo library but in 2 places:
For the focus manager I think there might be better way to handle this I just posting it for now like this. With this it looks like all 3 cases from my previous post works š
The Code Sandbox version isnāt working for me either (https://tanstack.com/query/v4/docs/react/examples/react/offline) but when running it locally with @timabilovās steps, am able to reproduce
I think it important to note that the issue happens when turning off the computerās wifi (or wife!) in the steps above and refreshing the page while the wifi is still disabled.
Disabling the connection via the Devtools doesnāt reproduce the issue for me, perhaps because the connection is automatically restored when refreshing?
Also experiencing this or something similar on React Native with v4.24.6 of query, the query client, and async storage persister.
To test, we do the following:
If we turn off airplane mode prior to relaunching the app, the mutation is is sent as expected.
We have the same issue with React Web + React Native.
So, i found that this is related to the fact that i have multiple setOnline(true) calls, and because of that āresumingā patch -
mutation.continue()only fired once where it basically still stays in pause. But without this resuming patch, subsequentsetOnline(true) -> resumePausedMutationscalls fires my requests.To be sure, it is definitely not request hanging or some timeout issues because my mutationFn is not called at all with given conditions.
Not sure if it is related to something with my connection anyway, because after setOnline(True) it still should trigger mutations, which is weird. but i see only
resumePausedMutationstriggers