Sync metadata reads skip an earlier held transaction's writes while an optimistic transaction persists
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 72/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- typescript
- Lĩnh vực
- database
Hướng nghiên cứu
Start in packages/db/src/collection/sync.ts at CollectionSyncManager.createSyncMetadataApi, then inspect how pendingSyncedTransactions and syncedCollectionMetadata feed collection.get and collection.list. Reproduce with the supplied Vitest case: hold committed sync transactions behind a persisting optimistic transaction, then read metadata. Done when reads include writes from all held transactions in newest-write-first order and the reproduction passes.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
- I've validated the bug against the latest version of DB packages (
@tanstack/db0.15.2,@tanstack/react-db0.5.12)
Describe the bug
While an optimistic transaction is persisting, core holds committed sync transactions until it settles. During that window, metadata.collection.get(key) in a sync function only reads the newest pending sync transaction. If that one didn't write key, the read falls through to the applied metadata. A value an earlier held transaction wrote is skipped, and the read returns the old value (or undefined). metadata.collection.list() has the same gap.
This bites a sync source that keeps a resume cursor in collection metadata. It writes the cursor in one transaction and rows in the next, then reads the cursor back to fetch the next page. The read returns the old cursor, so the source fetches pages it already has. Once the optimistic transaction settles and the held transactions apply, the read is right again.
Cause
CollectionSyncManager.createSyncMetadataApi (packages/db/src/collection/sync.ts) reads this.state.pendingSyncedTransactions.at(-1) and then syncedCollectionMetadata. It doesn't walk the earlier entries of pendingSyncedTransactions. collection.list merges only .at(-1) too, and row.get uses the same pattern. I've reproduced collection.get and collection.list; row.get is from reading the source.
The persisted wrapper already reads its own staged writes newest first (readStagedMetadata in db-sqlite-persistence-core createWrappedSyncConfig). Once it has handed a transaction to core, though, the read lands back in core's .at(-1) lookup.
To Reproduce
Fails on 0.15.2:
import { createCollection, createTransaction } from '@tanstack/db'
import type { SyncConfig } from '@tanstack/db'
import { expect, it } from 'vitest'
type Todo = { id: string; title: string }
it(`reads collection metadata written by an earlier held sync transaction`, async () => {
let sync!: Parameters<SyncConfig<Todo, string>[`sync`]>[0]
const todos = createCollection<Todo, string>({
id: `todos`,
getKey: (todo) => todo.id,
startSync: true,
sync: {
sync: (params) => {
sync = params
params.begin()
params.commit()
params.markReady()
},
},
})
await todos.preload()
// A persisting optimistic transaction makes core hold committed sync transactions.
let release!: () => void
const gate = new Promise<void>((resolve) => {
release = resolve
})
const tx = createTransaction({ autoCommit: false, mutationFn: () => gate })
tx.mutate(() => todos.insert({ id: `1`, title: `optimistic` }))
const persisted = tx.commit()
sync.begin()
sync.metadata?.collection.set(`cursor`, 5)
sync.commit()
sync.begin()
sync.write({ type: `insert`, value: { id: `2`, title: `from server` } })
sync.commit()
expect(sync.metadata?.collection.get(`cursor`)).toBe(5) // received undefined
expect(sync.metadata?.collection.list()).toEqual([{ key: `cursor`, value: 5 }]) // received []
release()
await persisted
expect(sync.metadata?.collection.get(`cursor`)).toBe(5) // passes
})
Expected behavior
A metadata read inside a sync function sees every committed sync transaction, newest write first, before the applied metadata, whether or not core is holding them. list() merges the same way.
Workaround
We keep a map of the metadata our own held transactions wrote and read it before metadata.collection.get, dropping each entry once that transaction's commit receipt settles.
Additional context
main still has the .at(-1) reads. A fix could walk pendingSyncedTransactions from the newest entry back, stopping at the first write for the key, or at a truncate for row metadata as the current code does. list() would apply them oldest first over syncedCollectionMetadata.
- Ngôn ngữ chính
- TypeScript
- Star
- 3.9k
- Fork
- 277
- Merge trung bình
- 17 giờ 54 phút
- Pull request đã merge (30 ngày)
- 226
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của TanStack/db
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
Maintainer thường phản hồi trong vòng 1 ngày
-
SQLite persistence: a sync transaction whose durable write fails still changes the public CollectionĐang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
Maintainer thường phản hồi trong vòng 1 ngày
-
sqlite-persistence: leader term write on every page load delays reading rowsCó thể đã có người làm @KyleAMathews đã nhận hôm nay. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 50/100
Maintainer thường phản hồi trong vòng 1 ngày
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
chaitin/MonkeyCode#1437 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
rtdb-info-connected-order test: unhandled 'connection is closed' rejection fails the CLI suiteĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 69/100
carbon-design-system/carbon-ai-chat#2615 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
e2e-failure ready-to-code
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
redhat-developer/rhdh-plugin-export-overlays#4486 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Bug]: fileUploadMatrix.test.ts URL case does a real DNS lookup and fails offlineCó thể đã có người làm @r2jaff đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
ComposioHQ/composio#5054 ·
Maintainer thường phản hồi trong vòng 1 ngày