Repository navigation
Description
Overview
Two concurrent reorder_node operations on distinct sibling nodes under the same parent can race and produce a corrupted final sibling order, because the fractional order key for each is computed from a sibling read that isn't isolated from the other operation's write.
Problem Statement
move_node's same-parent reorder branch reads the current sibling order values, computes a new fractional order key in Rust, then writes it back — as two separate, non-transactional database round-trips. There is no lock or transaction spanning the read, the computation, and the write.
When two reorders run concurrently against the same parent (for example, one moving a node to the beginning of the list and another moving a different node to the end), both can read the same sibling snapshot before either writes back. Each then computes its new order key against that stale snapshot, so the two writes can produce overlapping or otherwise inconsistent order keys — the final sibling order does not reflect both operations correctly.
Optimistic concurrency control does not catch this: the version field on move_node/reorder_node guards only the moved node's own row, not the read of its siblings' order values used to compute the new key.
This was found via a new Rust integration test that drives two concurrent reorder_node calls through the real Tauri command layer against a real headless daemon and real SQLite (packages/desktop-app/src-tauri/tests/indent_outdent_rapid_ordering_test.rs, concurrent_reorders_of_distinct_nodes_all_succeed). It reproduces intermittently — roughly 1 in 15 full-suite runs — consistent with a genuine race rather than a deterministic bug.
A related but distinct case is already documented as unresolved: ../nodespace-docs/archived/architecture/data/optimistic-concurrency-control.md describes a "Sibling Reorder Race Condition Fix" (a retry loop calling reorder_child_with_version_check) for a content-update-vs-reorder race. That function does not exist anywhere in the current codebase, so the documented mitigation was never implemented — and even if it were, it does not cover this concurrent-reorder-vs-reorder case.
Proposed Solution
Wrap the sibling read, fractional-order computation, and write-back in a single transaction for the same-parent reorder branch of move_node, so concurrent reorders under the same parent serialize correctly. The cross-parent move branch already wraps its delete+insert in a transaction for atomicity; the same protection was never extended to the same-parent branch's read-then-write window.
Acceptance Criteria
- Concurrent
reorder_node/move_nodecalls on distinct siblings under the same parent cannot produce an inconsistent final sibling order - The sibling read, order computation, and write-back for a same-parent reorder are protected by a single transaction (or equivalent serialization) per parent
-
concurrent_reorders_of_distinct_nodes_all_succeedinpackages/desktop-app/src-tauri/tests/indent_outdent_rapid_ordering_test.rspasses reliably under repeated full-suite runs, not just in isolation - Code passes
bun run quality:fix; tests pass (bun run test:all)
Technical Specifications
Reference Files
- sqlite_store.rs:
packages/core/src/db/sqlite_store.rs—move_node's same-parent branch: the siblingSELECTcomputing current min/max order, followed by the fractional order computation, followed by theUPDATEwriting the new order — currently three separate steps with no transaction spanning them. The cross-parent branch nearby already uses a transaction for its delete+insert; the same technique applies here. - node_service/mod.rs:
packages/core/src/services/node_service/mod.rs—resolve_insert_positionresolves anEndposition via its own separateget_childrenread, widening the same race window. - Test reproducing this:
packages/desktop-app/src-tauri/tests/indent_outdent_rapid_ordering_test.rs—concurrent_reorders_of_distinct_nodes_all_succeed.
Implementation Details
- Serialize the sibling-read + order-computation + write-back as a single transaction per parent, mirroring the existing transactional treatment of the cross-parent delete+insert case.
- The previously documented retry-loop approach (
reorder_child_with_version_check, described in../nodespace-docs/archived/architecture/data/optimistic-concurrency-control.md) is one option, but that function was never implemented in the current codebase and would need to be designed against this specific race, not just the content-update-vs-reorder case it was originally written for.
Related Issues
- Test the real Tauri-to-daemon seam end-to-end #1499 (where this was discovered, while adding Tauri-seam integration tests — not a blocking dependency)
Activity
Test the real Tauri-to-daemon seam end-to-end (ADR-048) (#1563)
mstomar125 commentedon Jul 25, 2026
✅ Completed — merged in PR #1747
Concurrent sibling reorders under the same parent no longer race on the fractional-order read-then-write.
Root cause & fix:
move_noderead sibling order values, computed a new fractional key in Rust, and wrote it back as separate awaited steps on a single sharedArc<libsql::Connection>— two concurrent moves interleaved at the awaits and computed keys against the same stale snapshot. The issue proposed a DB transaction, but aBEGIN IMMEDIATEcan't serialize concurrent tasks on one shared connection (the second hits "cannot start a transaction within a transaction"). The fix is an application-leveltokio::sync::Mutex(SqliteStore::reorder_lock) held across the whole read → compute → write-back (covering the same-parent, cross-parent, and make-root branches plus any rebalance). Reorders are user-driven and infrequent, so serializing them is free in practice.Verification:
concurrent_reorders_of_distinct_nodes_all_succeed: 30/30 consecutive passes against anodespacedbuilt with the fix (and the pre-push gate ran it against a fixed-daemon sidecar — green).concurrent_reorders_keep_all_children_with_distinct_order(16 simultaneous reorders): 30/30 standalone + 3/3 full-suite.cargo test -p nodespace-core --lib967 passed; clippy clean.Honest note: the 1-in-15 flake is load-dependent and doesn't reproduce deterministically in isolation (the single connection tends to complete each task's statements together), so the guarantee rests on the fix's construction (the mutex makes the interleave impossible) plus the above, not a failing-then-passing unit reproduction.
Follow-up #1746: 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 bypassreorder_lockand can race — pre-existing, outside this issue's scenario, tracked separately.Fix move_children_to_parent order collision and serialize order writes (