mining: avoid checkBlock crash when chain has no tip #36908

pull danielabrozzoni wants to merge 1 commits into bitcoin:master from danielabrozzoni:ipc_checkblock_crash_w_turtle changing 3 files +13 −5
  1. danielabrozzoni commented at 1:10 AM on November 18, 2026: member

    The problem: Init.makeMining returns as soon as the chainstate is loaded. On a first start with an empty datadir, or on a node started with -reindex/-reindex-chainstate, this is before the genesis block is connected, so the active chain has no tip. A Mining.checkBlock call in this window reaches TestBlockValidity(), which asserts that the tip exists, and the node aborts.

    The fix: in MakeMining, if wait_loaded is set, wait for the chainstate to be present and for the tip to be non-null.

    Alternative fix: In Mining.checkBlock, check if the tip is present, return early with "inconclusive-no-chain-tip" otherwise. (I initially implemented this fix, but decided to go with the makeMining one instead, see #36908 (review))

    The crash can be reproduced on master using the functional test included in this PR.

    This is joint work with Turtlecute33 :)

  2. DrahtBot added the label Mining on Nov 18, 2026
  3. DrahtBot commented at 1:10 AM on November 18, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process.

    Type Reviewers
    ACK w0xlt, xyzconstant

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  4. mining: avoid checkBlock crash when chain has no tip
    Currently `Init.makeMining` returns as soon as the chainstate is loaded.
    On a first start with an empty datadir, or on a node started with
    `-reindex`/`-reindex-chainstate`, this is before the genesis block is
    connected, so the active chain has no tip. A `Mining.checkBlock` call in
    this window reaches `TestBlockValidity()`, which asserts that the tip
    exists, and the node aborts.
    
    Fix it by also waiting for the tip to be present in `MakeMining()` when
    `wait_loaded` is set.
    
    Co-authored-by: Daniela Brozzoni <danielabrozzoni@protonmail.com>
    8cba4bace8
  5. in src/node/interfaces.cpp:1006 in e4603d10d1
    1000 | @@ -1001,6 +1001,13 @@ class MinerImpl : public Mining
    1001 |      bool checkBlock(const CBlock& block, const node::BlockCheckOptions& options, std::string& reason, std::string& debug) override
    1002 |      {
    1003 |          LOCK(chainman().GetMutex());
    1004 | +        // TestBlockValidity() requires a tip, which is not yet connected on
    1005 | +        // early startup.
    1006 | +        if (!chainman().ActiveChain().Tip()) {
    


    maflcko commented at 7:54 AM on November 18, 2026:

    Seems fine, but I think it would be better to not have to think about the tip being nullptr. This just adds mostly dead code that is practically only reachable in rare races or test envs.

    Just waiting a millisecond more to load the genesis block seems conceptually easier?


    danielabrozzoni commented at 9:17 PM on November 18, 2026:

    Right, thank you! I was very undecided between the two solutions, but I don't really see a reason for using Mining without a tip, so I agree that it's better to remove the check here.

  6. danielabrozzoni force-pushed on Nov 18, 2026
  7. danielabrozzoni commented at 9:56 PM on November 18, 2026: member

    In my last push (8cba4ba):

    • Addressed #36908 (review)
    • Removed the unit test (it was covering the check that is now gone), added a functional test instead. Since the fix sits in wait_loaded, it made sense to test it next to the existing createNewBlock early-startup test.
  8. w0xlt commented at 10:13 PM on November 18, 2026: contributor

    ACK 8cba4bace8df92afcc9ff46404e36c741d557b64

    makeMining() now waits for a tip the same way createNewBlock() and waitTipChanged() already do.

  9. xyzconstant commented at 3:29 AM on November 19, 2026: contributor

    tACK 8cba4bace8


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-19 19:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me