Repository navigation
Serialize the ops-layer has_child order write under reorder_lock - #1771
Merged
Merged
Conversation
#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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The ops-layer add-existing-child path (
NodeService::create_relationship,has_childauto-order) read the next child order and wrote the edge as two separate store calls (get_next_child_orderthen 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) underreorder_lock, but this ops-layer read-then-write was still unprotected.Change
SqliteStore::append_child_edge(parent, child)reads the max child order, computes the next fractional key, and inserts thehas_childedge as one unit underreorder_lock— the ordered analogue ofadd_to_collection's atomicmember_ofpath.get_next_child_order(its only caller) is removed.create_relationshipearly-returns viaappend_child_edgefor auto-orderedhas_child, mirroring the existingmember_ofearly-return. Explicit-orderhas_childand the unordered builtins fall through to the generic path unchanged.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_parentwas 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 plainSELECT+ a raw insert), so the non-re-entranttokio::sync::Mutexcannot 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.