-
Notifications
You must be signed in to change notification settings - Fork 99
fix race condition that resulted in live queries becoming stuck #650
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
Conversation
🦋 Changeset detectedLatest commit: 9f0f852 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/query-db-collection
@tanstack/react-db
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
Size Change: +7 B (+0.01%) Total Size: 75.2 kB
ℹ️ View Unchanged
|
Size Change: 0 B Total Size: 1.47 kB ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks great!
Just tried this in my codebase and it definitely fixes the issue I was seeing. You guys rock. |
fixes #645
The original reproduction from that issue updated to the preview package from this PR: https://stackblitz.com/edit/tanstack-db-init-jm98exso?file=src%2FApp.tsx
Summary
The bug was in the
capturePreSyncVisibleState()
method instate.ts
. Here's what was happening:The Race Condition:
capturePreSyncVisibleState()
captured the visible state for key "0" (with optimistic data)recomputeOptimisticState()
cleared T1's optimistic statecommitPendingTransactions()
couldn't run yet because T2 was still persistingcapturePreSyncVisibleState()
was called again and cleared all previous captures withthis.preSyncVisibleState.clear()
commitPendingTransactions()
finally ran, it had nopreviousVisibleValue
for key "0", causing it to emit INSERT instead of UPDATEThe Fix:
I modified
capturePreSyncVisibleState()
to:this.preSyncVisibleState.clear()
call to preserve previously captured stateif (!this.preSyncVisibleState.has(key))
before capturing to avoid overwriting earlier capturesThis ensures that when transactions complete at different times (due to async delays in
onInsert
), each key's visible state is captured at the correct moment - when its corresponding transaction completes and before its optimistic state is cleared. The state is then preserved until all sync operations can be committed together.