Repository navigation
fix(db): a failed graph write no longer leaves a graph that does not reopen; batch and kv-store.json writes keep memory and disk in step - #13911
Conversation
| (swap! *last-write | ||
| (fn [last-write] | ||
| (p/then (p/catch last-write (constantly nil)) | ||
| (fn [_] (<write-key! k value))))))})) |
There was a problem hiding this comment.
🔴 Concurrent graph workers lose saved keys
When graph workers save different keys concurrently, set! lets both read the same old file. Each worker has its own *last-write, so the last rename silently erases the other's saved key.
Learn more
Each graph worker creates a separate kv-store and therefore a separate *last-write queue. Both workers can read the same old state, write different temporary files, and successfully rename them onto the shared path. The second rename discards the first worker's key; rereading on every operation does not prevent this read-modify-write race. The existing test writes from the two workers sequentially, so it never exercises the overlap.
Example: Workers X and Y both read {}. X saves "password", while Y saves "aes-key". If Y renames last, both calls succeed but the file contains only "aes-key".
Recommended fix: Serialize the entire read-modify-rename operation across worker processes with an interprocess lock, or store independent keys using an atomic keyed storage operation. Add a test that overlaps the two workers' reads and checks both saved keys after both writes complete.
Was this helpful? React with 👍 or 👎 to provide feedback.
| (deftest-async journal-file-and-page-file-for-same-date-make-one-valid-journal | ||
| (p/let [conn (import-files! [["journals/2023_06_21.md" "- j1"] | ||
| ["pages/Jun 21st, 2023.md" "- p1"]]) | ||
| page (db-test/find-journal-by-journal-day @conn 20230621) | ||
| page-tags (set (map :db/ident (:block/tags page)))] | ||
| (is (= #{"j1" "p1"} (page-block-titles page)) "Both files' blocks are on the journal page") | ||
| (is (contains? page-tags :logseq.class/Journal) "The page is a journal") | ||
| (is (not (contains? page-tags :logseq.class/Page)) "The journal is not also tagged #Page") |
|
Thanks, both fixed in 8f50694 and 9430803. kv-store.json across processes (platform/node.cljs). Agreed, the reread only narrowed the window. The read, write and rename of a #1378 in the reverse order (exporter_page_merge_test.cljs). I added The deps/db suite (204 tests, 0 failures) and the graph-parser suite (120 tests, 0 failures) pass. |
66587c6 to
d952d6c
Compare
…hat does not reopen; batch and kv-store.json writes keep memory and disk in step (squashed: its commits conflicted one by one with the stack)
e1012b8 to
38ee386
Compare
…hat does not reopen; batch and kv-store.json writes keep memory and disk in step (squashed: its commits conflicted one by one with the stack)
38ee386 to
d9de376
Compare
…hat does not reopen; batch and kv-store.json writes keep memory and disk in step (squashed: its commits conflicted one by one with the stack)
…hat does not reopen; batch and kv-store.json writes keep memory and disk in step (squashed: its commits conflicted one by one with the stack)
d9de376 to
7529f66
Compare
…hat does not reopen; batch and kv-store.json writes keep memory and disk in step (squashed: its commits conflicted one by one with the stack)
…hat does not reopen; batch and kv-store.json writes keep memory and disk in step (squashed: its commits conflicted one by one with the stack)
7529f66 to
0a6374e
Compare
|
Opened logseq/db-test#1525 to track the related db-sync server partial-durable-flush / allocator-restart branch separately. The synthetic reproduction uses the exact deployed server storage bundle: interrupted KVS writes leave durable addresses beyond root |
…hat does not reopen; batch and kv-store.json writes keep memory and disk in step (squashed: its commits conflicted one by one with the stack)
c1629d9 to
db8a020
Compare
…hat does not reopen; batch and kv-store.json writes keep memory and disk in step (squashed: its commits conflicted one by one with the stack)
db8a020 to
36ddd6a
Compare
…hat does not reopen; batch and kv-store.json writes keep memory and disk in step (squashed: its commits conflicted one by one with the stack)
…hat does not reopen; batch and kv-store.json writes keep memory and disk in step (squashed: its commits conflicted one by one with the stack)
…hat does not reopen; batch and kv-store.json writes keep memory and disk in step (squashed: its commits conflicted one by one with the stack)
36ddd6a to
284c43f
Compare
…hat does not reopen; batch and kv-store.json writes keep memory and disk in step (squashed: its commits conflicted one by one with the stack)
…e new block The test pressed Enter, slept 60 ms and pressed Enter again to turn the empty numbered block back into a bullet. An Enter pressed while the previous insert is pending is dropped (pinned by master's enter-while-insert-is-pending-is-dropped-test), so on a slow runner the second Enter was lost and 4 numbered items stayed (CI on logseq#13911; 1 of 35 loaded runs here). It now waits until the editor is on the new block, as block/new-block does.
…e new block The test pressed Enter, slept 60 ms and pressed Enter again to turn the empty numbered block back into a bullet. An Enter pressed while the previous insert is pending is dropped (pinned by master's enter-while-insert-is-pending-is-dropped-test), so on a slow runner the second Enter was lost and 4 numbered items stayed (CI on logseq#13911; 1 of 35 loaded runs here). It now waits until the editor is on the new block, as block/new-block does.
284c43f to
bcff58d
Compare
…e new block The test pressed Enter, slept 60 ms and pressed Enter again to turn the empty numbered block back into a bullet. An Enter pressed while the previous insert is pending is dropped (pinned by master's enter-while-insert-is-pending-is-dropped-test), so on a slow runner the second Enter was lost and 4 numbered items stayed (CI on logseq#13911; 1 of 35 loaded runs here). It now waits until the editor is on the new block, as block/new-block does.
…hat does not reopen; batch and kv-store.json writes keep memory and disk in step (squashed: its commits conflicted one by one with the stack)
…e new block The test pressed Enter, slept 60 ms and pressed Enter again to turn the empty numbered block back into a bullet. An Enter pressed while the previous insert is pending is dropped (pinned by master's enter-while-insert-is-pending-is-dropped-test), so on a slow runner the second Enter was lost and 4 numbered items stayed (CI on logseq#13911; 1 of 35 loaded runs here). It now waits until the editor is on the new block, as block/new-block does.
bcff58d to
d873f96
Compare
…e new block The test pressed Enter, slept 60 ms and pressed Enter again to turn the empty numbered block back into a bullet. An Enter pressed while the previous insert is pending is dropped (pinned by master's enter-while-insert-is-pending-is-dropped-test), so on a slow runner the second Enter was lost and 4 numbered items stayed (CI on logseq#13911; 1 of 35 loaded runs here). It now waits until the editor is on the new block, as block/new-block does.
d873f96 to
1703c56
Compare
…e new block The test pressed Enter, slept 60 ms and pressed Enter again to turn the empty numbered block back into a bullet. An Enter pressed while the previous insert is pending is dropped (pinned by master's enter-while-insert-is-pending-is-dropped-test), so on a slow runner the second Enter was lost and 4 numbered items stayed (CI on logseq#13911; 1 of 35 loaded runs here). It now waits until the editor is on the new block, as block/new-block does.
…hat does not reopen; batch and kv-store.json writes keep memory and disk in step (squashed: its commits conflicted one by one with the stack)
…e new block The test pressed Enter, slept 60 ms and pressed Enter again to turn the empty numbered block back into a bullet. An Enter pressed while the previous insert is pending is dropped (pinned by master's enter-while-insert-is-pending-is-dropped-test), so on a slow runner the second Enter was lost and 4 numbered items stayed (CI on logseq#13911; 1 of 35 loaded runs here). It now waits until the editor is on the new block, as block/new-block does.
1703c56 to
f379ac9
Compare
…e new block The test pressed Enter, slept 60 ms and pressed Enter again to turn the empty numbered block back into a bullet. An Enter pressed while the previous insert is pending is dropped (pinned by master's enter-while-insert-is-pending-is-dropped-test), so on a slow runner the second Enter was lost and 4 numbered items stayed (CI on logseq#13911; 1 of 35 loaded runs here). It now waits until the editor is on the new block, as block/new-block does.
…hat does not reopen; batch and kv-store.json writes keep memory and disk in step (squashed: its commits conflicted one by one with the stack)
…e new block The test pressed Enter, slept 60 ms and pressed Enter again to turn the empty numbered block back into a bullet. An Enter pressed while the previous insert is pending is dropped (pinned by master's enter-while-insert-is-pending-is-dropped-test), so on a slow runner the second Enter was lost and 4 numbered items stayed (CI on logseq#13911; 1 of 35 loaded runs here). It now waits until the editor is on the new block, as block/new-block does.
…hat does not reopen; batch and kv-store.json writes keep memory and disk in step (squashed: its commits conflicted one by one with the stack)
f379ac9 to
ba6f042
Compare
With two graphs open on desktop or through the CLI, a value one graph's worker saved in kv-store.json (the E2EE password when the OS keychain is unavailable, cached keys) was dropped when the other worker saved its own value, so Logseq asked for the E2EE password again after a restart. A crash during a write left the file truncated, and it then read as empty. Each worker process read the file once, cached the whole map, and wrote that map back on every set! with a plain writeFile. Concurrent set! calls in one process were not ordered either. get now reads the file, and set! merges its key into the file's current content and replaces the file by a write to a temp file and a rename. The set! calls of one process run one after another. Test: platform-node-kv-store-test saves a key from each of two workers on one root dir after both have read the file, and saves 10 keys at once.
When a listener threw on the final report of ldb/batch-transact! (or another error came after the batch's store), the in-memory db was reset to the db before the batch while the graph file already held the batch. The app then showed the graph without the batch's writes, and the two differed until the next full store or a reload. The catch of batch-transact! reset the conn on every error, including those after d/store had returned. batch-transact! now resets the conn only when the error came before the store returned. After that it rethrows without a reset. Test: batch-transact-store-test throws from a listener on the batch's final report and checks the batch is both in memory and in the reopened file, and that a batch that throws before its store is still rolled back.
…k file Two graph worker processes that saved a kv-store key at the same time could still lose one: both read the old kv-store.json, each wrote its temp file and renamed it, and the last rename won. The read, write and rename of a set! now run while the process holds kv-store.json.lock, created with O_EXCL. A writer that finds the lock retries with a short backoff for up to 10 s. A lock older than 5 s was left by a process that died and is removed. Tests: platform-node-kv-store-test has four kv-stores on one root dir, each with its own write queue as a separate process has, save 20 keys at once, and every key is in the file (15 were lost without the lock). It also checks that a write waits for a held lock and removes a stale one.
…leaves a graph that does not reopen When one write of the graph failed (disk full, I/O error, or any error from the storage) and a later one succeeded, the graph no longer opened: restoring it threw "Cannot read properties of null (reading 'keys')", or it reopened with eavt and aevt indexes that differ. The app had shown no sign of it after the failed edit. A datascript full store marks its b-tree nodes as stored and gives them addresses before the storage's -store writes their rows, and a failed -store does not undo the marks. The next full store skips those nodes and writes a root that points at rows that were never written. A failed store also leaves the conn's tail bookkeeping out of step with the stored root. ldb/transact! and ldb/batch-transact! now reload the conn from its storage when a store throws, whatever the storage and whatever the error, and then rethrow. The restore that get-storage-conn did (with the tail's largest tx) moves to ldb/restore-storage-conn so both use it. Test: store-failure-reload-test makes a store throw before and after the write, through ldb/transact! and ldb/batch-transact!, and checks the reopened graph has the blocks the session shows. The storage fuzzer (30 seeds) is clean.
The reload and restore fns sat right after the commented debounced-store-db, which logseq#13786 removes, so the two PRs conflicted in deps/db/src/logseq/db.cljs. Move tail-max-tx, restore-storage-conn and reload-conn-from-storage! unchanged to just before transact-sync, their first caller. No behaviour change.
…wn fn transact-sync was at the 50-line limit of the large-vars lint; together with another open PR that adds a check to it, it went over. The write that skips the pipeline, with its reload on a failed store, is now transact-reloading-on-failure!; behaviour is unchanged.
…stored node address logseq/db-test#1525: the server's storage wrote a store's node rows one by one outside any transaction, so an interrupted store could leave rows above the :max-addr the root records. DataScript's address counter starts from that :max-addr after a restart, so later stores handed out those addresses and overwrote rows the restored indexes could still read; traversal then failed with `null.keys` while SQLite's quick_check stayed ok. A store now writes all of its rows in one transaction, and opening a graph raises the address counter above the largest address stored, which also covers graphs that already have such rows.
ba6f042 to
c5e8cda
Compare
…hat does not reopen; batch and kv-store.json writes keep memory and disk in step (squashed: its commits conflicted one by one with the stack)
|
This PR fixes some problems my test suite found. |
Three storage fixes and one kv-store fix, one commit each. This PR first also held file-graph import fixes; they moved to #13886, which is the import PR. That includes the fix for logseq/db-test#1378.
A failed graph write followed by a successful one leaves a graph that does not reopen
What the user sees. The disk fills up (or sqlite / OPFS hits an I/O error) during an edit, and that edit fails with "database or disk is full". The app keeps showing every edit. After space is freed, later edits save without error. On the next reload the graph does not open: restoring it throws
TypeError: Cannot read properties of null (reading 'keys'), and only an older backup brings it back.Cause. A datascript full store (one runs every few edits, when the tail passes 32 datoms) marks its b-tree nodes as stored and gives them addresses before the storage's
-storewrites their rows. When-storefails, the sqlite transaction rolls back, but nothing undoes the marks. The next full store skips those nodes and writes a root that points at rows that were never written. A failed store also leaves the conn's tail bookkeeping (:tx-tail,:db-last-stored) out of step with the stored root.Change. When a store throws, in any storage and at any point of the write,
ldb/transact!andldb/batch-transact!reload the conn from its storage and then rethrow. After the reload, memory holds exactly what the storage holds, and the failed edit is reported as failed. The restore thatget-storage-conndid (which takes the largest tx of the tail) moves toldb/restore-storage-conn, so both paths use it. The fix is on the Logseq side; the datascript fork is unchanged. Writes that calld/transact!directly on a stored conn do not go throughldb/transact!and are not covered.Tests.
logseq.db.store-failure-reload-test(deps/db) makes a store throw before the write and after it, throughldb/transact!andldb/batch-transact!on a real sqlite file, writes again, and checks that the reopened graph has the blocks the session shows. Without the change, a store that throws before the write leaves its blocks in memory but not on disk. The storage fuzzer (mutation/lib/storage_fuzz_test.cljs: I/O error before the write, SQLITE_FULL, error after the write, listener errors, crashes and kvs GC) passes all 30 seeds, with 0opens,indexesandequalsfailures. On master and on the first version of this fix it failed 19 of 30.A batch that fails after its store loses its writes in memory but not on disk
What the user sees. When an error comes after
ldb/batch-transact!has stored its batch (for example a listener that throws on the batch's final report), the app shows the graph without the batch's writes, but the graph file has them. Memory and file differ until the next full store or a reload.Cause. The catch of
batch-transact!reset the conn to the db before the batch on every error, including errors afterd/storehad returned.Change.
batch-transact!resets the conn only when the error came before the store returned. After that it rethrows without resetting. When the store itself fails, the conn is reloaded from the storage (see above).Tests.
logseq.db.batch-transact-store-testthrows from a listener on the batch's final report and checks that the batch is both in memory and in the reopened file (fails on master). It also checks that a batch that throws before its store is still rolled back.kv-store.json loses entries between graph workers
What the user sees. With two graphs open on desktop or through the CLI, a value one graph's worker saved in
kv-store.jsondisappears when the other graph's worker saves something. That value can be the E2EE password, which is stored there when the OS keychain is unavailable (common on Linux without a secret service), so Logseq asks for it again after a restart. A crash during a write leaves the file truncated, and it then reads as empty.Cause. Each worker process read the file once, cached the whole map and wrote that map back on every
set!with a plainwriteFile. Concurrentset!calls in one process were not ordered either.Change.
getreads the file.set!merges its key into the file's current content and replaces the file by writing a temp file and renaming it. It does that while holdingkv-store.json.lock, created with O_EXCL, so that graph worker processes take turns. A writer that finds the lock held retries with a short backoff for up to 10 s. A lock older than 5 s was left by a dead process and is removed. Theset!calls of one process run one after another.Tests.
frontend.worker.platform-node-kv-store-test: two platforms on one root dir both read the file, then each saves a key, and both keys are in the file. Four kv-stores with their own write queues save 20 keys at once, and all of them are in the file (15 were lost without the lock). A write waits for a held lock and removes a stale one. The first two cases fail on master.logseq/db-test#1525: a server store is atomic, and a restart never reuses a stored node address
Reported by @JaredStemper as the db-sync server's side of this problem. The server's storage (
deps/db-sync/.../storage.cljs) wrote a store's node rows one by one outside any transaction, so an interrupted store could leave rows above the:max-addrthe root records. After a restart DataScript's address counter starts from that:max-addr, so later stores reused those addresses and overwrote rows the restored indexes could still read (null.keys, while SQLite'squick_checkstays ok).with-sql-transaction!), so an interrupted store leaves nothing behind.open-conn) raises the address counter above the largest address inkvs, which also covers graphs that already have such rows.Tests:
logseq.db-sync.storage-address-high-water-test(a row above the root's:max-addrsurvives a modelled restart and 20 later stores; a store whose second row fails leaves no first row). Both fail without the change (3 assertions) and pass with it; the db-sync suite passes (246 tests).