WIP: mining: replace interrupt methods with cancellation arguments

bitcoin/bitcoin: PR #36097

1 major finding · 6 minor findings · 4 suggestions

Earlier concerns neither republished nor resolved: 1 minor finding · 3 suggestions. See the Run archive for their assessments.

Findings

Link

[major] · Cancelling a worker-thread destroy can deadlock the IPC event loop

A disconnect or dropped promise during a worker-thread destroy() can deadlock the shared IPC event loop; the triggering window is narrow and no natural occurrence is established.

Location: src/ipc/libmultiprocess/include/mp/proxy-io.h:739 (head).

Details and suggested fix

The temporary subtree update cd7867e1f72079d6b79355694785cc244bff9cd2 adds loop->sync() inside invokeDestroy(). For a Context-dispatched method such as BlockTemplate.destroy, ServerDestroy calls it while still holding request_lock. If cancellation reaches m_on_cancel after registration but before the event loop services that sync, the event loop waits for request_mutex while the worker waits for the event loop. All IPC clients on that loop stall, as do later synchronous cleanup calls. The base destruction path did not make this synchronous event-loop call. Release and reacquire the request lock around invokeDestroy(), as ServerCall does around wrapped methods, and add a regression test for cancellation during worker-thread destruction. This correction belongs in libmultiprocess.


Link

[minor] · Per-request cancellation leaks through shared interrupt flags

Cancellation can affect an unrelated later or concurrent call on the same object, although no current client was confirmed to make overlapping calls or cancel in the late window.

Location: src/node/interfaces.cpp:970 (head).

Details and suggested fix

The callbacks set m_interrupt_mining or m_interrupt_wait, but these object-member flags are cleared only when a wait observes them. A cancellation during CreateNewTemplate() or GetTip(), after the last flag check but before guard destruction, leaves a signal that the next call consumes and returns null. With concurrent Context workers, an uncancelled waiter can consume the signal first while the cancelled waiter continues waiting for a tip change or timeout. The base had these mechanics for explicit object-wide interrupts, not automatic per-request cancellation.

Use call-local cancellation state declared before the CancelGuard, and pass it directly through the manager's wait and cooldown operations, including the nested waits in createNewBlock(). In-process callers must also unregister or synchronize outstanding callbacks before that local state is destroyed. Add overlapping-call coverage that cancels only one request.


Link

[minor] · File-wide cancellation also makes queued submissions skippable

Dropping a queued submission promise can now prevent submission entirely; no in-tree client was found to drop submission promises.

Location: src/ipc/capnp/mining.capnp:9 (head).

Details and suggested fix

$Cxx.allowCancellation covers every method in mining.capnp, including submitBlock, submitSolution, and destroy. If a client abandons one of these requests before execution, such as while it is queued behind waitNext on the same Context thread, PassField skips the method entirely. A skipped submission never reaches block processing. A skipped destroy can still be recovered by releasing the capability. Put the annotation on waitTipChanged, createNewBlock, and waitNext individually, as the waitCancel test schema does, so the intended waits remain cancellable without changing abandoned side-effecting calls to best-effort execution.


Link

[minor] · The new Cap'n Proto minimum is not enforced by CMake

CMake accepts GCC with Cap'n Proto 0.9.2 or patched 0.10.x despite the new 1.0 requirement; the resulting schema-build failure is inferred, not reproduced.

Location: src/ipc/capnp/mining.capnp:9 (head).

Details and suggested fix

$Cxx.allowCancellation requires Cap'n Proto 1.0, and the subtree's updated installation instructions now state that minimum. However, find_package(CapnProto 0.9 NO_MODULE) and its compatibility checks still admit GCC with 0.9.2 or 0.10.3+, allowing configuration to succeed before the annotated schemas fail to compile. doc/dependencies.md still advertises 0.9.2, and the subtree's olddeps.bash pins it. Raise the configure-time minimum to 1.0 and align those documents and CI settings, with a release note for the dependency change. Depends builds already use a newer version and avoid this configuration.


Link

[minor] · The unclean-disconnect test now races its interruption log

Source inspection shows that interface_ipc.py can miss the earlier interruption log and no longer tests the non-null-interface return from #34250; no functional-test failure was reproduced.

Location: src/node/interfaces.cpp:928 (head).

Details and suggested fix

run_unclean_disconnect_test() drops the waitNext promise and leaves the connection scope before opening its "interrupted (canceled)" log expectation. The new callback can now interrupt the wait during promise disposal or disconnect, returning null and producing that message before the expectation starts. Since assert_debug_log() scans only bytes written after entry, it then times out waiting for a message already logged. Open the expectation before dropping or disconnecting, rather than relying on block generation to release this wait. Preserve the documented #34250 coverage separately with a method that still returns a non-null interface after disconnect and does not register an early-cancellation callback.


Link

[minor] · The waitCancel IPC test never reaches its server method

Without a ThreadMap exchange, the new test can pass through client-side cancellation alone and does not exercise the server cancellation hook.

Location: src/ipc/test/ipc_tests.cpp:142 (head).

Details and suggested fix

IpcPipeTest() bootstraps FooInterface directly, and that schema has no initialization method exchanging Proxy.ThreadMap. Its new Context-bearing call therefore pipelines makeThreadRequest() through a null capability and cannot dispatch FooImplementation::waitCancel. Meanwhile, the canceler fires as soon as parameter building supplies its callback, and handle_error translates any subsequent error into InterruptException once cancellation is set. Exchange a ThreadMap before the call, following the subtree tests' initialization pattern, then wait for a server-side registration signal before cancelling and assert that the server method exits.


Link

[minor] · Trailing whitespace in the cancellation blocks fails the lint

The new whitespace at interfaces.cpp:930 and :971 matches the trailing-whitespace lint; this is established by source inspection, not a recorded lint run.

Location: src/node/interfaces.cpp:930 (head).

Details and suggested fix

Line 930 contains only spaces, and line 971 has spaces after the closing brace. lint_trailing_whitespace() searches tracked files for \s$, and its exclusions do not cover src/node/interfaces.cpp. Remove the trailing spaces from these two lines.


Optional improvements

Link

[suggestion] · The interrupt methods become unsupported without a migration note

Clients using makeMining @3 can still connect, but their previously working interrupt @6 and interruptWait @9 calls now fail rather than remain functional deprecated methods.

Location: src/interfaces/mining.h:152 (head).

Details and suggested fix

Both retained ordinals map to default methods that throw, including when a client uses them to release waits during shutdown. The PR describes this as deprecation, while the v31 release notes explicitly advertise Mining.interrupt(). Either retain the existing flag-based implementations for a compatibility period or describe the hard break in the PR and add a release note explaining migration to promise-drop cancellation, following the earlier mining-interface migration notice. Clients using makeMiningOld2 were already rejected by the base and are not newly affected here.


Link

[suggestion] · The functional cancellation checks do not establish worker release

The new mining IPC assertions accept cancellation logs emitted before callback execution or without starting the method, so they do not prove that cancellation stopped an active wait.

Location: test/functional/interface_ipc_mining.py:168 (head).

Details and suggested fix

drop_promise() cancels before its first yield, and all three checks match only "canceled". That also matches "canceled before it could be executed" and "canceled while executing", which is logged before cancel_fn runs. Thus these assertions can succeed with a missing mining callback while an executing worker remains blocked. The createNewBlock case also follows submission of the previously header-only block, so it does not establish an active cooldown. Establish a blocking condition and synchronize with execution before cancelling, then require the post-return "interrupted (canceled)" message or a bounded follow-up call on the same Context worker.


Link

[suggestion] · A failed cancellation assertion aborts the miner test binary

If the new BOOST_REQUIRE fails, the still-joinable canceler thread causes std::terminate instead of allowing normal test-framework failure handling.

Location: src/test/miner_tests.cpp:178 (head).

Details and suggested fix

The fatal null-result assertion precedes canceler.join(). If waitNext() returns a template after a regression, assertion unwinding destroys a joinable std::thread and aborts the binary, preventing the remaining tests from running. Store the result, join the canceler, and then apply BOOST_REQUIRE to the stored result.


Link

[suggestion] · Generic cancellation plumbing and the mining migration share one review unit

Separating the generic cancellation support from its mining consumer would let reviewers validate callback lifetime and locking independently of the client-visible API change.

Location: src/ipc/capnp/common-types.h:192 at 201738bd9889 (commit) (commit).

Details and suggested fix

201738bd98897f2213f342414ef2214e6ba86728 combines CancelFn/CancelGuard/CancelArg, CancelState, the common-types extra-parameter hooks, and waitCancel coverage with the mining migration that makes ordinals @6/@9 throw. Split it after the prerequisite subtree update cd7867e1f72079d6b79355694785cc244bff9cd2: first add the generic types, hooks, and corrected IPC test setup; then change the mining interfaces, implementations, and mining tests. The generic part has no dependency on the new mining signatures, and common-types.h already included interfaces/types.h. This keeps the final tree unchanged while separating the cancellation synchronization review from the protocol migration and allowing each part to be validated independently.


Coverage details

Earlier concerns

Earlier reports raised these, with the titles and severities shown there. This review neither republished nor resolved them.