Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/prevent-residual-gc-timers.md
Original file line number Diff line number Diff line change
@@ -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.
32 changes: 32 additions & 0 deletions packages/query-core/src/__tests__/mutationCache.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
10 changes: 10 additions & 0 deletions packages/query-core/src/__tests__/removable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
})
4 changes: 4 additions & 0 deletions packages/query-core/src/mutationCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
*/
remove(mutation: Mutation<any, any, any, any>): void {
if (this.#mutations.delete(mutation)) {
mutation.destroy()
const scope = scopeFor(mutation)
if (typeof scope === 'string') {
const scopedMutations = this.#scopes.get(scope)
Expand Down Expand Up @@ -264,6 +265,9 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
const mutations = this.getAll()
this.#mutations.clear()
this.#scopes.clear()
mutations.forEach((mutation) => {
mutation.destroy()
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
})
mutations.forEach((mutation) => {
this.notify({ type: 'removed', mutation })
})
Expand Down
6 changes: 5 additions & 1 deletion packages/query-core/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,12 @@ export class Query<
* in-flight fetch.
*/
reset(): void {
this.destroy()
this.clearGcTimeout()
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
this.cancel({ silent: true })
this.setState(this.resetState)
if (!this.observers.length) {
this.scheduleGc()
}
}

/**
Expand Down
4 changes: 3 additions & 1 deletion packages/query-core/src/removable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ 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.
* A subclass may override this to release what it holds on to as well — `Query` also cancels any
* in-flight fetch.
*/
destroy(): void {
this.#isDestroyed = true
this.clearGcTimeout()
}

Expand All @@ -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)
Expand Down