Repository navigation
Description
Summary
walletpassphrase still runs the whole deferred P2MR refill, and a full TopUpKeyPool(), inline under cs_wallet.
Severity: medium. Found in the 2026-11-04 review of the v1.0.1 change set (all 1.x.x merges since v1.0.0 plus the open PRs that target it), against 1.x.x 21fee6ee8e; area: #143.
Observed
Confidence: confirmed (reproduced).
- Location:
src/wallet/rpc/encrypt.cpp:44-88(LOCK(m_unlock_mutex),LOCK(m_relock_mutex),LOCK(cs_wallet), thenpwallet->Unlock(strWalletPass)with the defaultrun_pending_initial_keypool_top_up=true, thenpwallet->TopUpKeyPool());src/wallet/wallet.cpp:926-955(Unlock->RunPendingInitialKeyPoolTopUp);src/wallet/wallet.cpp:3527-3550(step loop);src/wallet/wallet.cpp:3425(TopUpKeyPoolResulttops every active manager up to-keypool), at 21fee6e. - Scenario:
createwalletwith a passphrase builds a P2MR wallet with a 16-key warm pool and a pending deferred refill. Its firstwalletpassphrasederives the remaining2 x (keypool - 16)SLH-DSA keys, which is 1,968 at the default-keypool=1000, before it returns. It holds the wallet'scs_wallet,m_relock_mutexandm_unlock_mutexthe whole time. wallet: EncryptWallet derives the new keypool before VACUUM, leaving deleted plaintext keys unvacuumed for ~30 s #285 measured about 16 ms per key (31.7 s for 2,000 keys). The same happens without a deferred refill whenever a locked wallet's P2MR pool has drained, for example to the low watermark, because the background refill does not run while the wallet is locked andTopUpKeyPool()refills to the full size inline.
While the RPC runs:- every wallet RPC on that wallet blocks;
- that wallet's
blockConnectedandtransactionAddedToMempoolcallbacks block the single scheduler thread oncs_wallet, which also stalls every other wallet's callbacks and scheduled tasks; - once more than 10 callbacks are queued,
LimitValidationInterfaceQueue(src/validation.cpp:3801-3849) makesActivateBestChainwait, so on a busy node block connection onmsghandstalls too.
The relock timer starts only after the refill, so the effective unlock time is also extended by the refill time. If an earlier unlock's relock callback fires during the refill, it blocks the scheduler thread directly onm_relock_mutex(the Background PQC key re-encryption can stall the scheduler for the length of a rescan #302 pattern).
- Evidence: code path traced (
walletpassphrase->CWallet::Unlock(pass)->RunPendingInitialKeyPoolTopUp->RunPendingInitialKeyPoolTopUpStep->DescriptorScriptPubKeyMan::TopUp, all under the RPC'sLOCK(cs_wallet)). Local probe on a Release build of 21fee6e (regtest,-p2mronly=1 -keypool=300). The firstwalletpassphraseon acreatewallet(passphrase=...)wallet took 9.76 s. A block mined 1 s into it had its validation callbacks held back:syncwithvalidationinterfacequeueblocked for 8.76 s and returned 0.1 ms after the unlock finished.getwalletinfoon the same wallet from a second connection blocked for 9.0 s (first probe).keypoolsizewent from 16 to 300 within the call. That is about 17 ms per key on this loaded host, so the default-keypool=1000gives about 33 s.
Impact
This is a node-wide stall of tens of seconds on a routine RPC, often the first unlock of a new wallet. #181's release notes say "unlock returns promptly, and a pending initial P2MR keypool refill is completed through deduplicated scheduler steps (#143)". That is true only for qbit-qt. #138's suggested direction 3 was applied to the GUI path only. LoadWalletUnlockRefillsDeferredP2MRKeypoolForEncryptedWallets asserts the inline behaviour of CWallet::Unlock(passphrase), so it is intended at the CWallet level, but nothing documents its cost for RPC users. The same pattern, a whole P2MR keypool derived while cs_wallet is held, also applies to encryptwallet/GUI encryption through SetupDescriptorScriptPubKeyMans(use_create_keypool_warmup=false) (wallet.cpp:1256). #285 covers only the VACUUM side of that, and #300 moves the VACUUM but keeps the derivation under the lock.
Suggested fix
In walletpassphrase, call Unlock(pass, /*run_pending_initial_keypool_top_up=*/false) and then MaybeSchedulePendingInitialKeyPoolTopUp(context, wallet) after releasing cs_wallet, as interfaces::Wallet::unlock does. Replace the TopUpKeyPool() call with a top-up of the non-P2MR managers only, plus MaybeScheduleP2MRKeyPoolRefill for the P2MR ones. If that is too late for v1.0.1, narrow the release-note sentence to qbit-qt and add a Known-limitations bullet: the first walletpassphrase of a wallet created with a passphrase, or an unlock after the P2MR pool drained, derives the keypool inline and can take about 30 s at -keypool=1000. During that time the wallet's RPCs and the node's validation callbacks wait.
Related: new. Related to #138, whose fix was GUI-only, and #284, which says "an encrypted wallet recovers at its first walletpassphrase" without saying what that costs. Pre-existing in v1.0.0; not a v1.0.1 regression.
Activity
doc: cover #270, #277 and #289 and the deferred review findings in th…
wallet: refill P2MR keypools in the background after walletpassphrase
wallet: deferred P2MR keypool refill at startup and unlock; Lock() wa…
kiwidream commentedon Nov 5, 2026
Fixed on
1.x.xby #299 (merged as dd86425), which makes walletpassphrase run the deferred and low-watermark P2MR refills in the background instead of inline under cs_wallet, so an RPC unlock returns in about 0.1 s instead of tens of seconds (the qbit-qt unlock gap is tracked in #355).maingets the fix when1.x.xis integrated.doc: cover #270, #277 and #289 and the deferred review findings in th…
doc: cover #270, #277 and #289 and the deferred review findings in th…
wallet: refill P2MR keypools before walletpassphrase returns
doc: list every call that waits for the walletpassphrase refill
wallet: refill P2MR keypools before walletpassphrase returns (#411)
doc: cover #270, #277 and #289 and the deferred review findings in th…
doc: cover the PRs merged for v1.0.1 since the last notes update
release: prepare v1.0.1 (#181)
wallet: deferred P2MR keypool refill at startup and unlock; Lock() wa…
wallet: refill P2MR keypools before walletpassphrase returns (#411)
release: prepare v1.0.1 (#181)