Garbage collection cleans up a collection while one of its transactions is still persisting
維護者通常 1 天內回覆
評估
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 新手友好度
- 35/100
- Issue 類型
- 缺陷
- 描述清晰度
- 描述清楚
- 活躍度
- 活躍
- 技術堆疊
- typescript
- 領域
- database
研究方向
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.
由索引模型根據 Issue 內容生成。
描述
- 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.
- 主要語言
- TypeScript
- 星號
- 4k
- 分支
- 280
- 平均合併
- 1 天 10 小時
- 30 天內合併 PR
- 132
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
TanStack/db 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 76/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆
-
難度 1/5 1-3 小時 新手友好度 88/100
維護者通常 1 天內回覆
-
OfflineExecutor.dispose() never settles pendingTransactionPromises可能重新可做 @Claire-ovo 於 12 天前認領,目前沒有進行中的 PR。 未關閉
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆
相似的 Issue
-
desktop v3 windows
難度 2/5 1-3 小時 新手友好度 75/100
esengine/DeepSeek-Reasonix#15016 · 1 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 72/100
readest/readest#7413 · 1 則留言 ·
維護者通常 1 天內回覆
-
enhancement good first issue priority: medium size: XS
難度 2/5 1-3 小時 新手友好度 85/100
維護者通常 1 天內回覆
-
content triage
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 75/100
eliasstravik/herdr-projects#788 ·