Skip to content

Fix move_children_to_parent order collision and serialize order writes (#1746) - #1751

Merged
mstomar125 merged 1 commit into
mainfrom
worktree-issue-1746-order-write-races
Jul 27, 2026
Merged

mstomar125 merged 1 commit into
mainfrom
worktree-issue-1746-order-write-races

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

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 their has_child order keys even without concurrency. It now reads the current max order under new_parent_id and appends the moved children after it (empty parent → unchanged behavior).

2. Bring create_child_node_atomic + move_children_to_parent under reorder_lock

#1561 added reorder_lock (a non-reentrant tokio::Mutex) to move_node only. 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_lock is acquired in exactly three store methods (move_node, create_child_node_atomic, move_children_to_parent) and none calls another (rebalance_children_for_parent deliberately doesn't take it); no caller holds the lock (it's private to SqliteStore).

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_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.

Tests

  • New: move_children_into_non_empty_parent_appends_without_collision — seeds the new parent with two children at 1.0/2.0 via create_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 produced 1.0, 1.0, 2.0, 2.0.
  • Existing 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.

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]>
@mstomar125

Copy link
Copy Markdown
Contributor Author

APPROVE

Independent adversarial review — no deadlock, race, correctness bug, or regression:

  • Re-entrancy clear: reorder_lock is acquired in exactly three store methods (move_node, create_child_node_atomic, move_children_to_parent); none calls another, rebalance_children_for_parent deliberately doesn't take it, and no caller holds it (private field). notify is synchronous so it can't re-enter an async mutex. Lock-across-await-tx matches the pattern Concurrent sibling reorders can race on the fractional-order read-then-write window #1561 established.
  • Correctness: existing_max read (DESC LIMIT 1, NULL→0.0, None on empty) + prev = orders.last().or(existing_max) correctly appends after existing; the "child already under new_parent" edge case is version-gated (old edge deleted, re-inserted at a strictly larger key).
  • No regression: empty parent → identical sequence; all move tests + the concurrency test pass.

Scope (ops-layer get_next_child_order→write and bulk_create_has_child) deferred to #1750 — they need a layered atomic store method, not a one-line lock.

@mstomar125
mstomar125 merged commit 647a46c into main Jul 27, 2026
2 checks passed
@mstomar125
mstomar125 deleted the worktree-issue-1746-order-write-races branch July 27, 2026 08:48
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]>
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.

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

1 participant