Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Garbage collection cleans up a collection while one of its transactions is still persisting

オープン
#2,256 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@Nabil372 がすでに取り組んでいます。

2026年10月24日 から。

  • #2 @Nabil372 による — オープン
  • #2258 @Nabil372 による — オープン

評価

難易度
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
フォーク
277
平均マージ
17時間 40分
マージ済み PR(30日)
223

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

TanStack/db のほかの issue

TanStack/db の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。