You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This assertion in CheckBlockIndex may fail under certain block race and shutdown/restart conditions (see attached example log).
The root cause appears to be the modification of the nSequenceId field of CBlockIndex pointers, after they have been added to setBlockIndexCandidates. This is undefined behavior as the CBlockIndexWorkComparator compares nSequenceIds.
Specifically, here the tip's (and ancestor) sequence ids are changed while all the indices are in the candidates set already.
Could someone explain a bit more? The linked code attempts to deal with this very problem by removing/readding the tip to setBlockIndexCandidates, while ancestors of the tip shouldn't be in setBlockIndexCandidates. Where is the error in this?
The issue is that erase sometimes doesn't succeed in removing the tip. We populate setBlockIndexCandidates in LoadBlockIndex with sequence id 1 and then later change it to 0 in LoadChainTip, which ends up corrupting the tree structure of the set. The tip was initially stored based on the sequence id being 1, so calling erase after the change to 0 might just return null.
After we re-add the tip, it's now in setBlockIndexCandidates twice, but because there was a block race, that set ends up looking something like this:
2026-02-05T15:42:13Z DEBUG: Before erase/re-insert, candidates size=535, tip ptr=0x600000d17d20
2026-02-05T15:42:13Z DEBUG: erase(tip) removed 0 elements
2026-02-05T15:42:13Z DEBUG: After prune, candidates size=4 (should be 1)
2026-02-05T15:42:13Z DEBUG: h=505 seqId=0 ptr=0x600000d17d20 hash=583a9332474464f8
2026-02-05T15:42:13Z DEBUG: h=505 seqId=1 ptr=0x600000d149f0 hash=0f002c5df6f09f9b
2026-02-05T15:42:13Z DEBUG: h=505 seqId=1 ptr=0x600000d07df0 hash=5ed3fc31e5ae9f6d
2026-02-05T15:42:13Z DEBUG: h=505 seqId=0 ptr=0x600000d17d20 hash=583a9332474464f8
because the prune stops at the first instance of the tip. So now in CheckBlockIndex the assertion that a block that sorts worse than the tip is not in the set fails.
The simple fix is just to move the erase to before changing the sequence ids, but it's still undefined behavior to modify the sequence id while blocks are in setBlockIndexCandidates. I believe the proper fix is to move the population of that set from LoadBlockIndex to LoadChainTip after setting the sequence ids. This would also be better because the current behavior has the candidate set at some point holding the entire block tree, which isn't really what that set is for.
The refactor is unfortunately proving to be more difficult than expected because of the other snapshot chainstate that we maintain. I'm happy to just open the quick fix for v31.
Activity
dergoegge commentedon Feb 4, 2026
@marcofleon is working on a fix
dergoegge commentedon Feb 4, 2026
Looks like this was introduced by #29640
maflcko commentedon Feb 4, 2026
I guess this is #33948, which I had difficulties reproducing. So thanks for making this reproducible!
[-]validation: CheckBlockIndex() const: Assertion '!c->setBlockIndexCandidates.contains(const_cast<CBlockIndex*>(pindex))' failed[/-][+]validation: CheckBlockIndex() const: Assertion `!c->setBlockIndexCandidates.contains(const_cast<CBlockIndex*>(pindex))` failed[/+]mzumsande commentedon Feb 5, 2026
Could someone explain a bit more? The linked code attempts to deal with this very problem by removing/readding the tip to
setBlockIndexCandidates, while ancestors of the tip shouldn't be insetBlockIndexCandidates. Where is the error in this?In any case, this should be tagged/fixed for v31.
marcofleon commentedon Feb 5, 2026
The issue is that
erasesometimes doesn't succeed in removing the tip. We populatesetBlockIndexCandidatesinLoadBlockIndexwith sequence id 1 and then later change it to 0 inLoadChainTip, which ends up corrupting the tree structure of the set. The tip was initially stored based on the sequence id being 1, so callingeraseafter the change to 0 might just return null.After we re-add the tip, it's now in
setBlockIndexCandidatestwice, but because there was a block race, that set ends up looking something like this:because the prune stops at the first instance of the tip. So now in
CheckBlockIndexthe assertion that a block that sorts worse than the tip is not in the set fails.The simple fix is just to move the erase to before changing the sequence ids, but it's still undefined behavior to modify the sequence id while blocks are in
setBlockIndexCandidates. I believe the proper fix is to move the population of that set fromLoadBlockIndextoLoadChainTipafter setting the sequence ids. This would also be better because the current behavior has the candidate set at some point holding the entire block tree, which isn't really what that set is for.The refactor is unfortunately proving to be more difficult than expected because of the other snapshot chainstate that we maintain. I'm happy to just open the quick fix for v31.
l0rinc commentedon Feb 25, 2026
Just happened in https://github.com/bitcoin/bitcoin/actions/runs/22412561434/job/64889761007?pr=34641
ismaelsadeeq commentedon Mar 2, 2026
Happened today see #34716
Merge #34521: validation: fix UB in `LoadChainTip`
fanquake commentedon Mar 8, 2026
Closing given #34521 has been merged.
Merge bitcoin/bitcoin#34521: validation: fix UB in `LoadChainTip`
Merge bitcoin/bitcoin#34521: validation: fix UB in `LoadChainTip`
Merge bitcoin/bitcoin#34521: validation: fix UB in `LoadChainTip`