Skip to content

Other sibling-order write paths bypass reorder_lock (can still race); move_children_to_parent order collision #1746

Closed

Description

@mstomar125

Summary

The concurrent-reorder race fix (#1561) added SqliteStore::reorder_lock, which serializes move_node against move_node. Adversarial review surfaced that other sibling-order read-then-write paths do not take that lock, so a concurrent move_node + one of them (or one of them against itself) can still collide/corrupt has_child order under the same parent. These are pre-existing and outside #1561's stated two-reorders scenario, tracked here.

Paths that bypass reorder_lock

  • create_child_node_atomic append — reads max order (sqlite_store/nodes.rs:79-82), computes, inserts in a separate transaction.
  • has_child append via get_next_order_for_relationship (sqlite_store/relationships.rs:~211, 252-256).
  • bulk_create_has_child (sqlite_store/relationships.rs:~571).
  • move_children_to_parent (sqlite_store/nodes.rs:~1344) — also a single-threaded bug: assigns fresh 1.0, 2.0, 3.0 without reading existing children under the new parent, so it can collide with siblings already there even without concurrency.

Options

  • Extend reorder_lock to guard every sibling-order read→compute→write (uniform serialization of all order mutations). Must check each path for re-entrancy (nothing that holds the lock may call another lock-taking path).
  • Separately fix move_children_to_parent to read existing children and append after them (correctness bug, not just concurrency).

Context

Surfaced during review of #1561. Not a regression from that PR — the lock simply doesn't (and didn't claim to) cover these paths.

Activity

  1. added
    bug
    on Jul 25, 2026
  2. added a commit that references this issue on Jul 27, 2026
    647a46c
  3. mstomar125 commented on Jul 27, 2026

    @mstomar125
    ContributorAuthor

    Resolved in #1751 (merged to main as 647a46c7).

    What shipped

    1. move_children_to_parent order collision (correctness). It assigned the moved children fresh 1.0, 2.0, 3.0… without reading the new parent's existing children, so moving into a non-empty parent collided their has_child order keys even single-threaded. It now reads the current max order under new_parent_id and appends the moved children after it (empty parent → unchanged).
    2. create_child_node_atomic + move_children_to_parent under reorder_lock. Both now hold Concurrent sibling reorders can race on the fractional-order read-then-write window #1561's mutex across their sibling-order read → compute → write, so they can't interleave with a concurrent reorder on the shared connection. Re-entrancy verified clean (only these two plus move_node acquire the lock; none calls another; no caller holds it).

    Verification

    New deterministic test move_children_into_non_empty_parent_appends_without_collision (the old code produced 1.0, 1.0, 2.0, 2.0); existing move + concurrent_reorders_keep_all_children_with_distinct_order tests still pass. 968 core lib tests green, clippy clean, full pre-push gate green. Independent adversarial review: no deadlock / race / correctness bug / regression.

    Follow-up (filed #1750)

    The ops-layer "add existing node as child" (get_next_child_order read → create_generic_relationship write, two separate store calls) and bulk_create_has_child (inserts pre-computed orders) still write has_child orders without the lock. reorder_lock is private to SqliteStore, so serializing those cross-boundary read→writes needs an atomic store method — tracked in #1750.

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