Skip to content

Concurrent sibling reorders can race on the fractional-order read-then-write window #1561

Closed

Description

@malibio

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_node calls 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_succeed in packages/desktop-app/src-tauri/tests/indent_outdent_rapid_ordering_test.rs passes 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 sibling SELECT computing current min/max order, followed by the fractional order computation, followed by the UPDATE writing 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_position resolves an End position via its own separate get_children read, 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

Activity

  1. added
    bug
    on Jul 6, 2026
  2. mstomar125 commented on Jul 25, 2026

    @mstomar125
    Contributor

    ✅ 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_node read sibling order values, computed a new fractional key in Rust, and wrote it back as separate awaited steps on a single shared Arc<libsql::Connection> — two concurrent moves interleaved at the awaits and computed keys against the same stale snapshot. The issue proposed a DB transaction, but a BEGIN IMMEDIATE can't serialize concurrent tasks on one shared connection (the second hits "cannot start a transaction within a transaction"). The fix is an application-level tokio::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:

    • Existing repro test concurrent_reorders_of_distinct_nodes_all_succeed: 30/30 consecutive passes against a nodespaced built with the fix (and the pre-push gate ran it against a fixed-daemon sidecar — green).
    • New multi-threaded concurrent_reorders_keep_all_children_with_distinct_order (16 simultaneous reorders): 30/30 standalone + 3/3 full-suite.
    • cargo test -p nodespace-core --lib 967 passed; clippy clean.
    • Independent adversarial review: CONFIRMED-GOOD (full lock coverage, no deadlock, correct serialization, sound reasoning, non-flaky test).

    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_atomic append, get_next_order_for_relationship, bulk_create_has_child, move_children_to_parent) still bypass reorder_lock and can race — pre-existing, outside this issue's scenario, tracked separately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    backendbugrust

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions