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.