`batch-transact-with-temp-conn!` leaves `:max-tx` behind its datoms, so the next transaction reuses a tx id
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 70/100
Hướng nghiên cứu
Bắt đầu tại deps/db/src/logseq/db.cljs, ở batch-transact-with-temp-conn! (L273) và lệnh gọi transact! của nó (L312): so sánh cách các bản ghi Datom được phát lại giữ nguyên tx id tạm thời trong khi :max-tx chỉ tăng một lần. Chạy bài kiểm tra tái hiện tại src/test/logseq/db/batch_transact_tx_id_repro_test.cljs với các lệnh trong issue; áp dụng transact không có tx-ops được đề xuất rồi chạy lại. Hoàn thành khi cả hai assertion đều đạt và tx id của giao dịch tiếp theo chưa được sử dụng; get-storage-conn trong deps/db/src/logseq/db/common/sqlite.cljs là ngữ cảnh liên quan.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Search first
- I searched and no similar issues were found
What Happened?
Continuing the Opus 5.5 epic from last week. Claude follows.
ldb/batch-transact-with-temp-conn! runs an operation on a temporary conn, possibly as several transactions, and then replays the collected datoms into the real conn with one transact!. The replayed datoms keep the tx ids of the temporary transactions, but the real conn's :max-tx advances only once. When the operation made more than one inner transaction, the conn's :max-tx ends up lower than the tx of datoms it holds, and the next transaction takes a tx id that earlier datoms already carry.
outliner-op/apply-ops! goes through this function, so ordinary edits reach it: in our test an insert followed by a move produced a transaction report whose datoms carried two tx ids while :max-tx covered only the first.
Reproduce the Bug
Save this test as src/test/logseq/db/batch_transact_tx_id_repro_test.cljs, then run
clojure -M:test compile test
node static/tests.js -n logseq.db.batch-transact-tx-id-repro-test
(ns logseq.db.batch-transact-tx-id-repro-test
(:require [cljs.test :refer [deftest is]]
[datascript.core :as d]
[logseq.db :as ldb]))
(deftest batch-transact-with-temp-conn-keeps-tx-ids-unique-test
(let [conn (d/create-conn {})]
(d/transact! conn [{:db/id -1 :item/name "a"}])
(ldb/batch-transact-with-temp-conn!
conn {}
(fn [temp-conn _]
(d/transact! temp-conn [{:db/id -1 :item/name "b"}])
(d/transact! temp-conn [{:db/id -1 :item/name "c"}])))
(let [db @conn
max-datom-tx (reduce max (map :tx (d/datoms db :eavt)))]
(is (>= (:max-tx db) max-datom-tx))
(let [new-tx (:tx (first (:tx-data (d/transact! conn [{:db/id -1 :item/name "d"}]))))]
(is (not-any? #(= new-tx (:tx %)) (d/datoms db :eavt)))))))
Expected Behavior
After the batch, :max-tx is at least the tx of every datom, and the next transaction gets a tx id that no earlier datom carries.
Actual on master logseq/logseq@9af1a190bc:
FAIL expected: (>= (:max-tx db) max-datom-tx)
actual: (not (>= 536870914 536870915))
FAIL expected: (not-any? (fn* [p1__155722#] (= new-tx (:tx p1__155722#))) (d/datoms db :eavt))
actual: (not (not-any? #object[Function] (#datascript/Datom [1 :item/name "a" 536870913 true] #datascript/Datom [2 :item/name "b" 536870914 true] #datascript/Datom [3 :item/name "c" 536870915 true])))
The next transaction takes tx 536870915, which datom 3 already carries.
Screenshots
No response
Files
No response
Browser, Desktop or Mobile Platform Information
Unit test on master logseq/logseq@9af1a190bc (2026-11-19), Node 24, Linux x86_64. No UI involved.
Additional Context
Cause. batch-transact-with-temp-conn! (logseq/db.cljs#L273) collects the temporary conn's tx-data as Datom records and passes them to transact! (L312). Datascript inserts a Datom record with its own tx, and increments :max-tx once per transact!.
Related code. get-storage-conn (common/sqlite.cljs#L23) already raises :max-tx on restore to the largest tx of the tail's datoms (tail-max-tx), and its docstring describes these multi-tx entries. That covers a reopen from the tail, but not the conn in memory, and not a full store, which writes the lower :max-tx into the root. logseq/logseq#13911 moves that restore code without changing this.
Possible effects (not verified). The worker pipeline stamps :block/tx-id with (inc (:max-tx db-after)) (pipeline.cljs#L743), so two operations can stamp the same revision. Anything that keys data by tx id can merge two transactions.
Suggested fix. Transact the collected changes as ops without their tx, so the final transact! stamps them all with one new tx id:
(transact! conn
(mapv (fn [d] [(if (:added d) :db/add :db/retract) (:e d) (:a d) (:v d)]) tx-data)
(transform-tx-meta tx-meta))
On our fork this makes the test above pass, the whole unit-test suite passes with it (2,744 tests, bb dev:run-test-namespaces), and a storage property test that checks :max-tx after every transaction (100 random sequences with write failures and crashes) no longer finds the problem. I can open a PR with the change and the test.
Are you willing to submit a PR? If you know how to fix the bug.
- I'm willing to submit a PR (Thank you!)
- Ngôn ngữ chính
- Không có dữ liệu ngôn ngữ
- Star
- 29
- Fork
- 2
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
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 logseq/db-test
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
-
Potential Dataloss: Deleting an entry from the property page's node table has no confirmation dialogCó thể đã có người làm @tiensonqin đã nhận 59 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
Disabling Flashcards in Settings does not disable Flashcard shortcuts or tagsCó thể đã có người làm @tiensonqin đã nhận 60 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Tất cả issue của logseq/db-test
Issue tương tự
-
HTTP `batch.Send` returns nil and inserts no rows when the block does not encodeCó thể đã có người làm @Indian-boult đã nhận hôm nay. Đang mởbug queries
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
ClickHouse/clickhouse-go#2155 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Fix Crash When Event ExpiresĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
-
PNI-only contacts synced from Storage Service are left with RegisteredState.UNKNOWN and never appear in New ChatCó thể đã có người làm @ArviFoxy đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
signalapp/Signal-Android#15142 · 1 bình luận ·
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 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bug P2 tools
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày