Skip to content

validation: CheckBlockIndex() const: Assertion !c->setBlockIndexCandidates.contains(const_cast<CBlockIndex*>(pindex)) failed #34503

Closed

Description

@dergoegge

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.

setBlockIndexCandidates_contains.log

This was found with a test running on Antithesis.

Activity

  1. dergoegge commented on Feb 4, 2026

    @dergoegge
    MemberAuthor

    @marcofleon is working on a fix

  2. dergoegge commented on Feb 4, 2026

    @dergoegge
    MemberAuthor

    Looks like this was introduced by #29640

  3. maflcko commented on Feb 4, 2026

    @maflcko
    Member

    I guess this is #33948, which I had difficulties reproducing. So thanks for making this reproducible!

  4. changed the title [-]validation: CheckBlockIndex() const: Assertion '!c->setBlockIndexCandidates.contains(const_cast<CBlockIndex*>(pindex))' failed[/-] [+]validation: CheckBlockIndex() const: Assertion `!c->setBlockIndexCandidates.contains(const_cast<CBlockIndex*>(pindex))` failed[/+] on Feb 5, 2026
  5. mzumsande commented on Feb 5, 2026

    @mzumsande
    Contributor

    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?

    In any case, this should be tagged/fixed for v31.

  6. added this to the 31.0 milestone on Feb 5, 2026
  7. marcofleon commented on Feb 5, 2026

    @marcofleon
    Contributor

    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.

  8. ismaelsadeeq commented on Mar 2, 2026

    @ismaelsadeeq
    Member

    Happened today see #34716

  9. added a commit that references this issue on Mar 6, 2026
  10. fanquake commented on Mar 8, 2026

    @fanquake
    Member

    Closing given #34521 has been merged.

  11. added a commit that references this issue on Jun 28, 2026
  12. added a commit that references this issue on Jun 30, 2026
  13. added a commit that references this issue on Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions