Skip to content

Live queries stay in error after their source recovers through reload() #2482

Backlog

Description

@adamkasper

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 calls failQuery(), which sets fatalQueryError on every live query over it that isn't ready yet.
  • collection-config-builder.ts:862, 1455-1464: once the source is ready, maybeRunGraph() and updateLiveQueryStatus() return early while fatalQueryError is set. Only the query's own new sync run clears it (lines 976, 1066).
  • collection-config-builder.ts:156-179: a live query's reload() 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 ready with no error.
  • Pooled views behave differently: a pooled view takes status and health.error from 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

  1. Recover with the source (preferred). When a source goes from error to ready, 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 leaving error still goes through a reload and a new sync run. Both q1.reload() and items.reload() then recover every query over items, nested ones included. It also matches pooled views.
  2. reload({ dependents: true }), next to cleanup({ dependents: true }). Opt-in, and plain reload() 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 leaves q2 stuck, and items.reload() still leaves both stuck.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions