asyncrpc: make AsyncRPCOperation::cancel() virtual, so a running auto-op can be stopped

The base class said "Override this method if you can interrupt execution of main()", but cancel()
was not virtual. Every caller holds a std::shared_ptr<AsyncRPCOperation> -- the queue's map, the
wallet schedulers, z_consolidate -- so the overrides in the sweep, consolidation and autoshield ops,
written to move an EXECUTING operation to CANCELLED, never ran: the base version only cancels an
operation that has not started. In practice that meant a stop could not interrupt a running
automatic round; the node waited for it to finish every remaining transaction. Consolidation had
grown its own ShutdownRequested() checks as a workaround.

- cancel() is virtual, and the three overrides are marked override, so this cannot silently
  regress: with the base non-virtual again they fail to compile.
- Only the queue closing at shutdown can cancel a running round. The schedulers return early
  while a round is in flight (the sweep since its in-flight guard, consolidation and autoshield on
  their running flags) and z_consolidate refuses while a consolidation runs, so they only ever
  cancel an operation that has finished, which the overrides leave alone.
- A round cancelled part-way stays CANCELLED instead of being relabelled SUCCESS or FAILED by how
  much of it got done, and logs "(status=cancelled)".
- The sweep checks isCancelled() || ShutdownRequested() before starting each address as well as
  after each build, so a stop does not wait for another multi-second build to be discarded.
- Comments that described the old behaviour are corrected.

New gtest AsyncRPCCancel: each override is reached through a shared_ptr<AsyncRPCOperation> and
stops an EXECUTING op, leaves SUCCESS/FAILED alone and still cancels a READY one; an op without an
override still only cancels before it starts (z_sendmany and friends are unaffected). With the old
declarations, the three override tests fail. Regtest, same fixture, the node stopped while a sweep
round was building its first of four transactions: dev took 56.2 s to exit, building all four first;
this exits in 9-12 s ("Canceled. Stopping.", status=cancelled), and the wallet is intact on restart.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-20 19:05:53 -06:00
co-authored by Claude Opus 5.5
parent b0e0d211d2
commit 2ff5c5d541
10 changed files with 139 additions and 42 deletions
+5 -5
View File
@@ -643,11 +643,11 @@ void CWallet::RunSaplingSweep(int blockHeight) {
return;
}
// Self-guard: let an in-flight round finish. Re-dispatching on every block, as this used to, never
// stopped the running op -- AsyncRPCOperation::cancel() is not virtual, so through the queue's base
// pointer it only cancels an op that has not started -- it just queued a fresh round behind it,
// which then repeated note selection over the whole wallet for nothing. (On a single async worker
// a replacement could not get past a hung op anyway.)
// Self-guard: let an in-flight round finish. Re-dispatching on every block, as this used to, cancels
// the running op (now that AsyncRPCOperation::cancel() is virtual, the sweep's override can stop an
// executing round), discarding the transaction it is building -- on a wallet where one build
// outlasts a block, nothing would ever be swept. Before cancel() was virtual it merely queued a
// redundant round behind the running one.
if (fSweepInFlight) {
LogPrintf("%s: sweep operation %s (enqueued at height %d) is still running at blockHeight=%d; "
"not re-dispatching\n", __func__, saplingSweepOperationId, sweepDispatchHeight, blockHeight);