You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
wallet: walletlock does not stop an in-flight background P2MR keypool refill #286
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.
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.
Two more places where decrypted key material outlives Lock(), found during review of the #284/#287 fix (at 1.x.xb27130a7b9, 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.
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.
CWallet::Unlock(passphrase) itself is unchanged: it still keeps its master-key copy while it runs the inline create-time top-up. walletpassphrase, qbit-qt and the other callers now pass run_pending_initial_keypool_top_up=false, so the only caller left on that path is migratewallet, on the wallet instance it creates for the migration. Reopen this if that path should change too.
Closing by hand, because a merge into 1.x.x does not close issues automatically.
Activity
kiwidream commentedon Nov 4, 2026
Two more places where decrypted key material outlives
Lock(), found during review of the #284/#287 fix (at1.x.xb27130a7b9, by reading):CWallet::Unlock(const SecureString&, bool)keeps itsplain_master_keycopy (src/wallet/wallet.cpp:928) alive while it runsRunPendingInitialKeyPoolTopUp()(:951-952). Awalletlockor 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, throughGetKeys, the descriptor private keys. Background top-ups and the synchronouswalletpassphrasetop-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: haveLock()wait for them, or re-check the lock state and drop the copies.kiwidream commentedon Nov 12, 2026
Fixed on
1.x.xby #299 (dd864259ad), which ships in v1.0.1: each background keypool step (RunPendingInitialKeyPoolTopUpStep()andRunP2MRKeyPoolRefillStep()) now checksIsLocked()and registers as an active signing-provider snapshot undercs_wallet, andLock()waits for those snapshots before it clears the master key.walletlockand 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:
walletpassphrasetop-up now runs the same guarded steps (wallet: deferred P2MR keypool refill at startup and unlock; Lock() waits for key copies #299, wallet: refill P2MR keypools before walletpassphrase returns #411); its non-P2MR part runs entirely undercs_wallet.CWallet::Unlock(passphrase)itself is unchanged: it still keeps its master-key copy while it runs the inline create-time top-up.walletpassphrase, qbit-qt and the other callers now passrun_pending_initial_keypool_top_up=false, so the only caller left on that path ismigratewallet, on the wallet instance it creates for the migration. Reopen this if that path should change too.Closing by hand, because a merge into
1.x.xdoes not close issues automatically.