Skip to content

wallet: walletlock does not stop an in-flight background P2MR keypool refill #286

Closed

Description

@kiwidream

Existing issue search

  • I have searched the existing issues.

Summary

walletlock (and the timed relock) does not stop a background P2MR keypool refill step that is already running. The step keeps the copy of the master key and the decrypted ECDSA keys it took in PrepareTopUp. It goes on deriving keys and writing encrypted records after Lock() has returned.

Current behaviour

On regtest (-keypool=400 -debug=walletdb), I drained the receive pool to its low watermark, so getnewaddress scheduled a refill, then called walletlock. walletlock returned, and getwalletinfo showed unlocked_until: 0. The scheduler thread then wrote 7 more encrypted key records and committed about 120 ms later. All five trials behaved the same way.

The keys are written encrypted, and the delay is bounded by one refill step. In Core, top-ups and Lock() are serialized by cs_wallet.

Expected behaviour

After Lock() returns, no thread should still be using key material derived from the master key.

Suggested fix

Do one of the following:

  • have Lock() wait for in-flight background top-up steps, as it already waits for signing operations;
  • or re-check IsLocked() under cs_desc_man before each derivation and abort the step.

Version

1.x.x at e374ba24cf; v1.0.0 has the same background workers.

Activity

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

    @kiwidream
    MemberAuthor

    Two more places where decrypted key material outlives Lock(), found during review of the #284/#287 fix (at 1.x.x b27130a7b9, by reading):

    • CWallet::Unlock(const SecureString&, bool) keeps its plain_master_key copy (src/wallet/wallet.cpp:928) alive while it runs RunPendingInitialKeyPoolTopUp() (:951-952). A walletlock or timed relock during that top-up returns while that copy still exists.
    • DescriptorScriptPubKeyMan::PrepareTopUp (src/wallet/scriptpubkeyman.cpp:1225) copies the decrypted master key and, through GetKeys, the descriptor private keys. Background top-ups and the synchronous walletpassphrase top-up run without the active-signing guard. This is the case this issue already describes.

    The fix for #287 covers the only other place a signing guard is involved (SignTransaction). These two still need the treatment suggested above: have Lock() wait for them, or re-check the lock state and drop the copies.

  3. kiwidream commented on Nov 12, 2026

    @kiwidream
    MemberAuthor

    Fixed on 1.x.x by #299 (dd864259ad), which ships in v1.0.1: each background keypool step (RunPendingInitialKeyPoolTopUpStep() and RunP2MRKeyPoolRefillStep()) now checks IsLocked() and registers as an active signing-provider snapshot under cs_wallet, and Lock() waits for those snapshots before it clears the master key. walletlock and the timed relock therefore no longer return while a step still holds decrypted key copies. Test: wallet_p2mr_deferred_keypool_tests/WalletLockWaitsForBackgroundKeyPoolTopUpStep.

    On the two places in the comment above:

    Closing by hand, because a merge into 1.x.x does not close issues automatically.

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