Repository navigation
Description
When a source Collection fails its first load, every live query over it goes to status: 'error'. After the source recovers through reload(), only the live query that called reload() recovers, and none does when reload() is called on the source itself. Every other live query over that source stays in error with no rows until it is reloaded itself.
This breaks the retry pattern from #2168 (onRetry={() => collection.reload()}) as soon as two components read the same source. A list with a Retry button recovers, and the filters, counters and badges next to it, which read the same source through their own live queries, stay empty.
Repro
@tanstack/db@1.0.0-rc.1.5, @tanstack/query-db-collection@1.5.2, @tanstack/query-core@5.108.4:
import { createCollection, createLiveQueryCollection, eq } from '@tanstack/db'
import { queryCollectionOptions } from '@tanstack/query-db-collection'
import { QueryClient } from '@tanstack/query-core'
let attempts = 0
const items = createCollection(queryCollectionOptions({
id: 'items',
queryKey: ['items'],
queryClient: new QueryClient(),
retry: false,
getKey: (item: { id: string, active: boolean }) => item.id,
queryFn: async () => {
attempts++
if (attempts === 1) throw new Error('network error')
return [{ id: '1', active: true }]
},
}))
const q1 = createLiveQueryCollection(q => q.from({ item: items }))
const q2 = createLiveQueryCollection(q => q.from({ item: items }).where(({ item }) => eq(item.active, true)))
q1.subscribeChanges(() => {})
q2.subscribeChanges(() => {})
await Promise.allSettled([q1.preload(), q2.preload()])
// items: 'error', q1: 'error', q2: 'error'
await q1.reload()
// items: 'ready', q1: 'ready' (1 row), q2: 'error' (0 rows)
// q2.health.error.message === 'network error'
// q2.health.sources[0]: { status: 'ready', error: null }| Retry | items | q1 | q2 |
|---|---|---|---|
q1.reload() |
ready |
ready |
error |
items.reload() |
ready |
error |
error |
A live query created after the recovery comes up ready with the row.
Where it comes from
query/live/collection-config-builder.ts:1408-1417, 1514-1523: the source's failed first load callsfailQuery(), which setsfatalQueryErroron every live query over it that isn't ready yet.collection-config-builder.ts:862, 1455-1464: once the source isready,maybeRunGraph()andupdateLiveQueryStatus()return early whilefatalQueryErroris set. Only the query's own new sync run clears it (lines 976, 1066).collection-config-builder.ts:156-179: a live query'sreload()walks down only. It reloads its failed sources, then itself.
Two side effects:
- Contradictory health tree: the stuck query's own node still shows the old source error, while its source node shows
readywith no error. - Pooled views behave differently: a pooled view takes
statusandhealth.errorfrom its source (query/pooled-live-query.ts:608-612, 626-638), so it should recover with the source, while a plain live query over the same source stays stuck.
Workaround
Reload every live query over the source. The source's in-flight reload is shared, so this sends one request:
function reloadDependents(source: Pick<Collection, 'subscribers'>) {
return Promise.allSettled(source.subscribers
.map(dependent => dependent.collection)
.filter(collection => collection !== undefined)
.map(collection => collection.reload()))
}It reaches only direct dependents (a query over a query over the source stays stuck), and every Retry button has to know the source Collections behind its live query.
Possible fixes
- Recover with the source (preferred). When a source goes from
errortoready, a live query whose error came only from that source's failed first load, and that has no other failed source, reloads itself (_sync.reload()), so leavingerrorstill goes through a reload and a new sync run. Bothq1.reload()anditems.reload()then recover every query overitems, nested ones included. It also matches pooled views. reload({ dependents: true }), next tocleanup({ dependents: true }). Opt-in, and plainreload()stays as it is. It doesn't fix the retry pattern from feat(db): split Collection status from sync errors; add reload() and cleanup admission #2168 alone:q1.reload()still leavesq2stuck, anditems.reload()still leaves both stuck.
Activity