Skip to content

Serialize the ops-layer has_child order write (add-existing-child + bulk import) under reorder_lock #1750

Closed

Description

@mstomar125

Summary

#1746 brought the self-contained store order-writers under reorder_lock (move_node, create_child_node_atomic, move_children_to_parent). Two has_child order-write paths still bypass it because they span the store/ops boundary, so a concurrent write under the same parent can read a stale max order and collide/duplicate an order key with a locked create/move:

  1. Add existing node as a child — NodeService reads get_next_child_order (services/node_service/relationship.rs:651) then writes create_generic_relationship (db/sqlite_store/relationships.rs:825) as two separate store calls. Nothing holds reorder_lock across the read→write.
  2. bulk_create_has_child (db/sqlite_store/relationships.rs:571) inserts pre-computed orders — the max-order read that produced them happens upstream, unlocked.

Why it wasn't fixed in #1746

reorder_lock is a private field of SqliteStore, so the ops layer can't hold it across two store calls. Closing this needs a layered change, not a one-line lock — out of scope for #1746's contained fixes.

Fix direction

Add an atomic store method (e.g. add_existing_child / link_child_next_order) that computes the next has_child order and inserts the edge under reorder_lock, and route the ops-layer "add existing child" path and the bulk-import order computation through it.

Re-entrancy check required (per #1561/#1746): the new method must not call move_node / create_child_node_atomic / move_children_to_parent while holding the lock. create_generic_relationship does not take reorder_lock, so performing its insert under the lock is safe.

Context

Surfaced by the adversarial review of #1746. This is the remaining sibling-order write race after #1746; the fractional-order family also includes #1561.

Activity

  1. added
    bug
    on Jul 27, 2026
  2. self-assigned this
    on Jul 28, 2026
  3. mstomar125 commented on Jul 28, 2026

    @mstomar125
    ContributorAuthor

    Picking this up (mstomar125) — bringing the ops-layer has_child order write (add-existing-child and bulk import) under the same reorder_lock discipline that already covers the store-internal writers, so the read of get_next_child_order and the write can't interleave with a concurrent reorder. Confirmed #1751 covered only the store-internal methods (move_node/create_child_node_atomic/move_children_to_parent); the ops-layer add-existing-child path still reads-then-writes outside the lock.

  4. mstomar125 commented on Jul 28, 2026

    @mstomar125
    ContributorAuthor

    Resolved in #1771 (merged). The ops-layer add-existing-child order write is now atomic under reorder_lock via a new append_child_edge store method (read max order → compute next key → insert as one unit), matching the discipline #1751 brought to the store-internal writers. A mutation-checked concurrency test (16 concurrent appends racing a reorder) confirms no colliding order keys; the bulk-import paths use precomputed orders and were unaffected. Build + audit gates green.

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

Metadata

Metadata

Assignees

Labels

backendbugrust

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions