WIP: mining: replace interrupt methods with cancellation arguments #36097

Draft
xyzconstant wants to merge 2 commits from ipc-cancellation-support into master AGit
xyzconstant commented 2026-08-27 05:32:30 +00:00 (Migrated from github.com)

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.

**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 commented 2026-08-27 05:32:40 +00:00 (Migrated from github.com)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36097 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36097.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36097#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. A summary of reviews will appear here. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36747](https://github.com/bitcoin/bitcoin/pull/36747) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36747.svg"></sub> (Update libmultiprocess subtree to add nonunix platform support and fix race conditions by ryanofsky) * [#36525](https://github.com/bitcoin/bitcoin/pull/36525) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36525.svg"></sub> (mining: add getTotalFees(), protect m_block_template with mutex by Sjors) * [#36257](https://github.com/bitcoin/bitcoin/pull/36257) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36257.svg"></sub> (qa: assert_equals -> assert_true/assert_false by hodlinator) * [#36167](https://github.com/bitcoin/bitcoin/pull/36167) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36167.svg"></sub> ([RFC] Enable `-Wunused` by fanquake) * [#36074](https://github.com/bitcoin/bitcoin/pull/36074) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36074.svg"></sub> (scripted-diff: [test] Add util/check.h includes for assertions by maflcko) * [#35671](https://github.com/bitcoin/bitcoin/pull/35671) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35671.svg"></sub> (mining: add TxCollection to bandwidth-efficiently validate external block templates by Sjors) * [#35511](https://github.com/bitcoin/bitcoin/pull/35511) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35511.svg"></sub> (RFC: consensus: Make `CAmount` a class by hodlinator) * [#33922](https://github.com/bitcoin/bitcoin/pull/33922) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/33922.svg"></sub> (mining: add getMemoryLoad() and track template non-mempool memory footprint by Sjors) * [#32387](https://github.com/bitcoin/bitcoin/pull/32387) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/32387.svg"></sub> (ipc: add windows support by ryanofsky) * [#29409](https://github.com/bitcoin/bitcoin/pull/29409) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/29409.svg"></sub> (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. <!--5faf32d7da4f0f540f40219e4f7537a3-->
DrahtBot commented 2026-08-27 06:45:36 +00:00 (Migrated from github.com)

🚧 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.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `test ancestor commits`: https://github.com/bitcoin/bitcoin/actions/runs/33042822923/job/98420040676</sub> <sub>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`.</sub> <details><summary>Hints</summary> 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. </details>
ryanofsky commented 2026-08-27 20:58:38 +00:00 (Migrated from github.com)

Wow, this is very cool. The PR contains a lot of boilerplate updates but the heart of the change (5cdb565ee5) 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.

Wow, this is very cool. The PR contains a lot of boilerplate updates but the heart of the change (5cdb565ee565b9cd61b3286a3945d7244d0f744c) is simply: ```diff --- 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.
xyzconstant commented 2026-09-11 20:39:52 +00:00 (Migrated from github.com)

Updated to match the latest changes in the upstream PR.

5cdb565ee5 -> 1c02f2abea (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.
Updated to match the latest changes in the [upstream PR](https://github.com/bitcoin-core/libmultiprocess/pull/342). 5cdb565ee565b9cd61b3286a3945d7244d0f744c -> 1c02f2abea5f3d72fa4887ec8f76e85f51276bbb ([compare](https://github.com/bitcoin/bitcoin/compare/5cdb565ee565b9cd61b3286a3945d7244d0f744c..1c02f2abea5f3d72fa4887ec8f76e85f51276bbb)) 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.
DrahtBot commented 2026-09-24 05:43:03 +00:00 (Migrated from github.com)

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

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
First-time contributor

This is a draft pull request, so Ralph reviewed only its concept. Code review runs on the first push after it is marked ready for review.

Earlier concerns neither republished nor resolved: 1 major finding · 7 minor findings · 7 suggestions. See the Run archive linked from the Review report for their assessments.

Design and approach

The goal is to stop unwanted mining waits without destroying templates needed for late solutions. This approach is worth pursuing: a Rust client already drops waitNext promises, and the proposed integration connects abandonment to the existing cooperative interruption paths, avoiding a separate interrupt RPC. This improves a usable explicit-interruption baseline; it is not forced termination of template assembly.

The decisive question is whether overlapping waits on one Mining or BlockTemplate object must be independently cancelable. Tokens still feed shared per-object flags, so request-level cancellation does not establish mining-level isolation. Define that contract and the supported-client migration policy before replacing interrupt() and interruptWait(), which now return upgrade errors. Treat libmultiprocess cancellation support as a prerequisite. This is a concept assessment only, not a code-correctness review.

Review coverage was incomplete.


Reviewed: 4e377f98a534 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:e1a29e5e677c47d383756f0fcf8f3760725c91d1104dd3ccb2f333b6217eba26:47880f5569fd2047d249266f337c4f2a509adf7d96c3ccb1c7644c2c2a650bff --> <!-- ralph-base:966e9800e84ee96bf4f23a4732ed30c275ef1db9 --> <!-- ralph-head:4e377f98a534a684198392cbe1eb88d3261fba99 --> <!-- ralph-case:edec93caf68b3c2f7ee35b891d69a684c9b8fc847f3a1c02963d7f3510ea7fe4 --> This is a draft pull request, so Ralph reviewed only its concept. Code review runs on the first push after it is marked ready for review. Earlier concerns neither republished nor resolved: 1 major finding · 7 minor findings · 7 suggestions. See the Run archive linked from the Review report for their assessments. ##### Design and approach The goal is to stop unwanted mining waits without destroying templates needed for late solutions. This approach is worth pursuing: a Rust client [already drops `waitNext` promises](https://github.com/bitcoin/bitcoin/issues/33575#issuecomment-3383146351), and the [proposed integration](https://git.fish.foo/bitcoin/bitcoin/pulls/36097) connects abandonment to the existing cooperative interruption paths, avoiding a separate interrupt RPC. This improves a usable explicit-interruption baseline; it is not forced termination of template assembly. The decisive question is whether overlapping waits on one `Mining` or `BlockTemplate` object must be independently cancelable. Tokens still feed shared per-object flags, so request-level cancellation does not establish mining-level isolation. Define that contract and the supported-client migration policy before replacing `interrupt()` and `interruptWait()`, which now return upgrade errors. Treat libmultiprocess cancellation support as a prerequisite. This is a concept assessment only, not a code-correctness review. Review coverage was incomplete. <!-- Review tier: routine --> --- Reviewed: [`4e377f98a534`](<https://git.fish.foo/bitcoin/bitcoin/commit/4e377f98a534a684198392cbe1eb88d3261fba99>) · [Review report](<https://review.fish.foo/report/36097-4e377f98a534a684198392cbe1eb88d3261fba99-3424-7.html>) · [Previous reports](<https://review.fish.foo/traces/36097/>) [permalink](<https://review.fish.foo/report/36097-4e377f98a534a684198392cbe1eb88d3261fba99-3424-7.html>)
DrahtBot commented 2026-10-08 16:18:33 +00:00 (Migrated from github.com)

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

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
DrahtBot commented 2026-11-05 14:45:05 +00:00 (Migrated from github.com)

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

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
DrahtBot commented 2026-12-02 04:20:50 +00:00 (Migrated from github.com)

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

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36097
No description provided.