Skip to content

mining: replace interrupt methods with cancellation arguments - #36097

Draft
nullfail wants to merge 2 commits into
bitcoin:masterfrom
nullfail:ipc-cancellation-support
Draft

nullfail wants to merge 2 commits into
bitcoin:masterfrom
nullfail:ipc-cancellation-support

Conversation

@nullfail

@nullfail nullfail commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

Depends on bitcoin-core/libmultiprocess#342. Only the last commit belongs to this PR.


The blocking methods (waitTipChanged, createNewBlock, and BlockTemplate::waitNext) currently rely on interrupt()/interruptWait() to cancel them when they are in progress (#33676, #34184). With request cancellation support in libmultiprocess (bitcoin-core/libmultiprocess#342), this is no longer needed.

This PR adds an optional StopToken parameter to those blocking methods and deprecates the interrupt methods. Each method registers a StopCallback on the token that interrupts the wait when stop is requested, or the IPC request is canceled.

In the capnp schema, use the Proxy.extraParam annotation for the C++ StopToken parameter and the Cxx.allowCancellation annotation to enable request cancellation at the RPC layer.

interfaces::StopToken is std::stop_token where the standard library provides it by default, and libmultiprocess's mp/type-stop-token.h makes it cancelable over IPC. libc++ 19, the minimum supported Clang version, and the FreeBSD base-system library only provide it with -fexperimental-library. There, interfaces::StopToken is a minimal replacement type with stop_token/stop_source/stop_callback semantics, and the CustomBuildExtraParam/CustomReadExtraParam overloads in ipc/capnp/common-types.h make it cancelable over IPC. These can be removed once the minimum supported libc++ provides std::stop_token by default.

In tests, update miner_tests to stop waitNext() from another thread, ipc_tests to cancel a waitCancel call by requesting stop, and interface_ipc_mining.py to drop the response promise and check that the node logs the cancellation. Also add drop_promise() to ipc_util.py to abandon capnp promises.

Additional note: this is the Bitcoin Core side of approach 4 in #33575.

@DrahtBot

DrahtBot commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

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

External sites

Reviews

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36747 (Update libmultiprocess subtree to add nonunix platform support and fix race conditions by ryanofsky)
  • #36525 (mining: add getTotalFees(), protect m_block_template with mutex by Sjors)
  • #36257 (qa: assert_equals -> assert_true/assert_false by hodlinator)
  • #36167 ([RFC] Enable -Wunused by fanquake)
  • #36074 (scripted-diff: [test] Add util/check.h includes for assertions by maflcko)
  • #35671 (mining: add TxCollection to bandwidth-efficiently validate external block templates by Sjors)
  • #35511 (RFC: consensus: Make CAmount a class by hodlinator)
  • #33922 (mining: add getMemoryLoad() and track template non-mempool memory footprint by Sjors)
  • #32387 (ipc: add windows support by ryanofsky)
  • #29409 (ipc: Expose Chain interface to IPC clients by ryanofsky)

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.

@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task test ancestor commits: https://github.com/bitcoin/bitcoin/actions/runs/33042822923/job/98420040676
LLM reason (✨ experimental): CI failed due to a clang build error treated as fatal: a C++23 lambda attribute ([[noreturn]]) is used in mp/proxy-types.h, triggering -Wc++23-lambda-attributes under -Werror.

Hints

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.

@ryanofsky

Copy link
Copy Markdown
Contributor

Wow, this is very cool. The PR contains a lot of boilerplate updates but the heart of the change (5cdb565) is simply:

--- a/src/node/interfaces.cpp
+++ b/src/node/interfaces.cpp
@@ -926,8 +926,9 @@ public:
         return SubmitBlock(chainman(), std::make_shared<const CBlock>(m_block_template->block), reason, debug);
     }
 
-    std::unique_ptr<BlockTemplate> waitNext(BlockWaitOptions options) override
+    std::unique_ptr<BlockTemplate> waitNext(BlockWaitOptions options, interfaces::CancelArg cancel) override
     {
+        if (cancel) cancel([this] { InterruptWait(notifications(), m_interrupt_wait); });
         auto new_template = WaitAndCreateNewBlock(chainman(),
                                                   notifications(),
                                                   m_node.mempool.get(),
@@ -939,11 +940,6 @@ public:
         return nullptr;
     }
 
-    void interruptWait() override
-    {
-        InterruptWait(notifications(), m_interrupt_wait);
-    }
-
     const BlockCreateOptions m_create_options;
 
     const std::unique_ptr<CBlockTemplate> m_block_template;

Dropping the interruptWait method, replacing it with a CancelArg argument, and calling the argument with a lambda containing the code that used to be in interruptWait method.

With that change rust, python, and c++ clients can now cancel blocking requests more simply and efficiently using cap'n protos native cancellation mechanism, instead of needing to make new IPC calls to cancel previous ones.

There's an nontrivial amount of code to review here, but this is a very nice end-to-end implementation and demonstration that supporting this cancellation mechanism is worthwhile and makes things simpler for clients and servers. It could also become more important in the future if we want to have blocking calls that are individually cancellable instead of the current situation where interrupt methods can cancel multiple calls.

@nullfail

Copy link
Copy Markdown
Contributor Author

Updated to match the latest changes in the upstream PR.

5cdb565 -> 1c02f2a (compare)

Notable changes:

  • libmultiprocess no longer ships mp/type-cancel.h, so the CustomBuildExtraParam/CustomReadExtraParam overloads for CancelArg now live in ipc/capnp/common-types.h, and CancelFn/CancelArg moved to interfaces/types.h.
  • CancelArg now returns a CancelGuard that unregisters the callback when destroyed, so the event loop can't run it after the wrapped method goes out of scope.
  • Added a cancelable waitCancel method to the ipc_tests FooInterface to exercise both the client and server hooks.

nullfail added a commit to nullfail/bitcoin-capnp-types that referenced this pull request Sep 27, 2026
nullfail added a commit to nullfail/bitcoin-capnp-types that referenced this pull request Sep 27, 2026
Mirror bitcoin/bitcoin#36097. Long-running calls can now be stopped by
dropping their promise, so explicit interrupt methods are no longer
needed.
nullfail added a commit to nullfail/bitcoin-capnp-types that referenced this pull request Sep 27, 2026
nullfail added a commit to nullfail/bitcoin-capnp-types that referenced this pull request Sep 28, 2026
Mirror bitcoin/bitcoin#36097. Long-running calls can now be stopped by
dropping their promise, so explicit interrupt methods are no longer
needed.
nullfail added a commit to nullfail/bitcoin-capnp-types that referenced this pull request Sep 28, 2026
@nullfail
nullfail force-pushed the ipc-cancellation-support branch from a066bd8 to a4e7b2e Compare October 8, 2026 17:40
@nullfail
nullfail force-pushed the ipc-cancellation-support branch from a4e7b2e to 201738b Compare November 13, 2026 20:51
Add an optional StopToken parameter to the blocking Mining methods
waitTipChanged, createNewBlock, and waitNext, and deprecate
interrupt() and interruptWait(). Each method registers a StopCallback
that interrupts the wait when stop is requested or the request is canceled.

Use std::stop_token for interfaces::StopToken where the standard library
provides it. Since libc++ 19 only provides it with -fexperimental-library,
use a minimal replacement with stop_token, stop_source, and
stop_callback semantics, made cancelable over IPC by overloads in
ipc/capnp/common-types.h.

In the Cap'n Proto schema, annotate the C++ StopToken parameter with
Proxy.extraParam and enable RPC request cancellation with
Cxx.allowCancellation.

Update miner_tests to stop waitNext() from another thread; update
ipc_tests to cancel a call by requesting stop; and update
interface_ipc_mining.py to drop the response promise and verify that the
node logs the cancellation. Add drop_promise() to ipc_util.py to abandon
Cap'n Proto promises.
@nullfail
nullfail force-pushed the ipc-cancellation-support branch from d5aad83 to 4e377f9 Compare December 2, 2026 04:17
@DrahtBot

DrahtBot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

🐙 This pull request conflicts with the target branch and needs rebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants