Garbage collection cleans up a collection while one of its transactions is still persisting
Maintainer antworten meist innerhalb von 1 Tag
@Nabil372 arbeitet bereits daran.
Seit 24.10.2026.
Bewertung
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Anfängerfreundlichkeit
- 35/100
- Issue-Typ
- Bug
- Klarheit
- Klar beschrieben
- Aktivitätsstatus
- Aktiv
- Tech-Stack
- typescript
- Bereich
- database
Rechercherichtung
Start with packages/db/src/collection/lifecycle.ts and trace how canGarbageCollect() determines whether cleanup can proceed. Then inspect the transaction lifecycle and existing generated-history tests mentioned in the issue. Done means a persisting transaction keeps the collection and its optimistic state alive until it settles or rolls back, while explicit cleanup() retains its current behavior.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
- I've validated the bug against the latest version of DB packages
(Checked on main and on 0.15.2.)
Describe the bug
A collection with no subscribers is garbage collected after gcTime even when one of its transactions is still persisting. The optimistic row disappears while the mutation handler is still running. With the Electric collection, cleanup also rejects the handler's pending awaitTxId with StreamAbortedError, so the write fails even though the server accepted it.
We use TanStack DB with Electric in production in web apps and in Expo apps on iOS and Android, and this is easy to hit on mobile. A screen inserts a row and closes; the handler is waiting on a slow request, or on a txid that arrives late because the app went to the background. Once gcTime passes, the write is lost from the client's point of view.
canGarbageCollect() in packages/db/src/collection/lifecycle.ts checks for a cleanup in progress, active subscribers, and a pending preload, but not for persisting transactions.
To Reproduce
import { createCollection } from '@tanstack/db'
let release!: () => void
const handlerDone = new Promise<void>((resolve) => (release = resolve))
const collection = createCollection<{ id: string }, string>({
id: 'gc-during-persist',
getKey: (row) => row.id,
gcTime: 10,
startSync: true,
sync: {
sync: ({ markReady }) => {
markReady()
return () => {}
},
},
onInsert: async () => {
await handlerDone
},
})
const subscription = collection.subscribeChanges(() => {})
const tx = collection.insert({ id: 'a' })
subscription.unsubscribe()
await new Promise((resolve) => setTimeout(resolve, 2000))
console.log(collection.status, collection.has('a'), tx.state)
// cleaned-up false persisting
release()
Expected behavior
While a transaction that touches the collection is persisting, the collection is not garbage collected, so its optimistic state and any awaitTxId or awaitMatch wait survive until the transaction settles. GC can start its timer once the transaction completes or rolls back. An explicit cleanup() would keep its current behavior.
In production we work around it by holding the collection with a subscription for the length of each write, using subscribeChanges(() => {}, { includeInitialState: false, deferAcquisition: true }) so the hold does not count as a reader. deferAcquisition is marked @internal, so we would rather not depend on it.
If holding a collection while its transaction persists is not the intended behavior, a public way to hold a collection for the length of a write would cover the same need.
I have a fix with a generated-history test and will open a pull request for it.
Desktop: Node 24, jsdom (Vitest). Smartphone: React Native (Expo), iOS and Android, where screens close and apps background while writes are in flight.
- Vorherrschende Sprache
- TypeScript
- Sterne
- 4k
- Forks
- 277
- Ø Merge
- 17 Std. 40 Min.
- Gemergte PRs (30 T.)
- 223
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus TanStack/db
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
Maintainer antworten meist innerhalb von 1 Tag
-
persistedCollectionOptions without a schema can't be passed to createCollection (follow-up to #1452)Offen
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 68/100
Maintainer antworten meist innerhalb von 1 Tag
-
Electric: scoped move-in snapshots can prematurely resolve awaitTxId and remove optimistic rowsOffen
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 38/100
Maintainer antworten meist innerhalb von 1 Tag
Ähnliche Issues
-
a10y a10y-audit ai-driven-qa claude
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 86/100
linagora/twake-directory-manager#219 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
Maintainer antworten meist innerhalb von 1 Tag
-
type: bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 79/100
Maintainer antworten meist innerhalb von 1 Tag
-
0️⃣ phase/new
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
Maintainer antworten meist innerhalb von 1 Tag
-
Pulse v7: buildPreferencesFromTelos() bypasses telosSectionOrFile(), renders empty on unified-TELOS installsEvtl. vergeben @zephyr91 hat das heute übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
danielmiessler/LifeOS#2393 ·
Maintainer antworten meist innerhalb von 4 Tagen