fix(query-core): snapshot resetQueries match set before mutating state#10765
Open
spokodev wants to merge 1 commit into
Open
fix(query-core): snapshot resetQueries match set before mutating state#10765spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
Fixes TanStack#10705. `resetQueries` ran the caller's filter twice: once to pick queries to reset, then a second time inside `refetchQueries` to pick queries to re-fetch. The first call mutates each query's state (e.g. status flips from "error"/"success" to "pending"), so when the filter uses a state-based predicate the second pass no longer matches the same set and the refetch silently skips the queries we just reset. The fix snapshots the matched queries before calling `reset()`, then refetches the same set by identity (`matchedQueries.includes(query)`), still scoped to active queries. Query instances are stable across reset, so the identity predicate is a faithful replacement for the caller's filter without the state-mutation hazard.
Contributor
|
Note Currently processing new changes in this PR. This may take a few minutes, please wait... ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment Tip CodeRabbit can generate a title for your PR based on the changes.Add |
|
View your CI Pipeline Execution ↗ for commit 020ae15
☁️ Nx Cloud last updated this comment at |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #10705.
What was broken
queryClient.resetQueries({ predicate: q => q.state.status === 'error' })reset the matched queries but never re-fetched them — they stayed stuck inpendingforever.Root cause
resetQueriesran the caller's filter twice:refetchQueries({ type: 'active', ...filters })call, to pick the queries to re-fetch.query.reset()mutates each query's state — for example, the status flips from"error"to"pending". Whenfilterscontains a state-based predicate likeq => q.state.status === 'error', the second pass evaluates against the already-mutated state and the matched set collapses, so the re-fetch step silently does nothing.The original issue author traced this exactly: #10705
Fix
Snapshot the matched queries before calling
reset(), then refetch the same set by identity (matchedQueries.includes(query)), still scoped totype: 'active'. Query references are stable acrossreset(), so the identity predicate is a faithful replacement for the caller's filter without the state-mutation hazard.Test
Added a regression test in
queryClient.test.tsxcovering the exact scenario from the issue: two active queries (one errored, one successful), callresetQueries({ predicate: status === 'error' }), assert the errored query'squeryFnwas called once and the successful one's wasn't.main(erroredqueryFncalled0times — bug behaviour).Full
queryClient.test.tsxsuite: 108 passed (one new test added). Lint clean on changed files.Note on scope
The same two-pass pattern exists in
invalidateQueries, butinvalidate()only flipsisInvalidated(which the caller's predicate is unlikely to key on) and therefetchType: 'none'short-circuit covers the obvious case. Leaving that alone to keep this PR scoped to the reported bug.Summary by CodeRabbit
Bug Fixes
Tests