Using queryClient as a useCallback dependency? Possibly dangerous?
Hi all.
Chasing a very frustrating bug in production. We noticed occasionally our users would submit 20k requests in under a minute, effectively DoS'ing us. We're adding throttling etc, but I want to track down WHY they are sending so many requests. We think it's due to invalidation of some query keys, causing refetches to happen.
I cam across this code:
We call this when a file is finished uploading. Note the
queryClient in the dep array? Is it possible this is changing, and causing the invalidateQueries call to run many times? We will destructure and use just the invalidateQueries fn as a dep, as it's good practice, but I want to know if this is the cause/
Does anyone have any ideas?15 Replies
ratty-blush•2y ago
sounds like ure on the right track
diverse-roseOP•2y ago
Perhaps. But destructuring the query client like that is not supported as it’s a class
ratty-blush•2y ago
queryclient as a dep
is quite heavy 😅
ruins the point iwth use callback
🤔
ah jeez
diverse-roseOP•2y ago
Yeah. The docs suggest it should be stable. Yeah invalidateQueries will not have correct reference to this, so updating the query cache internally fails
There’s an issue on github
So unsure. Perhaps the bug I have is unrelated entirely
ratty-blush•2y ago
cc @TkDodo 🔮
maybe
ideas that come to mind
- memoize query client call
- use ref for the query client to ensure stable across re renders
const queryClientRef = React.useRef(queryClient);
im new to react query
so idk if using a ref would be okdiverse-roseOP•2y ago
Yeah. I think before I try those things, I need a way to know when it occurs and log or report the case, so I can know if the fix works. Right now it occurs rarely, perhaps twice a day judging by our load balancer logs.
Without a clear reproduction, or something to confirm the occurence (and thus the fix actually worked) I’ll never be satisfied the issue is gone
Wonder if I can track invalidate calls in a specific time period and report that out
As I suspect I am calling it hundreds of times erroneously
ratty-blush•2y ago
use a logging tool
:ThinkingIsHard:
a custom tracking tool may look something like
although
u prolly wanna use a logging tool for this
winston
axiom
sentry
someth around these
diverse-roseOP•2y ago
Perfect thank you
national-gold•2y ago
queryClient is stable because it's a class you just create once. Can safely be added as a dependency.
national-gold•2y ago
React Query FAQs
Answering the most frequently asked React Query questions
fascinating-indigo•2y ago
The reference to query client should not change. If it does, you don’t have a stable queryClient.
Also: why should reinstantiating this callback trigger the functions inside? Is it used in a useEffect?
diverse-roseOP•2y ago
It is used in a useEffect inside the component it is passed to
I think just adding the above tracing code will help me figure out if this function even being called multiple times. It may very well not.
Alright, added that to prod. See what we find. Hopefully i see it being triggered many times, and I can narrow it down. If not, need to look elsewhere.
ratty-blush•2y ago
what logging tool r u using
?
winstion
axiom
idk if sentry helps
sentry is a strong one
but i mean its more for errors really
diverse-roseOP•2y ago
Swntry
absent-sapphire•2y ago
useHotKeys