SQLite persistence: after two quick writes to one row, the collection briefly shows the state before the second write
維護者通常 1 天內回覆
已經有一個關聯 PR 被合併了。
- #2208 來自 @KyleAMathews —— 已合併
評估
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 新手友好度
- 48/100
- Issue 類型
- 缺陷
- 描述清晰度
- 基本清楚
- 活躍度
- 活躍
- 技術堆疊
- sqlite, typescript
研究方向
Start with the persistence and transaction flow described in the issue, especially awaitTxId, applyMutex, pendingSyncedTransactions, and recomputeOptimisticState; the linked reproduction and its README contain the trace and timings. Trace how an update is handled while SQLite still stores the insert's synced transaction, then verify that the collection never shows an older state after awaitTxId resolves. The issue does not identify source files or tests, so locating the relevant code and adding a regression test will take familiarity with the project.
由索引模型根據 Issue 內容生成。
描述
- I've validated the bug against the latest version of DB packages
Describe the bug
An Electric collection with persistedCollectionOptions inserts a row and updates it right away. Both handlers await awaitTxId. After the update is confirmed, the collection shows the pre-update state for 10–30 ms, then the updated state again. Without persistence, this never happens.
I hit this in a shopping list app. A user adds an item and ticks it off. The tick disappears for a moment, then comes back. An action that runs during that gap reads the old state: "clear bought items" saw the item as not bought and left it on the list. With 10× CPU throttling, the gap reached 263 ms in my app.
To Reproduce
Repro: https://github.com/daniel-fahl/tanstack-db-flicker-repro (StackBlitz). It needs no backend: a fake Electric server runs in the page.
- Open the repro.
- Click Add and tick 30 times. Each run inserts a todo and updates
donetotruewithout waiting in between. - See the result: 30 of 30 runs flicker. Each run logs the states the collection showed, for example
missing → unticked → ticked → unticked +24 ms → ticked +41 ms. - Switch to Control (
?persistence=off) and run again: 0 of 30 flicker.
Expected behavior
When awaitTxId(txid) resolves and the handler returns, the collection never shows a state older than that write.
Screenshots
None. The repro's log shows every state with its timing.
Desktop (please complete the following information):
- OS: macOS; my app's CI also hits it on Ubuntu (GitHub Actions
ubuntu-latest) - Browser: Chromium, via Playwright
- Version: Chromium as bundled with
@playwright/test1.64
Smartphone (please complete the following information):
Not tested on a device yet. 10× CPU throttling stands in for a slow phone.
Additional context
Versions: @tanstack/db 0.15.1, @tanstack/electric-db-collection 0.6.2, @tanstack/browser-db-sqlite-persistence 0.2.36, @tanstack/offline-transactions 1.1.3, @electric-sql/client 1.5.28. The same result on @tanstack/db 0.15.0 with @tanstack/electric-db-collection 0.6.1, and on 0.13.0 with 0.5.9.
Cause, in brief: the update's txid arrives while SQLite still stores the insert's synced transaction. awaitTxId resolves, but the update's synced transaction waits for applyMutex, so it is not in pendingSyncedTransactions yet. When onUpdate returns, recomputeOptimisticState finds no committed pending synced transaction for the key and drops the optimistic row. The repro's README has the full trace with timings from my app.
Possible fix: I am not sure where it belongs. Two ideas:
awaitTxIdresolves only once the synced transaction for that txid has reached the collection, at least as a committed pending transaction.recomputeOptimisticStatealso keeps the optimistic row while a synced transaction for that key still waits on persistence.
Waiting until the rows are fully applied would deadlock: the collection holds back synced rows while a transaction is persisting, and the handler's own transaction is persisting while it awaits the txid.
My workaround: the handler waits 150 ms after awaitTxId resolves. That was the shortest wait that removed every flicker in my app at 10× CPU throttling.
Related: #2120 may be related. It covers changes that progressive mode applies and confirms during the background full sync.
- 主要語言
- TypeScript
- 星號
- 3.9k
- 分支
- 277
- 平均合併
- 18 小時 12 分鐘
- 30 天內合併 PR
- 229
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
TanStack/db 的其他 Issue
-
難度 4/5 1-2 天 新手友好度 48/100
維護者通常 1 天內回覆
-
Ordered-replay oracle: terminal error after a failed settle following truncate markers可能已有人在做 @KyleAMathews 今天認領。 未關閉
難度 4/5 3-5 天 新手友好度 35/100
維護者通常 1 天內回覆
-
難度 5/5 一週以上 新手友好度 35/100
維護者通常 1 天內回覆
-
難度 4/5 1-2 天 新手友好度 58/100
維護者通常 1 天內回覆
-
難度 4/5 1-2 天 新手友好度 48/100
維護者通常 1 天內回覆
相似的 Issue
-
area:agent-view area:tui bug platform:macos
難度 2/5 1-3 小時 新手友好度 70/100
anthropics/claude-code#104558 ·
維護者通常 1 天內回覆
-
updater
難度 2/5 1-3 小時 新手友好度 68/100
UniversityOfHelsinkiCS/oodikone#5141 ·
-
maintanance
難度 2/5 1-3 小時 新手友好度 74/100
inmanta/web-console#7606 ·
維護者通常 1 天內回覆
-
bug skills
難度 2/5 1-3 小時 新手友好度 77/100
equinor/fusion-framework#5846 ·
維護者通常 1 天內回覆
-
難度 1/5 1 小時以內 新手友好度 74/100