Skip to content

Serialize the ops-layer has_child order write under reorder_lock - #1771

Merged
mstomar125 merged 1 commit into
mainfrom
issue-1750-ops-layer-order-lock
Jul 28, 2026
Merged

mstomar125 merged 1 commit into
mainfrom
issue-1750-ops-layer-order-lock

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

Summary

The ops-layer add-existing-child path (NodeService::create_relationship, has_child auto-order) read the next child order and wrote the edge as two separate store calls (get_next_child_order then a generic insert). A concurrent reorder could interleave between them and collide the fractional order key. #1751 brought the store-internal writers (move_node, create_child_node_atomic, move_children_to_parent) under reorder_lock, but this ops-layer read-then-write was still unprotected.

Change

  • New store method SqliteStore::append_child_edge(parent, child) reads the max child order, computes the next fractional key, and inserts the has_child edge as one unit under reorder_lock — the ordered analogue of add_to_collection's atomic member_of path. get_next_child_order (its only caller) is removed.
  • create_relationship early-returns via append_child_edge for auto-ordered has_child, mirroring the existing member_of early-return. Explicit-order has_child and the unordered builtins fall through to the generic path unchanged.
  • Bulk paths (bulk_create_hierarchy, bulk_create_has_child) assign explicit precomputed orders and don't read-then-write, so they aren't exposed to this race; move_children_to_parent was already locked by Fix move_children_to_parent order collision and serialize order writes (#1746) #1751.

Re-entrancy: the lock is taken only inside append_child_edge, which calls only lock-free code (a plain SELECT + a raw insert), so the non-re-entrant tokio::sync::Mutex cannot deadlock. Four lock sites total, none nesting another.

Tests

  • concurrent_add_existing_child_keeps_distinct_order — 16 concurrent appends onto one parent plus a concurrent reorder; asserts no lost/duplicated child and all order keys distinct. Mutation-checked: removing the lock makes it fail with colliding keys.
  • add_existing_child_appends_in_order — the simple sequential case.

cargo test -p nodespace-core (980 lib + integration incl. the #1751 regression tests), cargo build --workspace, clippy -D warnings, fmt — all green.

Closes #1750.

 #1750)

The ops-layer add-existing-child path read the next child order and wrote the
has_child edge as two separate store calls, so a concurrent reorder could
interleave between them and collide the fractional order key. Add an atomic store
method append_child_edge that reads the max order, computes the next fractional
key, and inserts the edge as one unit under reorder_lock — the same discipline
#1751 brought to the store-internal writers (move_node, create_child_node_atomic,
move_children_to_parent). The bulk paths assign explicit precomputed orders and
are unaffected; get_next_child_order (its only caller) is removed. Adds a
mutation-checked concurrency test.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@mstomar125
mstomar125 merged commit 64feb30 into main Jul 28, 2026
2 checks passed
@mstomar125
mstomar125 deleted the issue-1750-ops-layer-order-lock branch July 28, 2026 10:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant