rpc: fix a race between scantxoutset calls

bitcoin/bitcoin: PR #36959

The review is inconclusive; no findings were published.

Coverage details

Background

Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.

Documented decisions

#19362 addressed stale progress from a preceding scan being reported during preparation of a new scan. The original report observed 100% while deriving keys for a large XPUB range, followed by progress returning to zero only when UTXO scanning began. Progress therefore needed to be zero before descriptor/key preparation, not merely before iterating the UTXO set.

In the pinned code, start acquires the reservation before evaluating descriptors. Resetting progress as part of the previous reservation’s cleanup establishes the intended initial progress for that preparation phase.

Source 1 · Source 2

The zero-progress check was intended as an executable invariant, not as the operation that initializes progress. Promag favored retaining invariants because they make the code easier to understand; Jon Atack wanted a visible failure if progress was unexpectedly nonzero, particularly in the absence of test coverage. Luke described such a state as something that should be impossible.

This explains why reserve checks g_scan_progress after acquiring the global reservation: a newly available reservation was expected already to have clean progress state. It does not establish a separate rationale for checking before recording ownership in m_could_reserve.

Source 1 · Source 2 · Source 3

Ralph's inferences

The RAII structure expresses an owner-only cleanup contract: once reserve successfully records ownership, leaving the scope normally or through an exception releases the reservation and resets progress. A caller that fails to reserve must not clean up another caller’s scan. This covers errors during descriptor preparation as well as completion, interruption, or cooperative abortion of scanning.

The pinned start handler creates its reserver before argument validation and descriptor evaluation. The destructor conditions cleanup on m_could_reserve. Base and head retain this contract; the changed ordering concerns whether zero progress is established before the reservation becomes available again.

Start, status, and abort share the reservation protocol, rather than status and abort merely reading an independent busy flag. An idle status or abort call temporarily acquires ownership and releases it on return. When acquisition fails, status reads progress and abort requests cooperative cancellation without taking over cleanup. Abort is not a promise that scanning has stopped before the RPC returns.

Consequently, the clean-state-at-acquisition invariant applies to all three actions. Overlapping observers or cancellation requests are not intended to release an active scanner’s reservation; cleanup remains with the owning scope. The scan loop periodically checks the abort flag, and the RPC help explicitly describes cancellation as asynchronous.

Unresolved disagreements

The failure policy for a violated invariant remained contested. Luke explicitly warned that failure could block all future reservations and suggested removing the check, repairing progress, or releasing the reservation before throwing. Promag discussed an assertion and later an exchange-to-zero before checking. Marco Falke instead argued that restarting the node was possible and crashing it was unnecessary. The inspected record does not establish an agreed cleanup guarantee for failure before m_could_reserve is set.

This distinguishes the intended ordinary RAII cleanup guarantee from the exceptional acquisition-check path. Historical support for checking the invariant should not be read as proof that its failure was intended to unwind with the same guarantees as a successfully recorded reservation.

Source 1 · Source 2 · Source 3 · Source 4 · Source 5