fix(query-core): keep garbage collecting an unobserved query after it is reset #12712
main fromrefs/pull/12712/head·opened View on GitHub🎯 Changes#
Refs #12561. The main fix for that issue already landed in #12683; this covers one remaining case from the issue's expected behavior ("reset/refetch ... should still be collected normally").
Query.reset() calls destroy(), which clears the GC timeout, but the query stays in the cache. For an unobserved query nothing reschedules GC (only a refetch of an active query would), so it stayed in the cache with no timer, even though gcTime is finite.
reset()now schedules GC again when the query has no observers and is still in the cache.- Added a regression case to
cacheGc.test.tsx. It fails onmainand passes with this change.
Tests: the full query-core suite passes (1659 tests) via vitest in the package. I did not run pnpm run test:pr.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with
pnpm run test:pr, or these tests do not apply to this pull request. - I have followed the AI contribution policy and fully understand the code in this pull request, including any code generated with AI assistance.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Summary by CodeRabbit#
- Bug Fixes
- Unobserved queries are now garbage-collected on schedule after being reset.
- 1
tanstack-source added labelsbug
No actionable comments were generated in the recent review.
Walkthrough
-
Query.reset()now schedules garbage collection when the query has no observers and remains cached. A test verifies the timer persists afterresetQueriesand the query is removed from the cache after 1000 ms.Changes: Query reset garbage collection
Layer / File(s) Summary Schedule GC after query reset
packages/query-core/src/query.ts,packages/query-core/src/__tests__/cacheGc.test.tsx,.changeset/reset-query-keeps-gc.mdQuery.reset()schedules GC when there are no observers. The test checks that the timer remains scheduled and that the query is removed from the cache after 1000 ms. The changeset records the patch.Estimated code review effort: 2 (Simple) | ~5 minutes
Change: Bug fix
Possibly related issues
- TanStack/query issue 12561 concerns garbage-collection timer lifecycle in query-core. This change addresses a related case where resetting an unobserved cached query leaves it without a GC timer.
Possibly related PRs
- TanStack/query#12664: Both changes update
Query.reset()and the GC timer lifecycle for queries that remain cached.
Suggested reviewers:
tkdodo,sukvvon
Merge Risk:
Minimal · up to97019-
Resetting an unobserved cached query now keeps it eligible for garbage collection, so it no longer lingers in the cache without a timer. The change is small and covered by a regression test, and no merge-blocking risk is evident.
Pre-merge checks:
4-
Check name Status Explanation Title check The title names the actual change: Query.reset() now reschedules garbage collection for an unobserved query. It also names the affected package, query-core, and follows the conventional fix(scope) for… Description check The description follows the template's three sections: Changes, Checklist, and Release Impact. The Changes section explains the cause (reset() calls destroy(), which clears the GC timeout, and nothing… Linked Issues check Check skipped because no linked issues were found for this pull request. Out of Scope Changes check Check skipped because no linked issues were found for this pull request. Full details: Title check
-
Explanation
The title names the actual change: Query.reset() now reschedules garbage collection for an unobserved query. It also names the affected package, query-core, and follows the conventional fix(scope) format. It matches the raw summary, the test addition, and the PR objectives.
Full details: Description check
-
Explanation
The description follows the template's three sections: Changes, Checklist, and Release Impact. The Changes section explains the cause (reset() calls destroy(), which clears the GC timeout, and nothing reschedules GC for an unobserved query) and the fix (GC is rescheduled when there are no observers and the query is still cached). It also lists the regression test and the test results. The author left one checklist item unchecked: pnpm run test:pr was not run. The author states this openly, so the description is mostly complete.
-
Finishing Touches
-
Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
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.
Recent review info-
Run configuration-
- Source mode: Auto
- Repository: TanStack/query
- Branch: main
- File: .coderabbit.yaml
- Commit: 635aad6
- Layers: Repository YAML
- Review profile: CHILL
- Plan: Advanced
- Run ID:
f1504736-513c-4b4c-b7e7-46294dc8084a
Commits-
Reviewing files that changed from the base of the PR and between 635aad6a95b08dffc91ba1973c043b219f085cd6 and 970196b393076a7982ee295fa49b6d273fc12d17.
Files selected for processing: 3-
.changeset/reset-query-keeps-gc.mdpackages/query-core/src/__tests__/cacheGc.test.tsxpackages/query-core/src/query.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
-
-
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: 970196b393076a7982ee295fa49b6d273fc12d17
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 26 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR