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..497fbba3d92 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) @@ -264,6 +265,9 @@ export class MutationCache extends Subscribable { const mutations = this.getAll() this.#mutations.clear() 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 6351ff9fa93..02039694421 100644 --- a/packages/query-core/src/query.ts +++ b/packages/query-core/src/query.ts @@ -459,8 +459,12 @@ export class Query< * in-flight fetch. */ reset(): void { - this.destroy() + this.clearGcTimeout() + this.cancel({ silent: true }) this.setState(this.resetState) + if (!this.observers.length) { + this.scheduleGc() + } } /** 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)