Local-only persisted startup calls markReady after collection cleanup #1889
Versions#
@tanstack/db-sqlite-persistence-core@0.2.23(latest published)@tanstack/db@0.9.2- Local-only
persistedCollectionOptions(no upstream sync adapter)
Bug#
createLoopbackSyncConfig() starts runtime.ensureStarted() asynchronously and calls params.markReady() in both its fulfillment and rejection callbacks. Its cleanup() calls runtime.cleanup() but does not invalidate those already-registered callbacks.
A deterministic sequence is:
- Create a local-only persisted collection with an adapter whose initial SQLite hydration/read is held on a deferred Promise.
- Start
collection.preload(). - Call
collection.cleanup()while the startup read is pending. - Settle the read, either by resolving it or rejecting it.
The late callback calls markReady() on the cleaned collection. In our account-switch/logout path this produces InvalidCollectionStatusTransitionError (cleaned-up -> ready) and an unhandled rejection. The rejection branch also logs a stale startup warning after the collection has been disposed.
The published dist/esm/persisted.js path is the same in current main: createLoopbackSyncConfig calls params.markReady() after ensureStarted().then(...) and in .catch(...), while cleanup only calls runtime.cleanup() / runtime.clearSyncControls().
Expected: Cleanup invalidates both startup continuations; neither calls markReady() or logs an obsolete startup error after cleanup. A new collection/session can then start independently.
Workaround: We carry a small package patch with a cleanedUp flag set before runtime cleanup, checked in both startup outcomes. Related app-side tracking: https://github.com/digitalcontour/oreol-monorepo/issues/299.
This is distinct from #1576 (preload after cleanup) and from the sync-present readiness policy in #1659. PR #1853 addresses several persistence lifecycle failures, but its current createLoopbackSyncConfig diff does not appear to fence the late markReady() callback; the regression above would be useful alongside it.
KyleAMathews closed this
Sign in to join this conversation.
Closing as resolved by current main. The Collection sync-run callback checks its epoch before markReady (https://github.com/TanStack/db/blob/236d77bc/packages/db/src/collection/sync.ts#L300-L307). PR #1895 added a regression test for both late startup fulfillment and rejection after cleanup (https://github.com/TanStack/db/blob/236d77bc/packages/db-sqlite-persistence-core/tests/persisted.test.ts#L6357-L6440). The focused test passes in both cases: the collection stays cleaned-up, readiness rejects with AbortError, no stale warning or unhandled rejection appears, and an independent peer reaches ready. A distinct remaining failure can be filed separately.