-
Notifications
You must be signed in to change notification settings - Fork 152
Fix garbage collection issue with Infinity setting #1135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KyleAMathews
wants to merge
2
commits into
main
Choose a base branch
from
claude/fix-gc-infinity-issue-pincd
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
When gcTime was set to Infinity, setTimeout(fn, Infinity) would coerce Infinity to 0 via JavaScript's ToInt32 conversion, causing immediate garbage collection instead of disabling it. This fix treats Infinity (and any non-finite values) the same as 0, effectively disabling automatic garbage collection when the user intends for data to never be collected. Fixes issue where collections in Electron apps would unexpectedly lose data when navigating back and forth with gcTime set to Infinity.
🦋 Changeset detectedLatest commit: 4c988df The changes in this PR will be included in the next version bump. This PR includes changesets to release 12 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
More templates
@tanstack/angular-db
@tanstack/db
@tanstack/db-ivm
@tanstack/electric-db-collection
@tanstack/offline-transactions
@tanstack/powersync-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
Co-Authored-By: Claude Opus 4.5 <[email protected]>
Contributor
|
Size Change: +14 B (+0.02%) Total Size: 90.6 kB
ℹ️ View Unchanged
|
Contributor
|
Size Change: 0 B Total Size: 3.47 kB ℹ️ View Unchanged
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a bug where setting
gcTime: Infinityto disable garbage collection actually caused immediate GC instead of never collecting, due to JavaScript'ssetTimeoutcoercingInfinityto0via ToInt32.Root Cause
When users configure
gcTime: Infinityexpecting their collection to never be garbage collected, JavaScript'ssetTimeoutsilently converts the delay:This happens because the HTML spec requires the delay to be clamped to a 32-bit signed integer, and
ToInt32(Infinity) === 0. The result: collections were immediately cleaned up the moment subscribers dropped to zero—the exact opposite of the intended behavior.Approach
Added an explicit check for non-finite values alongside the existing
gcTime === 0check:Using
Number.isFinite()handlesInfinity,-Infinity, andNaNin a single check, making the code robust against any edge cases.Key Invariants
gcTime: 0→ GC disabled (existing behavior, unchanged)gcTime: Infinity→ GC disabled (now works correctly)gcTime: <positive number>→ GC after specified milliseconds (unchanged)Non-goals
gcTimevalue (5 minutes)gcTimevalues—the API contract is clearTrade-offs
Alternative considered: Could have special-cased only
Infinity, but!Number.isFinite()is more defensive againstNaNor negative infinity edge cases that would also misbehave withsetTimeout.Verification
pnpm test packages/db/tests/collection-lifecycle.test.tsThe new test case specifically validates that
gcTime: Infinityprevents any timer from being scheduled.Files Changed
packages/db/src/collection/lifecycle.ts!Number.isFinite(gcTime)check instartGCTimer()packages/db/tests/collection-lifecycle.test.tsgcTime: Infinitybehavior🤖 Generated with Claude Code