Removed cache entries retain or recreate garbage-collection timers after clear()
維護者通常 1 天內回覆
評估
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 新手友好度
- 25/100
- Issue 類型
- 缺陷
- 描述清晰度
- 描述清楚
- 活躍度
- 停滯
- 技術堆疊
- react, typescript
研究方向
Read the reproductions in reproduction.mjs and the regression cases in packages/query-core/src/tests/cacheGc.test.tsx. The issue says a fix is already prepared in linked pull request #12562, so check that work before considering this issue available. Done means removed entries release their GC timers and later cancellation or observer cleanup cannot recreate them, while replacement queries retain normal GC behavior.
由索引模型根據 Issue 內容生成。
描述
Describe the bug
QueryClient.clear() empties both caches, but garbage-collection timers can outlive the entries they were meant to remove:
- MutationCache.clear()/remove() remove a mutation without clearing its existing GC timer.
- QueryCache.remove() destroys a query and clears its timer, but an in-flight fetch cancellation later runs its finally block and schedules GC again. Removing the last observer after removal can also recreate a timer.
In the reproductions below, the residual timer retains the entry for gcTime (five minutes with browser defaults). An unbounded memory leak has not been measured. A removed mutation that is still pending can repeatedly schedule GC until it settles. The caches are empty; the timers are still live.
Your minimal, reproducible example
Save the file in an empty directory, then run:
npm install --no-save @tanstack/[email protected]
node reproduction.mjs
It uses @tanstack/query-core 5.107.1 and its public timeout provider to count live timers without waiting for them. The observer/query classes are real; no request, framework app, or private source is required. Regressions in packages/query-core/src/tests/cacheGc.test.tsx exercise the same behavior with the upstream test runner.
Steps to reproduce
- Complete a mutation, reset its observer so it is unused, then call client.clear(). Its cache is empty but a 300000 ms timer remains.
- Start a pending query with an observer that consumes its AbortSignal, then clear the client. The signal aborts and the cache empties, but once cancellation settles another 300000 ms GC timer appears. Later observer removal can also schedule it again.
- Explicitly destroying the removed entries after these continuations clears the residual timers.
Expected behavior
Removing an entry should release its existing GC timer and should not let later cancellation or observer cleanup schedule another timer for that removed entry. A replacement query with the same key must retain its own normal GC lifecycle.
This does not require cancelling running mutations, changing gcTime defaults, or disabling server-side GC. Cached entries, reset/refetch and entries re-added and used through their normal observer lifecycle should still be collected normally.
How often does this bug happen?
Every time in the isolated reproductions.
Platform
macOS arm64. The library regression tests run on Node.js 24.22.1 using the repository's jsdom environment; the standalone reproduction counts timers with a custom TimeoutProvider rather than a browser heap snapshot.
TanStack Query adapter and version
Core/vanilla reproduction; our consumer uses react-query. @tanstack/query-core and @tanstack/react-query 5.107.1. Also reproduced on the current main checkout, commit 8f6da1e2a1b5e9afd774cd12bd3865df5c432984. TypeScript 6.0.3 for the local checkout.
Additional context
Our test teardown currently retains references to cache entries even after inline clear(), awaits query cancellation, then explicitly destroys queries and mutations. Releasing removed entries' timers in the library would let us simplify that cleanup. No application-level latency or memory improvement has been measured.
Related: #12025 and closed/unmerged #12053 proposed disabling GC timers on the server. This report is narrower: release timers for entries explicitly removed from a cache, while keeping normal finite gcTime behavior on both client and server.
A fix is prepared with 14 regression cases. The complete query-core target passes 1,654 tests, and the repository test:pr gate passes all 627 selected tasks across 194 projects (24 valid Nx cache hits). The patch includes a changeset.
- 主要語言
- TypeScript
- 星號
- 50.6k
- 分支
- 4.2k
- 平均合併
- 9 小時 50 分鐘
- 30 天內合併 PR
- 310
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
TanStack/query 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 78/100
TanStack/query#12466 · 1 則留言 ·
維護者通常 1 天內回覆
-
solid-query: STRICT_READ_UNTRACKED on Solid 2 — client()/options() read in component body (useMutation, useBaseQuery)可能重新可做 @LeonxLJX 於 77 天前認領,目前沒有進行中的 PR。 未關閉
難度 2/5 1-3 小時 新手友好度 84/100
TanStack/query#11358 · 2 則留言 · 1 個 reaction ·
維護者通常 1 天內回覆
-
select that throws for placeholderData brings back the selection of data removed by resetQueries可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉
難度 3/5 1-2 天 新手友好度 56/100
維護者通常 1 天內回覆
-
Render loop since v5.105.2 when placeholderData, or its select result, is not referentially stable未關閉
難度 4/5 3-5 天 新手友好度 54/100
維護者通常 1 天內回覆
-
useQueries: stable combine returns a stale result when a query updates before setQueries runs可能已有人在做 @sakaintp 於 1 天前認領。 未關閉
難度 3/5 1-2 天 新手友好度 62/100
TanStack/query#12626 · 2 則留言 ·
維護者通常 1 天內回覆
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 78/100
fb55/htmlparser2#2579 ·
維護者通常 1 天內回覆
-
bug good first issue needs-review
難度 1/5 1-3 小時 新手友好度 83/100
維護者通常 1 天內回覆
-
documentation public
難度 1/5 1-3 小時 新手友好度 68/100
midnightntwrk/umbradb#65 ·
維護者通常 2 天內回覆
-
area/channels kind/bug priority/medium
難度 2/5 1-3 小時 新手友好度 78/100
nanocoai/nanoclaw#4501 · 1 則留言 ·
維護者通常 1 天內回覆
-
needs verification
難度 2/5 1-3 小時 新手友好度 74/100
維護者通常 13 天內回覆