From fa8079bf30581d06ff066c825e32d7d2795eb30c Mon Sep 17 00:00:00 2001 From: Jonathan Hernandez Sanchez <57070806+JonathanHerSa@users.noreply.github.com> Date: Sat, 21 Nov 2026 20:13:54 -0600 Subject: [PATCH 1/2] fix(query-core): prevent residual gc timers on removed cache entries --- .changeset/prevent-residual-gc-timers.md | 5 +++ .../src/__tests__/mutationCache.test.tsx | 32 +++++++++++++++++++ .../src/__tests__/removable.test.tsx | 10 ++++++ packages/query-core/src/mutationCache.ts | 2 ++ packages/query-core/src/query.ts | 3 +- packages/query-core/src/removable.ts | 4 ++- 6 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 .changeset/prevent-residual-gc-timers.md diff --git a/.changeset/prevent-residual-gc-timers.md b/.changeset/prevent-residual-gc-timers.md new file mode 100644 index 00000000000..7fa34a33e59 --- /dev/null +++ b/.changeset/prevent-residual-gc-timers.md @@ -0,0 +1,5 @@ +--- +'@tanstack/query-core': patch +--- + +Prevent residual garbage-collection timers from remaining active or being rescheduled after entries are removed or destroyed. diff --git a/packages/query-core/src/__tests__/mutationCache.test.tsx b/packages/query-core/src/__tests__/mutationCache.test.tsx index 13d17265847..0fc60409600 100644 --- a/packages/query-core/src/__tests__/mutationCache.test.tsx +++ b/packages/query-core/src/__tests__/mutationCache.test.tsx @@ -630,5 +630,37 @@ describe('mutationCache', () => { unsubscribe() }) + + it('should destroy mutation when removing it', () => { + const testCache = new MutationCache() + const testClient = new QueryClient({ mutationCache: testCache }) + + const mutation = testCache.build(testClient, { + mutationFn: () => Promise.resolve('data'), + }) + + const destroySpy = vi.spyOn(mutation, 'destroy') + testCache.remove(mutation) + expect(destroySpy).toHaveBeenCalledTimes(1) + }) + + it('should destroy all mutations when clearing cache', () => { + const testCache = new MutationCache() + const testClient = new QueryClient({ mutationCache: testCache }) + + const mutation1 = testCache.build(testClient, { + mutationFn: () => Promise.resolve('data1'), + }) + const mutation2 = testCache.build(testClient, { + mutationFn: () => Promise.resolve('data2'), + }) + + const destroySpy1 = vi.spyOn(mutation1, 'destroy') + const destroySpy2 = vi.spyOn(mutation2, 'destroy') + testCache.clear() + + expect(destroySpy1).toHaveBeenCalledTimes(1) + expect(destroySpy2).toHaveBeenCalledTimes(1) + }) }) }) diff --git a/packages/query-core/src/__tests__/removable.test.tsx b/packages/query-core/src/__tests__/removable.test.tsx index 9b7fd04085a..77a1c24791f 100644 --- a/packages/query-core/src/__tests__/removable.test.tsx +++ b/packages/query-core/src/__tests__/removable.test.tsx @@ -182,5 +182,15 @@ describe('Removable', () => { expect(provider.clearTimeout).toHaveBeenCalledWith(123) }) + + it('should not schedule gc after being destroyed', () => { + const removable = new RemovableTest() + removable.callUpdateGcTime(1000) + removable.destroy() + + removable.callScheduleGc() + + expect(provider.setTimeout).not.toHaveBeenCalled() + }) }) }) diff --git a/packages/query-core/src/mutationCache.ts b/packages/query-core/src/mutationCache.ts index 31a539926fa..a3f3ad90295 100644 --- a/packages/query-core/src/mutationCache.ts +++ b/packages/query-core/src/mutationCache.ts @@ -185,6 +185,7 @@ export class MutationCache extends Subscribable { */ remove(mutation: Mutation): void { if (this.#mutations.delete(mutation)) { + mutation.destroy() const scope = scopeFor(mutation) if (typeof scope === 'string') { const scopedMutations = this.#scopes.get(scope) @@ -265,6 +266,7 @@ export class MutationCache extends Subscribable { this.#mutations.clear() this.#scopes.clear() mutations.forEach((mutation) => { + mutation.destroy() this.notify({ type: 'removed', mutation }) }) }) diff --git a/packages/query-core/src/query.ts b/packages/query-core/src/query.ts index 6351ff9fa93..705cbf9e023 100644 --- a/packages/query-core/src/query.ts +++ b/packages/query-core/src/query.ts @@ -459,7 +459,8 @@ export class Query< * in-flight fetch. */ reset(): void { - this.destroy() + this.clearGcTimeout() + this.cancel({ silent: true }) this.setState(this.resetState) } diff --git a/packages/query-core/src/removable.ts b/packages/query-core/src/removable.ts index d6356c73845..975ecaafe5e 100644 --- a/packages/query-core/src/removable.ts +++ b/packages/query-core/src/removable.ts @@ -10,6 +10,7 @@ import type { ManagedTimerId } from './timeoutManager' export abstract class Removable { gcTime!: number #gcTimeout?: ManagedTimerId + #isDestroyed = false /** * Clears the pending garbage collection timeout, so the entry is no longer scheduled for removal. @@ -17,6 +18,7 @@ export abstract class Removable { * in-flight fetch. */ destroy(): void { + this.#isDestroyed = true this.clearGcTimeout() } @@ -27,7 +29,7 @@ export abstract class Removable { protected scheduleGc(): void { this.clearGcTimeout() - if (isValidTimeout(this.gcTime)) { + if (!this.#isDestroyed && isValidTimeout(this.gcTime)) { this.#gcTimeout = timeoutManager.setTimeout(() => { this.optionalRemove() }, this.gcTime) From 9f725de0379056b6c2dd184ff4fed424dd0520bc Mon Sep 17 00:00:00 2001 From: Jonathan Hernandez Sanchez <57070806+JonathanHerSa@users.noreply.github.com> Date: Sat, 21 Nov 2026 21:03:40 -0600 Subject: [PATCH 2/2] refactor(query-core): address review feedback on clear and reset gc scheduling --- packages/query-core/src/mutationCache.ts | 2 ++ packages/query-core/src/query.ts | 3 +++ 2 files changed, 5 insertions(+) diff --git a/packages/query-core/src/mutationCache.ts b/packages/query-core/src/mutationCache.ts index a3f3ad90295..497fbba3d92 100644 --- a/packages/query-core/src/mutationCache.ts +++ b/packages/query-core/src/mutationCache.ts @@ -267,6 +267,8 @@ export class MutationCache extends Subscribable { this.#scopes.clear() mutations.forEach((mutation) => { mutation.destroy() + }) + mutations.forEach((mutation) => { this.notify({ type: 'removed', mutation }) }) }) diff --git a/packages/query-core/src/query.ts b/packages/query-core/src/query.ts index 705cbf9e023..02039694421 100644 --- a/packages/query-core/src/query.ts +++ b/packages/query-core/src/query.ts @@ -462,6 +462,9 @@ export class Query< this.clearGcTimeout() this.cancel({ silent: true }) this.setState(this.resetState) + if (!this.observers.length) { + this.scheduleGc() + } } /**