wallet: parallel fast rescan

bitcoin/bitcoin: PR #34400

3 minor findings · 6 suggestions

Earlier concerns neither republished nor resolved: 1 major finding · 1 suggestion. See the Run archive for their assessments.

Findings

Link

[minor] · Parallel read-ahead continues through disconnected successors

With block filters, -walletpar>1 and no IBD, a reorg during read-ahead can turn an otherwise successful rescan into a failure or extend its stale endpoint; production frequency is unmeasured.

Location: src/wallet/scan.cpp:438 (head).

Details and suggested fix

fill_queue saves successor hashes before their verdicts are delivered. If a reorg disconnects them, a NO_MATCH result still reaches RecordSkipped, and delivery continues through its saved successors. For example, a skipped stale block followed by a stale matching block makes the scan return FAILURE. Serial traversal instead finds no successor when processing the first stale block and ends successfully there. An entirely nonmatching stale tail can advance last_scanned_block farther along the disconnected branch. AttachChain holds cs_wallet throughout the scan, so pending disconnect notifications cannot shorten that tail. The failure rejects wallet loading, while a successful stale endpoint remains until notifications correct it. This does not establish lost transactions or funds, and a retry after the reorg succeeds.

Revalidate queued blocks at delivery and discard the remaining queued and in-flight tail, including next_block, when a skipped block has left the active chain. Preserve the existing failure handling for a stale block requiring inspection. Add a parallel test that reorganizes the chain after read-ahead has queued the old successors.


Link

[minor] · Parallel skipped batches can suppress GUI progress updates

An all-NO_MATCH parallel scan from an even height to an odd height, without top-ups, emits no intermediate GUI progress updates even though the scan continues.

Location: src/wallet/scan.cpp:578 (head).

Details and suggested fix

Wait() collects one two-block task, and Scan() reports progress only at the last skipped height of that delivery. The even-sized windows preserve this alignment, so every sampled height is odd. UpdateProgress() still emits ShowProgress only for heights divisible by 100, leaving the dialog or startup splash at its initial value until the final completion signal. Base called it for every block. Scan results and the atomic progress exposed by getwalletinfo remain unaffected; matches, top-ups, or a partial final task can change the alignment. Emit GUI progress when a 100-block boundary has been crossed rather than requiring an exact sampled height. Add an all-skipped parallel test that checks intermediate ShowProgress events.


Link

[minor] · Parallel rescans defer cancellation until the batch finishes

Unlike base's per-block checks, the parallel scan can read and sync the rest of a matching batch after abort or shutdown is requested; the additional delay is unmeasured.

Location: src/wallet/scan.cpp:579 (head).

Details and suggested fix

The outer loop checks m_abort and shutdownRequested(), but the loop over blocks_to_scan does not. Normally this batch contains one two-block task. After a keypool top-up, UpdateFilter() drains all in-flight tasks, allowing consecutive MATCH or NO_FILTER results from up to two pre-shrink windows to form one batch, bounded by 128 blocks at the thread cap. Each remaining entry can require a full block read and transaction sync. Check cancellation at the top of each inner-loop iteration to restore the per-block behavior. Remaining filter tasks also run during ThreadPool destruction and could return early on cancellation. abortrescan itself returns after setting the flag; the running scan waits and ultimately reports USER_ABORT.


Optional improvements

Link

[suggestion] · -walletpar does not bound workers across concurrent wallet rescans

Outside IBD, concurrent filtered rescans each receive the auto-detected thread budget, up to 15 dedicated workers apiece; their combined effect on live validation is unmeasured.

Location: src/wallet/scan.cpp:541 (head).

Details and suggested fix

LoadWalletArgs() applies -walletpar=0 independently to each wallet, using the core count capped at 16 total threads. Each eligible Scan() creates a separate pool, and reservations exclude overlap only within one wallet. Two capped rescans therefore start 30 dedicated workers. The IBD exclusion does not bound this allocation outside IBD. Make the intended budget explicit and consider a shared limiter to bound aggregate usage, accepting reduced throughput when scans overlap. Before retaining full auto as the default, measure block-connection latency during rescanning, including concurrent wallets, or choose a conservative default. The supplied single-rescan throughput benchmarks do not establish this effect, and cs_main covers only the block-index lookup, not filter reading or matching.


Link

[suggestion] · The late-disconnect ordering lacks regression coverage

The existing reorg test would still pass with base's early active-chain check, so it does not protect the late-disconnect fix in 8382329d.

Location: src/wallet/scan.cpp:504 (head).

Details and suggested fix

scan_for_wallet_transactions_reorged_block invalidates the block and builds the replacement branch before calling Scan(). Both the old check during successor lookup and the new check inside ScanBlock() reject that already-stale matching block. Add a case that disconnects a matching block from a ShowProgress callback, after successor lookup but before ScanBlock(). As the tip-extension test already does, select the callback height from the actual chain height so it is divisible by 100, and ensure the scan has a nonzero progress range. Assert FAILURE and that the transaction is not recorded as confirmed, with walletpar set to both 1 and 4. This would distinguish the late membership check from the old ordering; no current runtime failure is claimed.


Link

[suggestion] · -walletpar help understates the accepted thread maximum

The help advertises a maximum of 15, but -walletpar accepts 16 total threads, including the calling scan thread.

Location: src/wallet/init.cpp:79 (head).

Details and suggested fix

The help prints MAX_WALLETPAR, which limits dedicated workers. LoadWalletArgs() instead clamps the configured total to MAX_WALLETPAR + 1, and ParallelFilterChecker starts num_threads - 1 workers. Thus -walletpar=16 runs 15 workers plus the caller. Print the total maximum of 16 in the help and state that the calling thread counts toward the setting.


Link

[suggestion] · The new rescan default has no release note

The PR adds -walletpar and enables parallel filtered rescans automatically on eligible multicore nodes, but does not document this user-visible change in release notes.

Location: src/wallet/wallet.h:140 (head).

Details and suggested fix

DEFAULT_WALLETPAR=0 selects an auto-detected total, and filtered scans use the parallel checker outside IBD when that total exceeds one. Add a release note describing the block-filter-index prerequisite, the auto default and cap including the calling thread, the serial fallback during IBD, and -walletpar=1 as the explicit serial override. This gives users a way to understand and control the changed resource usage.


Link

[suggestion] · Worker creation failure has no serial fallback

If worker-thread creation fails, an otherwise viable serial rescan fails instead; no environment exhibiting this trigger was identified, so this is availability hardening.

Location: src/wallet/scan.cpp:258 (head).

Details and suggested fix

ParallelFilterChecker calls ThreadPool::Start() during construction, where creating a std::thread can throw std::system_error. The exception escapes Scan() before any blocks are processed, and startup wallet loading turns it into an initialization error. The pool destructor joins any workers already created, so recovery can safely retain the existing filter and serial path. Catch thread-creation failure around checker construction, log a warning, and leave ctx.checker unset so NextBlocksSerial() completes the scan without extra workers.


Link

[suggestion] · The parallel commit also rewrites the serial filter API

Separating the serial FastWalletRescanFilter rewrite would let reviewers validate serial equivalence before reviewing the parallel checker; the final tree need not change.

Location: src/wallet/scan.cpp:118 at 4830caf469e0 (commit) (commit).

Details and suggested fix

At commit 34a0646b74a1b79bfd22ae4a65b992c743734281 (parent=null), the preparatory serial refactor still uses UpdateIfNeeded() and AddScriptPubKeys(). Commit 4830caf469e09e70dfe505f842c77452077a9d6d (parent=null) replaces them with Update(GCSFilter::ElementSet* added = nullptr), updates range ends in place, inlines constructor population, and changes the serial caller alongside the new pool and window logic. Move that Update() rewrite, constructor inlining, and serial call change into a separate refactor immediately after the preparatory commit. Leave NeedsUpdate(), GetElementSet(), and the parallel checker in the parallel commit. The defaulted parameter keeps the intermediate serial code usable, preserves buildability, and leaves the final tree unchanged.


Coverage details

Earlier concerns

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