Removed cache entries retain or recreate garbage-collection timers after clear()

オープン
#12,561 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@rvitaliy がすでに取り組んでいます。

2026年11月8日 から。

  • #12562 @rvitaliy による — オープン

評価

難易度
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:

  1. MutationCache.clear()/remove() remove a mutation without clearing its existing GC timer.
  2. 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

reproduction.mjs

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分
マージ済み PR(30日)
310

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

TanStack/query のほかの issue

TanStack/query の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。