Repository navigation
Serialize concurrent sibling reorders to fix fractional-order race (closes #1561) - #1747
Conversation
…loses #1561) move_node reads sibling order values, computes a new fractional-order key, and writes it back as separate awaited steps on the shared DB connection. Two concurrent moves interleave at those awaits, computing keys against the same stale snapshot, so the final sibling order reflects only one move (or a move errors). Version OCC doesn't catch it — it guards the moved node's row, not the sibling-order read. Adds SqliteStore::reorder_lock (tokio Mutex) held across the read→compute→ write-back. A BEGIN IMMEDIATE can't serialize here: all tasks share one libsql connection, so the second would hit 'cannot start a transaction within a transaction' rather than waiting. Reorders are user-driven and infrequent, so serializing them costs nothing noticeable. Adds a multi-threaded concurrency test exercising 16 simultaneous reorders. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
APPROVE Independent adversarial review returned CONFIRMED-GOOD across all 6 primary checks: the |
Summary
Fixes a data-corruption race: two concurrent
reorder_nodeoperations on distinct siblings under the same parent could produce a corrupted final sibling order.move_node's order logic reads the current sibling order values, computes a new fractional-order key in Rust, and writes it back as separate, non-transactionalawaited steps on the shared DB connection. Two concurrent moves interleave at thoseawaitpoints, both reading the same stale snapshot and computing overlapping/inconsistent order keys — so the final order reflects only one of the two moves. Version-based OCC doesn't catch it: it guards the moved node's own row, not the read of its siblings' order values.Fix
Serialize the read → compute → write-back with an application-level async mutex (
SqliteStore::reorder_lock), held across the wholemove_nodeorder section.Why not a DB transaction (as the issue proposed): the store uses a single shared
Arc<libsql::Connection>for all tasks. ABEGIN IMMEDIATEon that connection can't serialize concurrent moves — the second task would hit "cannot start a transaction within a transaction" (nested transaction on the same connection), not wait. An app-leveltokio::sync::Mutexserializes them deterministically regardless of the connection model, and also protects the cross-parent branch's read-compute-write (itsnew_orderis likewise computed from an out-of-transaction read). Reorders are user-driven and infrequent, so the added contention is negligible.Changes
packages/core/src/db/sqlite_store/mod.rs— newreorder_lock: Arc<tokio::sync::Mutex<()>>field + doc comment; initialized innew().packages/core/src/db/sqlite_store/nodes.rs—move_nodeacquiresreorder_lockimmediately before the sibling-order read and holds it (through any rebalance) until the write-back completes.rebalance_children_for_parentdoes not take the lock, so there is no re-entrant deadlock.Verification
concurrent_reorders_of_distinct_nodes_all_succeed(previously flaky ~1-in-15 under full-suite load) — 30/30 consecutive passes against anodespacedbuilt with this fix.concurrent_reorders_keep_all_children_with_distinct_order(multi-threaded, 16 simultaneous reorders): all moves succeed, all children retained, distinct order keys — 30/30 standalone + 3/3 full-suite passes.cargo test -p nodespace-core --lib: 967 passed; clippy clean.reorder_lockis the store's only mutex (no lock-ordering hazard); and the test's distinctness check can't false-fail (serialized beginning-moves stay ~1.0 apart).Scope (follow-up #1746):
reorder_lockserializesmove_nodeagainstmove_node— the exact #1561 scenario. Review noted other sibling-order write paths (create_child_node_atomicappend,get_next_order_for_relationship,bulk_create_has_child,move_children_to_parent) still bypass the lock and can race; those are pre-existing and tracked in #1746.Honest note on reproduction: the race is timing/load-dependent and does not reproduce deterministically in isolation (single shared libsql connection tends to complete each task's statements together; the 1-in-15 flake needs full-suite pressure). Verification therefore rests on the fix's construction (the mutex makes the interleave impossible), the existing repro test passing against the fixed daemon, and the concurrency smoke test — not on a failing-then-passing unit reproduction.
Closes #1561.