Skip to content
Source
Sign inSign up
TanStack/db

DbClient.cleanup() cleans up a live query's source before the live query, logging an error on every SSR request #2293

ClosedJaime02(GitHub profile, opens in a new tab) opened this issue 1 comment

Problem

On the server, routerWithDbClient cleans up the request's DbClient once the render finishes. When one useLiveQuery reads from the collection another useLiveQuery returned (as in "Caching Intermediate Results"), every request logs:

[Live Query Error] Source collection 'live-query-2' was manually cleaned up while live query 'live-query-3' depends on it. Live queries prevent automatic GC, so this was likely a manual cleanup() call.

DbClient.cleanup() runs every registered live query cleanup at once, in creation order. The source query was created first, so it's cleaned up before the query that depends on it, and the dependent query reports an error. A single unchained live query cleans up silently.

Repro

@tanstack/db 0.15.1, @tanstack/react-db 0.5.11, React 19.3.1, Bun:

import { DbClient, DbProvider, collectionOptions, localOnlyCollectionOptions, useLiveQuery } from '@tanstack/react-db'
import { renderToString } from 'react-dom/server'

const todos = collectionOptions('todos', () =>
  localOnlyCollectionOptions({ id: 'todos', getKey: (t: { id: number }) => t.id, initialData: [{ id: 1 }, { id: 2 }] }),
)

function Todos() {
  const { collection: base } = useLiveQuery({ queryKey: ['base'], query: (q) => q.from({ todo: todos }) })
  const { data } = useLiveQuery({ queryKey: ['page', base.id], query: (q) => q.from({ todo: base }) })
  return <p>{data.length}</p>
}

const client = new DbClient()
client._setSsrServerCleanupEnabled(true) // what routerWithDbClient does on the server
renderToString(<DbProvider client={client}><Todos /></DbProvider>)
await client.cleanup() // logs the error above

Expected

A whole-client cleanup() tears the queries down without errors: either dependents before their sources, or no source-cleaned error for a query the same cleanup is about to clean up anyway.

  1. Confirmed the reported bug on the source tagged @tanstack/db@0.15.1 / @tanstack/react-db@0.5.11. It is already fixed by #2168.

    Using the reduced React SSR reproduction with server cleanup enabled:

    Source revisionSingle live queryChained live queries
    31dad2dd3b835310612747c5af457561aa77e997 (both reported release tags)Silent cleanupLogs the exact reported source-manually-cleaned error
    7d9ed02e085d11e871e4b6424d47e3c0d7073023 (#2168)Silent cleanupSilent cleanup
    c351fc04d7f70dc59bf92ecbed310f899b9511cd (tested main)Silent cleanupSilent cleanup

    The fix changes the registered SSR resource callback to collection.cleanup({ dependents: true }), which ends dependent live queries before cleaning up their source. The callbacks can still run concurrently without producing this error.

    Additional local verification covers all 24 combinations of one/two/four query levels, one/two children per parent, empty/nonempty sources, and cold/preloaded sources. After each of two cleanups, every owned Collection is cleaned up, its live-query dependencies are gone, and no errors were logged. Temporarily removing { dependents: true } makes all 16 chained histories fail while the eight single-query controls still pass. All 391 relevant tests pass with the existing fix restored. These additional regression tests are local and have not yet been merged.

    The historical probes used exported release-tag source with installed dependencies under Node 24.19.0 and React/React DOM 19.2.4; Bun and the reporter's exact React version were not tested.

    Closing as fixed by #2168. The reported older versions remain affected; a version containing that change is needed.

  2. KyleAMathews closed this

Sign in to join this conversation.