Skip to content

B+Tree: remove-then-reinsert of a leaf's first key stalls insert for 10,000 retries then throws — single-threaded, shipped in PR #737 #740

Closed

Description

@nockawa

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

Activity

  1. self-assigned this
    on Aug 9, 2026
  2. updated Area Indexes, Product Engine, Estimate XS, Priority P0-Critical and Bug status Fixing on Aug 9, 2026
  3. added theissue type on Aug 9, 2026
  4. moved this to In Progress in Typhonon Aug 9, 2026
  5. added 2 commits that reference this issue on Aug 10, 2026
  6. nockawa commented on Aug 10, 2026

    @nockawa
    CollaboratorAuthor

    Fixed on main in 6dae31e (PR #745).

    Guard now compares against the previous leaf's HighKey instead of the leaf's own first key, so the band a removal legitimately creates — separator <= key < firstKey — is no longer rejected. BtreeTests.RemoveThenReinsertLeafFirstKey_DoesNotStallInsert covers 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 unmodified main. 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-03 in claude/rules/indexing.md (typhon-claude#73), so the next guard added here has the rule to check itself against.

    Bug status → Verified.

  7. moved this from In Progress to Done in Typhonon Aug 10, 2026
  8. added 2 commits that reference this issue on Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Fields

Priority

P0-Critical

Estimate

XS

Area

Indexes

Start date

None yet

Target date

None yet

Bug status

Verified

Product

Engine

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions