SQLite persistence: after two quick writes to one row, the collection briefly shows the state before the second write
Mantenedores costumam responder em até 1 dia
Um pull request relacionado já foi integrado.
- #2208 de @KyleAMathews — integrado
Avaliação
- Dificuldade
- 4/5
- Tempo estimado
- 3-5 dias
- Facilidade para iniciantes
- 48/100
- Tipo de issue
- Bug
- Clareza
- Razoavelmente clara
- Status de atividade
- Ativa
- Stack de tecnologia
- sqlite, typescript
Direção de pesquisa
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.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
- 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.
- Linguagem predominante
- TypeScript
- Estrelas
- 3.9k
- Forks
- 277
- Merge médio
- 18h 12min
- PRs com merge (30d)
- 229
Preparar o ambiente
- Sem Dockerfile nem arquivo Docker Compose
- Tem um modelo de pull request
- Ler o guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de TanStack/db
-
Dificuldade 4/5 1-2 dias Facilidade para iniciantes 48/100
Mantenedores costumam responder em até 1 dia
-
Ordered-replay oracle: terminal error after a failed settle following truncate markersTalvez já em andamento @KyleAMathews assumiu há 1 dia. Aberta
Dificuldade 4/5 3-5 dias Facilidade para iniciantes 35/100
Mantenedores costumam responder em até 1 dia
-
Dificuldade 5/5 Mais de uma semana Facilidade para iniciantes 35/100
Mantenedores costumam responder em até 1 dia
-
Dificuldade 4/5 1-2 dias Facilidade para iniciantes 58/100
Mantenedores costumam responder em até 1 dia
-
Dificuldade 4/5 1-2 dias Facilidade para iniciantes 48/100
Mantenedores costumam responder em até 1 dia
Todas as issues de TanStack/db
Issues semelhantes
-
bug hacktoberfest help wanted
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 82/100
libredb/libredb-studio#1941 ·
Mantenedores costumam responder em até 1 dia
-
chore other
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
alastair-smith/card-games#308 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 86/100
paperclipai/paperclip#17379 ·
Mantenedores costumam responder em até 1 dia
-
[good first issue, hacktoberfest] 🏯 Hacktoberfest Task - Add new Anime Quote 626 (good-first-issue)Abertacommunity first-timers-only good first issue hacktoberfest help wanted low hanging fruit up-for-grabs
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 76/100
lingdojo/kana-dojo#33040 · 1 comentário · 5 reações ·
Mantenedores costumam responder em até 1 dia
-
bug mobile ui
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 88/100
Mantenedores costumam responder em até 1 dia