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/fix-gc-infinity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/db': patch
---

Fix `gcTime: Infinity` causing immediate garbage collection instead of disabling GC. JavaScript's `setTimeout` coerces `Infinity` to `0` via ToInt32, so we now explicitly check for non-finite values.
6 changes: 4 additions & 2 deletions packages/db/src/collection/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,10 @@ export class CollectionLifecycleManager<

const gcTime = this.config.gcTime ?? 300000 // 5 minutes default

// If gcTime is 0, GC is disabled
if (gcTime === 0) {
// If gcTime is 0 or Infinity (or any non-finite value), GC is disabled.
// Note: setTimeout with Infinity coerces to 0 via ToInt32, causing immediate GC,
// so we must explicitly check for non-finite values here.
if (gcTime === 0 || !Number.isFinite(gcTime)) {
return
}

Expand Down
20 changes: 20 additions & 0 deletions packages/db/tests/collection-lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,26 @@ describe(`Collection Lifecycle Management`, () => {
expect(mockSetTimeout).not.toHaveBeenCalled()
expect(collection.status).not.toBe(`cleaned-up`)
})

it(`should disable GC when gcTime is Infinity`, () => {
const collection = createCollection<{ id: string; name: string }>({
id: `infinity-gc-test`,
getKey: (item) => item.id,
gcTime: Infinity, // Disabled GC via Infinity
sync: {
sync: () => {},
},
})

const subscription = collection.subscribeChanges(() => {})
subscription.unsubscribe()

// Should not start any timer when gcTime is Infinity
// Note: Without this fix, setTimeout(fn, Infinity) would coerce to 0,
// causing immediate GC instead of never collecting
expect(mockSetTimeout).not.toHaveBeenCalled()
expect(collection.status).not.toBe(`cleaned-up`)
})
})

describe(`Manual Preload and Cleanup`, () => {
Expand Down
Loading