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:
@@ -54,11 +54,10 @@ void AsyncRPCOperation_sweep::main() {
|
||||
|
||||
stop_execution_clock();
|
||||
|
||||
if (success) {
|
||||
set_state(OperationStatus::SUCCESS);
|
||||
} else {
|
||||
set_state(OperationStatus::FAILED);
|
||||
}
|
||||
// A round cancelled part-way (only the queue closing at shutdown cancels a running op) stays
|
||||
// CANCELLED rather than being relabelled by how much of it got done.
|
||||
if (!isCancelled())
|
||||
set_state(success ? OperationStatus::SUCCESS : OperationStatus::FAILED);
|
||||
|
||||
// Scheduler bookkeeping, done here so it runs on success AND failure AND
|
||||
// exception (main_impl's terminal code is skipped when it throws). Only the
|
||||
@@ -78,7 +77,9 @@ void AsyncRPCOperation_sweep::main() {
|
||||
}
|
||||
|
||||
std::string s = strprintf("%s: Sweep operation finished. (status=%s", getId(), getStateAsString());
|
||||
if (success) {
|
||||
if (isCancelled()) {
|
||||
s += ")\n";
|
||||
} else if (success) {
|
||||
s += strprintf(", success)\n");
|
||||
} else {
|
||||
s += strprintf(", error=%s)\n", getErrorMessage());
|
||||
@@ -271,6 +272,11 @@ bool AsyncRPCOperation_sweep::main_impl() {
|
||||
bool sweepComplete = true;
|
||||
|
||||
for (std::map<libzcash::SaplingPaymentAddress, std::vector<SaplingNoteEntry>>::iterator it = mapAddresses.begin(); it != mapAddresses.end(); it++) {
|
||||
// Stop before starting another address: building takes seconds, and a stop would only discard it.
|
||||
if (isCancelled() || ShutdownRequested()) {
|
||||
LogPrintf("%s: %s. Stopping.\n", getId(), isCancelled() ? "Canceled" : "Shutting down");
|
||||
break;
|
||||
}
|
||||
auto addr = (*it).first;
|
||||
auto saplingEntries = (*it).second;
|
||||
|
||||
@@ -385,8 +391,8 @@ bool AsyncRPCOperation_sweep::main_impl() {
|
||||
}
|
||||
CTransaction tx = maybe_tx.get();
|
||||
|
||||
if (isCancelled()) {
|
||||
LogPrintf("%s: Canceled. Stopping.\n", getId());
|
||||
if (isCancelled() || ShutdownRequested()) {
|
||||
LogPrintf("%s: %s. Stopping.\n", getId(), isCancelled() ? "Canceled" : "Shutting down");
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -447,11 +453,12 @@ void AsyncRPCOperation_sweep::setSweepResult(int numTxCreated, const CAmount& am
|
||||
}
|
||||
|
||||
void AsyncRPCOperation_sweep::cancel() {
|
||||
// Cancelling is how the scheduler stops an in-flight round, so unlike the base
|
||||
// class this must be able to move an EXECUTING operation to CANCELLED. What it
|
||||
// must not do is overwrite a state that is already terminal: the scheduler
|
||||
// cancels the previous operation when it enqueues the next one, and that one may
|
||||
// have already SUCCEEDED, whose result would otherwise be relabelled as cancelled.
|
||||
// Lets the queue stop an executing round when it closes at shutdown, so unlike the
|
||||
// base class this must be able to move an EXECUTING operation to CANCELLED. The
|
||||
// scheduler never cancels a running round (it returns early while one is in flight);
|
||||
// it cancels the previous operation only when enqueueing the next, by which time
|
||||
// that one is terminal -- and a terminal state must not be overwritten, or a
|
||||
// SUCCEEDED result would be relabelled as cancelled.
|
||||
if (isSuccess() || isFailed() || isCancelled())
|
||||
return;
|
||||
set_state(OperationStatus::CANCELLED);
|
||||
|
||||
Reference in New Issue
Block a user