rpc: Handle nullptr from createNewBlock in getblocktemplate #34357

pull DevPatel-11 wants to merge 2 commits into bitcoin:master from DevPatel-11:fix/34262-getblocktemplate-nullcheck changing 1 files +3 −1
  1. DevPatel-11 commented at 2:26 am on January 21, 2026: none

    Fixes #34262

    getblocktemplate currently asserts when miner.createNewBlock() returns nullptr. This can happen during shutdown or when the mining subsystem is unavailable.

    Replace the assertion with proper error handling that returns RPC_CLIENT_NOT_CONNECTED instead, indicating the service is temporarily unavailable.

  2. DrahtBot added the label RPC/REST/ZMQ on Jan 21, 2026
  3. DrahtBot commented at 2:26 am on January 21, 2026: contributor

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

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/34357.

    Reviews

    See the guideline for information on the review process. A summary of reviews will appear here.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #32420 (miner: drop dummy extraNonce in coinbase scriptSig for templates requested via IPC by Sjors)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  4. maflcko commented at 7:33 am on January 21, 2026: member
    Not sure this is the right fix. Isn’t the rpc supposed to be spun up only when all systems are live, and supposed to be spun down before the first system is taken offline? So the correct fix would be to ensure that, no?
  5. DevPatel-11 marked this as a draft on Jan 22, 2026
  6. DrahtBot added the label CI failed on Jan 22, 2026
  7. DrahtBot commented at 8:20 pm on January 22, 2026: contributor

    🚧 At least one of the CI tasks failed. Task lint: https://github.com/bitcoin/bitcoin/actions/runs/21194965976/job/61189286769 LLM reason (✨ experimental): Lint failure: tabs used as whitespace detected by the tabs_whitespace check, causing exit code 1.

    Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example:

    • Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch.

    • A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test.

    • An intermittent issue.

    Leave a comment here, if you need help tracking down a confusing failure.

  8. DevPatel-11 commented at 4:58 am on January 23, 2026: none

    @maflcko Thanks for the feedback!

    I see two possible approaches here:

    Approach 1 (my current fix): Add a defensive null-check in the RPC layer. When createNewBlock() returns nullptr, we return a clear error message to the caller instead of crashing. This handles the symptom gracefully.

    Approach 2 (root cause fix): Ensure the RPC server only accepts requests after all subsystems (including mining) are fully initialized, and stops accepting requests before any subsystem shuts down. This prevents the nullptr scenario from happening in the first place.

    Should I focus on fixing the startup/shutdown initialization order instead, or should we combine both approaches?

    The defensive check still seems useful as a safety net (defense in depth), but I understand your concern that we should address why the RPC is accepting requests when the mining subsystem isn’t ready.

  9. rpc: Handle nullptr from createNewBlock in getblocktemplate d50675e6c1
  10. DevPatel-11 force-pushed on Jan 23, 2026
  11. Merge branch 'bitcoin:master' into fix/34262-getblocktemplate-nullcheck a88c1194c6
  12. DevPatel-11 marked this as ready for review on Jan 23, 2026
  13. maflcko commented at 7:35 am on January 23, 2026: member

    Approach 1 (my current fix): Add a defensive null-check in the RPC layer. When createNewBlock() returns nullptr, we return a clear error message to the caller instead of crashing. This handles the symptom gracefully.

    There is intentionally no crash in release builds the name also says: CHECK_NONFATAL. There is only a crash in debug builds, but this is just for increasing debug UX.

    Do you understand the changes you are making, or is this generated by an LLM bot?

  14. DevPatel-11 commented at 8:18 am on January 23, 2026: none

    Yes I understand what I’m changing. The CHECK_NONFATAL was failing when createNewBlock returns null during shutdown/startup. I replaced it with a null check that returns an RPC error instead.

    I see your point about it only crashing in debug builds though. I was looking at the logs from issue #34262 and saw both cases happened during startup or shutdown, so I thought handling the nullptr would help.

    This is not generated by any LLM bot. I spent time debugging the issue and reading through the code.

  15. maflcko commented at 8:33 am on January 23, 2026: member

    Ok, fair enough.

    i just think the current approach (1) is worse than CHECK_NONFATAL, because:

    • To me this seems like a bug that should be fixed, so CHECK_NONFATAL is the right choice.
    • Using a “silent” RPC error just sweeps the bug under the carpet.

    Recall that release behavior is not changed at all by your change (in both cases an exception is thrown), just the message is changed.

    I don’t see why this should be done, when it would be better to just fix the root cause.

  16. DevPatel-11 commented at 11:36 am on January 23, 2026: none
    Got it, makes sense. I was focused on preventing the crash but didn’t think about it just hiding the real issue. Will close this and look at the root problem instead. Thanks for the feedback.
  17. DevPatel-11 closed this on Jan 23, 2026


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-01-27 06:13 UTC

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