Skip to content

rpc: Assertion 'block_template' failed in getblocktemplate (during shutdown or startup) #34262

Open

Description

@dergoegge

The CHECK_NONFATAL check here may fail when getblocktemplate is called during shutdown or during startup.

rpc/mining.cpp:875 auto getblocktemplate()::(anonymous class)::operator()(const RPCHelpMan &, const JSONRPCRequest &) const: Assertion `block_template' failed.

See attached debug logs of two examples.

getblocktemplate_during_startup.log
getblocktemplate_during_shutdown.log

This was found with a test running on Antithesis.

Activity

  1. changed the title [-]rpc: Assertion `block_template' failed in `getblocktemplate` (during shutdown or startup)[/-] [+]rpc: Assertion 'block_template' failed in `getblocktemplate` (during shutdown or startup)[/+] on Jan 12, 2026
  2. DevPatel-11 commented on Jan 17, 2026

    @DevPatel-11

    I'd like to work on this. Looking at the code now.

  3. DevPatel-11 commented on Jan 19, 2026

    @DevPatel-11

    I tried reproducing this locally by spamming getblocktemplate calls during startup and shutdown, but couldn't trigger the race condition (given the timing window).

    Looking at the debug logs:

    • Shutdown case: "Shutdown in progress..." message appears right before the assertion failure
    • Startup case: Assertion fails while node is still initializing (network threads just starting)

    Both show Assertion 'block_template' failed, which means createNewBlock() returned nullptr when the mining subsystem wasn't available.

    The issue is that CHECK_NONFATAL assumes this should never happen, but nullptr is actually a valid return during these states. The fix is to replace the assertion with a proper null check and return an RPC error instead.

    I'll open a PR with the fix.

  4. chriszeng1010 commented on Aug 18, 2026

    @chriszeng1010

    Looks like this issue is closed due to LLM usage. Should we still work on this?

  5. sedited commented on Aug 18, 2026

    @sedited
    Contributor

    The pull requests in question were not just closed because they were using LLMs, but because they didn't fix the problem and the authors didn't understand the fixes they were applying. It would still be good to address this issue properly.

  6. chriszeng1010 commented on Aug 18, 2026

    @chriszeng1010

    I'll take a crack at this.

  7. chriszeng1010 commented on Aug 18, 2026

    @chriszeng1010

    I'm able to reproduce the error by setting up a test node with a funded wallet and running getblocktemplate on the 1st thread with a longpoll ID on a second terminal to display result. And then, I ran Cli stop after sending funds on the first terminal triggers a 2nd thread to run the shut down process.

    This happens on half of the attempts that I've tried. The other half exists normally with error -9 (Shut down)

    $ ./build/bin/bitcoin-cli -regtest -datadir=/tmp/gbtrepro getblocktemplate '{"rules":["segwit"],"longpollid":"4d9ad53b6b0cdda5d3e2f46ca3065053068dc7095d713034d355f237a783457c4"}'
    error code: -1
    error message:
    Internal bug detected: block_template
    ./rpc/mining.cpp:910 (getblocktemplate()::<lambda(const RPCMethod&, const JSONRPCRequest&)>)
    Bitcoin Core v31.99.0-311847fffa21
    Please report this issue here: https://github.com/bitcoin/bitcoin/issues

  8. chriszeng1010 commented on Aug 18, 2026

    @chriszeng1010

    There's a race condition during shutdown.

    Thread A sleeps here on condition after longpoll to call getblocktemplate

    std::optional<BlockRef> WaitTipChanged(ChainstateManager& chainman, KernelNotifications& kernel_notifications, const uint256& current_tip, MillisecondsDouble& timeout, bool& interrupt)
    {
        ......
            kernel_notifications.m_tip_block_cv.wait_until(lock, deadline, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
                return Assume(kernel_notifications.TipBlock()) != current_tip || chainman.m_interrupt || interrupt;
            });
        .....
    }
    

    Thread B notifies shutdown and wakes up Thread A running the following:

    
    void Interrupt(NodeContext& node)
    {
    #if HAVE_SYSTEM
        ShutdownNotify(*node.args);
    #endif
        // Wake any threads that may be waiting for the tip to change.
        if (node.notifications) WITH_LOCK(node.notifications->m_tip_block_mutex, node.notifications->m_tip_block_cv.notify_all());
        InterruptHTTPServer();
        InterruptHTTPRPC();
        InterruptRPC();
        ......
    }
    
    

    Thread A Wakes up, continues, and returns {}

    std::optional<BlockRef> WaitTipChanged(ChainstateManager& chainman, KernelNotifications& kernel_notifications, const uint256& current_tip, MillisecondsDouble& timeout, bool& interrupt)
    {
        ....
            if (chainman.m_interrupt || interrupt) {
                interrupt = false;
                return {};
            }
        }
        ....
    }
    
    

    And then it exits

    std::optional<BlockRef> maybe_tip{miner.waitTipChanged(hashWatchedChain, checktxtime)};
    // Node is shutting down
     if (!maybe_tip) break;
    
    ..........
    
    if (!IsRPCRunning())
                throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "Shutting down");
    

    Mean while on Thread B its 3 lines away from InterruptRPC()

    
    // wake up everyone here
    if (node.notifications) WITH_LOCK(node.notifications->m_tip_block_mutex, node.notifications->m_tip_block_cv.notify_all());
    InterruptHTTPServer();
    InterruptHTTPRPC();
    //interrupt here
    InterruptRPC();
    
    

    If B is faster and makes it in time, A shuts down correctly.

    Otherwise, A keeps going, hits here

    block_template = miner.createNewBlock({}, /*cooldown=*/false);
    

    And then end up in here and returns empty {}

    if (chainman.m_interrupt || interrupt) {
         interrupt = false;
         return {};
      }
    

    And failing here

    CHECK_NONFATAL(block_template);

  9. chriszeng1010 commented on Aug 18, 2026

    @chriszeng1010

    I will also check startup path.

  10. chriszeng1010 commented on Aug 20, 2026

    @chriszeng1010

    The startup failure path is the same as shutdown failure path triggered by the same condition chainman.m_interrupt || interrupt caused by the shutdown flag.

  11. added 2 commits that reference this issue on Aug 20, 2026
    4610cb2
    102deb4
  12. chriszeng1010 commented on Aug 30, 2026

    @chriszeng1010

    Can i ask the reviewers to take a look at the PR? Thanks.

  13. maflcko commented on Aug 31, 2026

    @maflcko
    Member

    @chriszeng1010 Instead of asking for review on what is basically a small style cleanup, you can help with review of the other ~400 open pull requests.

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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions