fix race condition that resulted in live queries becoming stuck#650
Merged
fix race condition that resulted in live queries becoming stuck#650
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: |
Contributor
|
Size Change: +7 B (+0.01%) Total Size: 75.2 kB
ℹ️ View Unchanged
|
Contributor
|
Size Change: 0 B Total Size: 1.47 kB ℹ️ View Unchanged
|
Contributor
|
Just tried this in my codebase and it definitely fixes the issue I was seeing. You guys rock. |
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
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.
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 nopreviousVisibleValuefor 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.