Skip to content
Source
Sign inSign up
TanStack/query

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

Openmgarcia-improving(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/12712/headopened View on GitHub

mgarcia-improving(GitHub profile, opens in a new tab)commented

🎯 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.
    1. tanstack-source added labelsbug

    2. 🦋 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
      NameType
      @tanstack/query-corePatch
      @tanstack/angular-queryPatch
      @tanstack/lit-queryPatch
      @tanstack/preact-queryPatch
      @tanstack/query-async-storage-persisterPatch
      @tanstack/query-broadcast-client-experimentalPatch
      @tanstack/query-devtoolsPatch
      @tanstack/query-persist-client-corePatch
      @tanstack/query-sync-storage-persisterPatch
      @tanstack/react-queryPatch
      @tanstack/solid-queryPatch
      @tanstack/svelte-queryPatch
      @tanstack/vue-queryPatch
      @tanstack/angular-query-devtoolsPatch
      @tanstack/angular-query-persist-clientPatch
      @tanstack/preact-query-devtoolsPatch
      @tanstack/preact-query-persist-clientPatch
      @tanstack/react-query-devtoolsPatch
      @tanstack/solid-query-devtoolsPatch
      @tanstack/svelte-query-devtoolsPatch
      @tanstack/vue-query-devtoolsPatch
      @tanstack/react-query-persist-clientPatch
      @tanstack/solid-query-persist-clientPatch
      @tanstack/svelte-query-persist-clientPatch
      @tanstack/react-query-next-experimentalPatch
      @tanstack/eslint-plugin-queryPatch

      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

    3. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      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 nameStatusExplanation
      Title checkPassedThe 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 checkPassedThe 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 checkPassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes checkPassedCheck 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 635aad6a95b08dffc91ba1973c043b219f085cd6 and 970196b393076a7982ee295fa49b6d273fc12d17.

      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.

Sign in to join this conversation.