Repository navigation
Description
Regression introduced by PR #737, live on main since 3958c154
No concurrency involved. A plain single-threaded Remove(k) followed by Add(k), where k is the first key of any non-leftmost leaf, spins the pessimistic insert loop through all 10,000 restarts (~2.5 minutes) and then throws:
System.InvalidOperationException: B+Tree insert made no progress in 10000 pessimistic retries.
The descent keeps reaching a leaf it can neither validate nor modify, which no further retrying
resolves. This is a liveness defect in the tree, not contention (see #695).
at BTree`2.AddOrUpdateCorePessimistic(InsertArguments&) BTree.Insert.cs:604
at BTree`2.AddOrUpdateCore(InsertArguments&) BTree.Insert.cs:148
at BTree`2.Add(TKey, Int32, ChunkAccessor`1&) BTree.cs:1242
Root cause: the guard is strictly stronger than the invariant it protects
KeyBelowLeafLowerBound (added in PR #737 for #297/#679 mode 1) restarts the descent when the key sits below the target leaf's first key and the leaf has a previous sibling. Its stated premise is "a leaf's first key IS the separator its parent holds."
That premise holds only immediately after a split. Removing a leaf's first key raises the leaf's minimum and leaves the separator where it was, so the separator now sits legitimately below the leaf's first key — which is exactly the one-sided slack ValidateLeafSeparators was deliberately written to tolerate in the same PR. The guard and the consistency check therefore contradict each other.
Mode 1 is a defect only when key < separator: the inserted key lands below the separator routing to it and becomes unreachable. Testing key < firstKey also rejects the whole band separator <= key < firstKey, where the leaf is the correct destination. Single-threaded, nothing mutates between attempts, so every re-descent reaches the same leaf and fails the same test until the bound throws.
Why the suite did not catch it
5,072 tests passed. Nothing in the suite removed a leaf's first key and re-inserted it on a tree large enough to have interior leaves. It was caught by BTreeMicroBenchmarks.Insert_Random — a benchmark, not a test — which does exactly Remove(key) then Add(key) over a 10,000-key tree and reported NA because the benchmark process exited on the exception.
This is the concrete cost of the "perf is unmeasured" item left open on #297: the benchmark that was never run was also the only thing exercising this shape.
Fix
Test the key against the leaf's true lower bound — the previous leaf's HighKey, the exclusive bound the B-link descent already steers by — instead of against its first key:
if (leaf.GetCount(ref accessor) == 0 || comparer.Compare(key, leaf.GetFirst(ref accessor).Key) >= 0)
{
return false;
}
var previous = leaf.GetPrevious(ref accessor);
return previous.IsValid && comparer.Compare(key, previous.GetHighKey(ref accessor)) < 0;Same cost on the common path (one GetCount, one GetFirst, one compare); the two extra chunk reads are paid only once the first-key comparison has already failed.
Status
Fixed on branch olc-738-739 with a deterministic regression test, BtreeTests.RemoveThenReinsertLeafFirstKey_DoesNotStallInsert — 2 m 34 s and failing before, 130 ms and green after. All 70 B+Tree fixture tests pass. Three unrelated suite failures (PayloadAxes_SurviveACrash, DirtyCounter_IsConservedAtQuiesce, ParallelOperations_AreLinearizable) were baselined on unmodified main and fail identically there.
Related
- OLC: concurrent inserts can lose keys under contention without throwing #297 / OLC B+Tree: concurrent disjoint-key inserts can lose a key and leave the tree inconsistent #679 — the defects PR #297 #679 Six OLC B+Tree defects: silent key loss on insert, remove and read #737 was fixing when it introduced this one
- OLC B+Tree: concurrent splits drive AddOrUpdateCorePessimistic to its 10,000-restart bound #738, OLC B+Tree: merge/unlink writes a garbage chunk id, and a key can survive its Remove #739 — the other open OLC B+Tree defects
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Estimate
Area
Start date
Target date
Bug status
Product
Projects
- StatusShow more project fieldsDone
Activity
#740 Compare the insert lower-bound guard against the previous leaf's…
#738 Stop the race harness calling slowness a hang, and leaking a loc…
nockawa commentedon Aug 10, 2026
Fixed on
mainin 6dae31e (PR #745).Guard now compares against the previous leaf's
HighKeyinstead of the leaf's own first key, so the band a removal legitimately creates —separator <= key < firstKey— is no longer rejected.BtreeTests.RemoveThenReinsertLeafFirstKey_DoesNotStallInsertcovers it: 2 m 34 s and failing before, 130 ms and green after. Full suite on the branch alone was 5,073 passed with only the three failures that fail identically on unmodifiedmain. No perf regression — the lookup controls, which execute none of the added code, moved further between builds than any insert-path delta.Invariant recorded as
IXW-03inclaude/rules/indexing.md(typhon-claude#73), so the next guard added here has the rule to check itself against.Bug status → Verified.
#747 Carry IXW-03 across, and bump the count it changes in the same c…
#740 Fix the lower-bound guard's surviving twin in RemoveIterative, a…