Versions
@tanstack/db 0.11.1
@tanstack/query-db-collection 1.3.2
@tanstack/expo-db-sqlite-persistence 0.2.26
@tanstack/react-db 0.5.1
Problem
A second refetch of a persisted Query Collection can leave an active collection in error when the previous query result has already committed a sync transaction but SQLite has not applied it yet. This happens during ordinary use, without cleanup or a session change.
The adapter logs:
[QueryCollection] Error applying query overlap: AbortError: Sync transaction was aborted before application
[Live Query Error] Source collection 'overlap' entered error state
The collection can retain the old row after both refetches settle.
Deterministic reproduction
- Create a persisted Query Collection with one row
{ id: 'row', value: 1 }, a QueryClient, and an Expo-SQLite-compatible database. Preload it.
- Decorate the database
runAsync to await a deferred promise when a holdWrites flag is set. This delays SQLite application without delaying queryFn.
- Set
holdWrites = true, change the server row to { id: 'row', value: 2 }, and call collection.utils.refetch().
- Wait until the query fetch is idle and its result has started applying to SQLite (the decorated
runAsync has entered the wait).
- Change the server row to
{ id: 'row', value: 3 } and call collection.utils.refetch() again.
- Release the SQLite write; await both refetches.
Expected: value 3 is visible; no collection application error.
Actual: an AbortError is logged by trackResultApplication, and the visible row can remain value 1. The second query function did run, so this is not a fetch deduplication issue.
This also occurs when a mutation handler publishes an authoritative server row with writeUpsert while a query refresh is in flight, as in our XP award collection.
Likely cause
enqueueResultApplication calls invalidatePendingResultApplication for the previous result. That unconditionally aborts its controller. When its sync transaction has already been committed and is waiting for SQLite, aborting it removes it from the core pending queue. The newer transaction for the same row can depend on that queued transaction and gets invalidated too (SyncTransactionAbortedError).
I reproduced this in a real node:sqlite integration test. Keeping committed sync transactions alive until their SQLite application finishes, while still aborting pre-commit work, makes the test pass. A guard around cancellation after commit(signal) returns a pending application receipt is one possible fix. This is a suggested direction, not a claim that it covers all interleavings.
Versions
@tanstack/db0.11.1@tanstack/query-db-collection1.3.2@tanstack/expo-db-sqlite-persistence0.2.26@tanstack/react-db0.5.1Problem
A second refetch of a persisted Query Collection can leave an active collection in error when the previous query result has already committed a sync transaction but SQLite has not applied it yet. This happens during ordinary use, without cleanup or a session change.
The adapter logs:
The collection can retain the old row after both refetches settle.
Deterministic reproduction
{ id: 'row', value: 1 }, a QueryClient, and an Expo-SQLite-compatible database. Preload it.runAsyncto await a deferred promise when aholdWritesflag is set. This delays SQLite application without delayingqueryFn.holdWrites = true, change the server row to{ id: 'row', value: 2 }, and callcollection.utils.refetch().runAsynchas entered the wait).{ id: 'row', value: 3 }and callcollection.utils.refetch()again.Expected: value 3 is visible; no collection application error.
Actual: an
AbortErroris logged bytrackResultApplication, and the visible row can remain value 1. The second query function did run, so this is not a fetch deduplication issue.This also occurs when a mutation handler publishes an authoritative server row with
writeUpsertwhile a query refresh is in flight, as in our XP award collection.Likely cause
enqueueResultApplicationcallsinvalidatePendingResultApplicationfor the previous result. That unconditionally aborts its controller. When its sync transaction has already been committed and is waiting for SQLite, aborting it removes it from the core pending queue. The newer transaction for the same row can depend on that queued transaction and gets invalidated too (SyncTransactionAbortedError).I reproduced this in a real
node:sqliteintegration test. Keeping committed sync transactions alive until their SQLite application finishes, while still aborting pre-commit work, makes the test pass. A guard around cancellation aftercommit(signal)returns a pending application receipt is one possible fix. This is a suggested direction, not a claim that it covers all interleavings.