Skip to content

fix(query-core): keep garbage collecting an unobserved query after it is reset - #12712

Open
mgarcia-improving wants to merge 1 commit into
TanStack:mainfrom
mgarcia-improving:issue-12561
Open

mgarcia-improving wants to merge 1 commit into
TanStack:mainfrom
mgarcia-improving:issue-12561

Conversation

@mgarcia-improving

@mgarcia-improving mgarcia-improving commented Nov 26, 2026 •

Copy link
Copy Markdown

🎯 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 on main and 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.

… is reset

Co-authored-by: Cursor <cursoragent@cursor.com>
@changeset-bot

changeset-bot Bot commented Nov 26, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 970196b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 26 packages
Name Type
@tanstack/query-core Patch
@tanstack/angular-query Patch
@tanstack/lit-query Patch
@tanstack/preact-query Patch
@tanstack/query-async-storage-persister Patch
@tanstack/query-broadcast-client-experimental Patch
@tanstack/query-devtools Patch
@tanstack/query-persist-client-core Patch
@tanstack/query-sync-storage-persister Patch
@tanstack/react-query Patch
@tanstack/solid-query Patch
@tanstack/svelte-query Patch
@tanstack/vue-query Patch
@tanstack/angular-query-devtools Patch
@tanstack/angular-query-persist-client Patch
@tanstack/preact-query-devtools Patch
@tanstack/preact-query-persist-client Patch
@tanstack/react-query-devtools Patch
@tanstack/solid-query-devtools Patch
@tanstack/svelte-query-devtools Patch
@tanstack/vue-query-devtools Patch
@tanstack/react-query-persist-client Patch
@tanstack/solid-query-persist-client Patch
@tanstack/svelte-query-persist-client Patch
@tanstack/react-query-next-experimental Patch
@tanstack/eslint-plugin-query Patch

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

@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

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 after resetQueries and 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.md
Query.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 to 97019

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: Passed 4

Check name Status Explanation
Title check Passed 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 Passed 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 Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed 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.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 635aad6 and 970196b.

Files selected for processing: 3

  • .changeset/reset-query-keeps-gc.md
  • packages/query-core/src/__tests__/cacheGc.test.tsx
  • packages/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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants