Repository navigation
Fix move_children_to_parent order collision and serialize order writes (#1746) - #1751
Merged
Merged
Conversation
move_children_to_parent assigned fresh orders 1.0, 2.0, 3.0 to the moved children without reading the new parent's existing children, so moving into a non-empty parent collided their has_child order keys even single-threaded. Read the current max order under new_parent_id and append the moved children after it. Also bring create_child_node_atomic and move_children_to_parent under reorder_lock (the mutex #1561 added to move_node) so their sibling-order read -> compute -> write can't interleave with a concurrent reorder on the shared connection. No re-entrancy: reorder_lock is acquired only in these three store methods and none calls another (rebalance does not take the lock). Adds a deterministic test the old code failed (moving into a parent that already holds children at 1.0/2.0). The ops-layer add-existing-child and bulk-import order writes still bypass the lock (they span the store/ops boundary and need an atomic store method) - tracked in #1750. Closes #1746. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Contributor
Author
|
APPROVE Independent adversarial review — no deadlock, race, correctness bug, or regression:
Scope (ops-layer |
This was referenced Jul 27, 2026
mstomar125
added a commit
that referenced
this pull request
Jul 28, 2026
#1750) (#1771) 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
Follow-up to #1561. Two fixes to sibling-order writes:
1.
move_children_to_parent— single-threaded order collision (correctness)It assigned the moved children fresh orders
1.0, 2.0, 3.0…without reading the new parent's existing children, so moving into a non-empty parent collided theirhas_childorder keys even without concurrency. It now reads the current max order undernew_parent_idand appends the moved children after it (empty parent → unchanged behavior).2. Bring
create_child_node_atomic+move_children_to_parentunderreorder_lock#1561 added
reorder_lock(a non-reentranttokio::Mutex) tomove_nodeonly. These two other self-contained store paths do a sibling-order read → compute → write on the shared connection and could interleave with a concurrent reorder. They now hold the lock across that sequence.No re-entrancy / deadlock:
reorder_lockis acquired in exactly three store methods (move_node,create_child_node_atomic,move_children_to_parent) and none calls another (rebalance_children_for_parentdeliberately doesn't take it); no caller holds the lock (it's private toSqliteStore).Scope
Deferred (needs a layered atomic store method, not a one-line lock) and filed as #1750: the ops-layer "add existing node as child" (
get_next_child_orderread →create_generic_relationshipwrite, two separate store calls) andbulk_create_has_child(inserts pre-computed orders) still writehas_childorders without the lock.Tests
move_children_into_non_empty_parent_appends_without_collision— seeds the new parent with two children at 1.0/2.0 viacreate_child_node_atomic, moves two more in, asserts all four order keys are distinct and the moved children hold the two largest (appended after). The old code produced1.0, 1.0, 2.0, 2.0.test_move_children_to_parent_store_*,concurrent_reorders_keep_all_children_with_distinct_order, and the ops-layer move tests still pass.968 core lib tests pass; clippy clean; full pre-push gate green (one transient daemon-readiness e2e flake — a startup-timing timeout unrelated to this change — cleared on a longer
E2E_DAEMON_TIMEOUT; passes cleanly standalone). Independent adversarial review: no deadlock / race / correctness bug / regression.Closes #1746.