fix(db): item disappears during optimistic-to-synced transition#1469
Open
freddybreitenstein wants to merge 1 commit intoTanStack:mainfrom
Open
fix(db): item disappears during optimistic-to-synced transition#1469freddybreitenstein wants to merge 1 commit intoTanStack:mainfrom
freddybreitenstein wants to merge 1 commit intoTanStack:mainfrom
Conversation
…tack#1017) collection.insert() called commit() (which synchronously enters mutationFn/onInsert) before recomputeOptimisticState(true), so the item was not in optimisticUpserts when onInsert ran. Any sync delivery during onInsert (e.g., Electric's txid handshake) would not find the item. Fix: move transactions.set(), scheduleTransactionCleanup(), and recomputeOptimisticState(true) before commit() so the optimistic entry exists when mutationFn executes. Regression test verifies collection.has(key) is true at every checkpoint during the optimistic → synced transition. Fixes TanStack#1017
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.
Summary
Fixes #1017 — items disappear from collection during the optimistic → synced transition when
onInsertsyncs data back (e.g., Electric's txid handshake).Root Cause
collection.insert()calledcommit()beforerecomputeOptimisticState(true). Sincecommit()synchronously entersmutationFn/onInsert, the item was not inoptimisticUpsertswhenonInsertran. Any code checkingcollection.has(key)insideonInsert— or any sync data delivered duringonInsert— would not find the item.Impact
This bug causes items to disappear from live queries during the optimistic → synced transition. With Electric SQL on React Native (20-second long-poll), the item could be gone for up to 20 seconds. On web with SSE, it manifests as a brief flicker.
Test Plan
collection.has(key)istrueat every checkpoint during the transition