Skip to content

Review finding: walletpassphrase runs the deferred P2MR refill inline under cs_wallet #304

Closed

Description

@kiwidream

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), then pwallet->Unlock(strWalletPass) with the default run_pending_initial_keypool_top_up=true, then pwallet->TopUpKeyPool()); src/wallet/wallet.cpp:926-955 (Unlock -> RunPendingInitialKeyPoolTopUp); src/wallet/wallet.cpp:3527-3550 (step loop); src/wallet/wallet.cpp:3425 (TopUpKeyPoolResult tops every active manager up to -keypool), at 21fee6e.
  • Scenario: createwallet with a passphrase builds a P2MR wallet with a 16-key warm pool and a pending deferred refill. Its first walletpassphrase derives the remaining 2 x (keypool - 16) SLH-DSA keys, which is 1,968 at the default -keypool=1000, before it returns. It holds the wallet's cs_wallet, m_relock_mutex and m_unlock_mutex the 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 and TopUpKeyPool() refills to the full size inline.
    While the RPC runs:
    • every wallet RPC on that wallet blocks;
    • that wallet's blockConnected and transactionAddedToMempool callbacks block the single scheduler thread on cs_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) makes ActivateBestChain wait, so on a busy node block connection on msghand stalls 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 on m_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's LOCK(cs_wallet)). Local probe on a Release build of 21fee6e (regtest, -p2mronly=1 -keypool=300). The first walletpassphrase on a createwallet(passphrase=...) wallet took 9.76 s. A block mined 1 s into it had its validation callbacks held back: syncwithvalidationinterfacequeue blocked for 8.76 s and returned 0.1 ms after the unlock finished. getwalletinfo on the same wallet from a second connection blocked for 9.0 s (first probe). keypoolsize went from 16 to 300 within the call. That is about 17 ms per key on this loaded host, so the default -keypool=1000 gives 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

  1. added
    bug
    on Nov 4, 2026
  2. kiwidream commented on Nov 5, 2026

    @kiwidream
    MemberAuthor

    Fixed on 1.x.x by #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). main gets the fix when 1.x.x is integrated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions