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
+82
View File
@@ -0,0 +1,82 @@
// Copyright (c) 2016-2024 The Hush developers
// Distributed under the GPLv3 software license, see the accompanying
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
#include <gtest/gtest.h>
#include <memory>
#include <utility>
#include "asyncrpcoperation.h"
#include "wallet/asyncrpcoperation_autoshieldcoinbase.h"
#include "wallet/asyncrpcoperation_saplingconsolidation.h"
#include "wallet/asyncrpcoperation_sweep.h"
namespace {
// Lets a test put an operation into any state, without running it.
template <class Op>
class Probe : public Op {
public:
template <class... Args>
explicit Probe(Args&&... args) : Op(std::forward<Args>(args)...) {}
void Set(OperationStatus status) { this->set_state(status); }
};
// An operation that does not override cancel(): the base behaviour.
class PlainOp : public AsyncRPCOperation {
public:
void main() override {}
void Set(OperationStatus status) { set_state(status); }
};
int S(OperationStatus status) { return static_cast<int>(status); }
// The queue's map and the wallet schedulers hold every operation as a shared_ptr<AsyncRPCOperation>, so
// that is how cancel() is called here. Before cancel() was virtual, each of these overrides was dead.
template <class P>
void CheckOverride(std::shared_ptr<P> probe)
{
std::shared_ptr<AsyncRPCOperation> op = probe;
probe->Set(OperationStatus::EXECUTING);
op->cancel();
EXPECT_TRUE(op->isCancelled()) << "a running round can be stopped";
for (OperationStatus done : {OperationStatus::SUCCESS, OperationStatus::FAILED}) {
probe->Set(done);
op->cancel();
EXPECT_EQ(S(done), S(op->getState())) << "a finished round is not relabelled as cancelled";
}
probe->Set(OperationStatus::READY);
op->cancel();
EXPECT_TRUE(op->isCancelled()) << "a round that has not started is cancelled, as before";
}
}
TEST(AsyncRPCCancel, TheSweepOverrideIsReachedThroughTheBasePointer) {
CheckOverride(std::make_shared<Probe<AsyncRPCOperation_sweep>>(100));
}
TEST(AsyncRPCCancel, TheConsolidationOverrideIsReachedThroughTheBasePointer) {
CheckOverride(std::make_shared<Probe<AsyncRPCOperation_saplingconsolidation>>(100, SaplingConsolidationRequest()));
}
TEST(AsyncRPCCancel, TheAutoshieldOverrideIsReachedThroughTheBasePointer) {
CheckOverride(std::make_shared<Probe<AsyncRPCOperation_autoshieldcoinbase>>(100));
}
TEST(AsyncRPCCancel, AnOperationWithoutAnOverrideOnlyCancelsBeforeItStarts) {
auto plain = std::make_shared<PlainOp>();
std::shared_ptr<AsyncRPCOperation> op = plain;
plain->Set(OperationStatus::EXECUTING);
op->cancel();
EXPECT_TRUE(op->isExecuting()) << "z_sendmany and friends are not interrupted mid-run";
plain->Set(OperationStatus::READY);
op->cancel();
EXPECT_TRUE(op->isCancelled());
}