fix(query-core): don't reschedule GC for removed queries by Elehiggle · Pull Request #12036 · TanStack/query · GitHub
Skip to content

fix(query-core): don't reschedule GC for removed queries - #12036

Open
Elehiggle wants to merge 1 commit into
TanStack:mainfrom
Elehiggle:fix/removed-query-gc
Open

Elehiggle wants to merge 1 commit into
TanStack:mainfrom
Elehiggle:fix/removed-query-gc

Conversation

@Elehiggle

@Elehiggle Elehiggle commented Oct 16, 2026 •

Copy link
Copy Markdown

🎯 Changes

A query removed from the cache can get a new GC timer when an observer unsubscribes or a cancelled fetch finishes. That timer keeps the removed query and its data alive until gcTime expires.

In those two paths, only schedule GC if the cache still holds that query instance. This also handles a new query using the same key, without affecting its timer.

Added regression tests for late unsubscribe and fetch cancellation, with and without a replacement query. They fail before the fix and pass after it. pnpm run test:pr only fails on the Preact example's typecheck, which picks up generated dist files. The same failure occurs on unpatched main.

Repro: standalone reproduction.

✅ 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
    • Removed queries no longer trigger unnecessary garbage-collection timers when observers unsubscribe or cancelled requests finish.
    • Prevented late results from removed queries from affecting replacement data; removed queries without replacements remain absent from the cache.

Late observer removal and fetch completion can restart GC after a query leaves the cache. Check the cached instance before scheduling so removed queries are released and same-key replacements keep their own timers.
@changeset-bot

changeset-bot Bot commented Oct 16, 2026

Copy link
Copy Markdown

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-16T12:20:07.267338Z 39c35c7 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: TanStack/query/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 83fff001-1353-4f07-8cac-02cb652385c0
📥 Commits

Reviewing files that changed from the base of the PR and between ee47993 and 39c35c7.

📒 Files selected for processing: 3
  • .changeset/removed-query-gc.md
  • packages/query-core/src/__tests__/query.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; 9 remain after this review.

📝 Walkthrough

Query garbage-collection scheduling now occurs only when the query remains in its cache. Regression tests cover observer removal and cancelled fetch cleanup, with and without replacement data.

Changes: Removed Query Garbage Collection

Layer / File(s) Summary
Guard garbage-collection scheduling
packages/query-core/src/query.ts
removeObserver and the fetch finalizer now check that the cache still maps the query hash to the query before scheduling garbage collection.
Test removed-query cleanup
packages/query-core/src/__tests__/query.test.tsx, .changeset/removed-query-gc.md
Regression tests cover observer removal and cancelled fetches after query removal, both with and without replacement data. The changeset records the patch.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: sukvvon

Merge Risk: ⚪ Minimal · up to 39c35

The removed-query GC fix is ready for normal checks; no actionable merge-blocking issue was found.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: preventing garbage-collection timers from being rescheduled for removed queries.
Description check Passed The description explains the bug, fix, regression tests, test status, reproduction, completed checklist items, and release impact. It matches the required template and provides sufficient implementati…
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
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: Description check

Explanation

The description explains the bug, fix, regression tests, test status, reproduction, completed checklist items, and release impact. It matches the required template and provides sufficient implementation context.

Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2 files. (1 skipped: 1 unsupported.)

✨ 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

Comment @coderabbitai help to get the list of available commands.

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