NotifyManager breaks when a exception occurs in onSuccess callback #3215
Send to my agentDescribe the bug#
When a exception occurs in onSuccess callback
mutation.mutate(
{},
{
onSuccess: () => {
throw new Error();
},
},
);
whitch notify by NotifyManager.batch
batch<T>(callback: () => T): T {
this.transactions++
const result = callback() // exception raise
this.transactions-- // not executed
if (!this.transactions) { // never be 0
this.flush()
}
return result
}
since NotifyManager.batch breaks, all newly created query's result would stuck in loading state even fetch succuessful
Your minimal, reproducible example#
https://codesandbox.io/s/gracious-danny-zfq8u?file=/src/App.js
- click mutate
- click query
Steps to reproduce#
mutation.mutate(
{},
{
onSuccess: () => {
throw new Error();
},
},
);
Expected behavior#
QueryObervable should notify onSuccess defensively
How often does this bug happen?#
No response
Screenshots or Videos#
No response
Platform#
irrelevant
react-query version#
3.34.7
TypeScript version#
No response
Additional context#
No response
Your minimal, reproducible example none
please create a codesandbox reproduction, thanks
done
TkDodo added labelsbug
thank you. In the attached PR, the sandbox works, and I also made a test that failed before and now works. Can you give it a quick review? Then we can merge it: https://github.com/tannerlinsley/react-query/pull/3216
Thanks! I think the next batch works ok now. I haven't try yet, but I guess there still an issue that subsequence callbacks (e.g.
getQueryCache().notify) on the same batch whitch exception occurs wound't get call. Catch exception above onSuccess/onSettled should fix this.do you mean that
onSettledwon't be called ifonSuccesserrors? If so, I think that's fine. I explicitly didn't want to catch errors because what should we do with it? It's not the libs responsibility to catch user errors. I think it's fine that errors bubble up, and yes that stops further execution.If you expect errors in your callbacks, you can and should wrap them in try/catch.
I mean the cache listener in QueryObervable, will this affect other queries with same cache key if this not get call? I'm not sure, this just look like internal logic whitch shouldn't stop by user error
hmm, we just tested for mutations here. For queries, it would mean that listeners (useQuery) would not be informed about the update because of the error. But I don't think that try/catch around those is really a better option...
I think I'll go ahead and merge the fix and we can think about further improvements separately
TkDodo closed this
:tada: This issue has been resolved in version 3.34.11 :tada:
The release is available on:
Your semantic-release bot :package::rocket:
tannerlinsley added labelsreleased
:tada: This issue has been resolved in version 4.0.0-alpha.8 :tada:
The release is available on:
Your semantic-release bot :package::rocket:
tannerlinsley added labelsreleased on @alpha
I think the issue is still present, it happened to me on 4.3.4 on the onSettled function.
@ncoen97 show an example please
Here is a code sandbox. If you look at the onSettled, I was missing the function before the
queryClient.invalidateQueries()I can't see the error in the sandbox, but in my local development this is what I'm getting:Uncaught (in promise) TypeError: callback is not a function at notifyManager.ts:51:9 at notifyFn (notifyManager.ts:15:5) at notifyManager.ts:63:13 at Array.forEach (<anonymous>) at notifyManager.ts:62:25 at batchedUpdates$1 (react-dom.development.js:26096:12) at notifyManager.ts:61:9.this is a different problem. by doing:
onSettled: queryClient.invalidateQueries()you're calling
queryClient.invalidateQueries()during render, and you pass the resulting promise toonSettled, where a function is expected. TypeScript can solve this one.
Sign in to join this conversation.
none
please create a codesandbox reproduction, thanks