Repository navigation
Description
Summary
An exception after EncryptWallet's commit is reported as a failure, leaves the wallet unlocked with no relock, and skips the rewrite.
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: #183, #277.
Observed
Confidence: plausible (proven from code that the exception skips ClearMasterKey; the trigger needs an I/O failure).
- Location:
src/wallet/wallet.cpp:1248-1262at 21fee6e (afterTxnCommit:ClearMasterKey(); Unlock(passphrase, false); SetupDescriptorScriptPubKeyMans(...); ClearMasterKey(); GetDatabase().Rewrite();, with no try/catch or scope guard). The RPC issrc/wallet/rpc/encrypt.cpp:288-290. - Scenario:
encryptwallet(RPC) commits the encryption, thenSetupDescriptorScriptPubKeyMansspends about 30 s writing about 2,000 encrypted PQC keys in one transaction. If anything fails there, it throws. That includesTopUpWithDBwiththrow_on_persistence_error=trueon a write failure such as a full disk, a failedTxnBegin("cannot process db transaction for descriptors setup"), and "Could not encrypt new descriptors". The exception skips bothClearMasterKey()calls andRewrite(). The RPC returns-1with the exception text, although:- the wallet is encrypted on disk with the caller's passphrase;
vMasterKeyis still set, so the wallet stays unlocked until restart orwalletlock, and no relock is scheduled.getwalletinforeportsunlocked_until: 0, which its help text defines as "locked";- the plaintext key records the commit deleted stay in free pages, because the VACUUM never runs (the wallet: EncryptWallet derives the new keypool before VACUUM, leaving deleted plaintext keys unvacuumed for ~30 s #285 effect, made permanent with no retry on 1.x.x);
- the "setup descriptors" transaction is aborted by
~WalletBatch, but the managers it already added stay inm_spk_managersand the active maps in memory. Their top-ups are rolled back by the listener, and their master keys and active records are not on disk. A followinggetnewaddresshands out addresses from such a manager, whose self-committing top-up writes the descriptor, cache and PQC keys but not the master key or the active record. After a restart the old seed is active again and that manager loads as inactive, without its xprv.
qbit-qt is not affected in the same way, because qt: keep Encrypt Wallet responsive during existing-wallet encryption #178 stops the application on an exception from the worker.
- Evidence: code read. wallet: close races around CWallet::EncryptWallet #183's commit message says "Post-commit failures are unchanged". wallet: remove deleted plaintext keys from the wallet file after encryption #300 (open) moves
Rewrite()before the setup and adds a retry at load, which fixes item 3 only. Not reproduced: a write failure that does not abort the SQLite transaction is hard to inject from a functional test.
Impact
This is an outcome reported as failed when it succeeded on the call whose job is to protect the keys. The decrypted master key stays resident with no timeout while monitoring reports the wallet as locked In-memory active descriptors diverge from disk The failure window is the about 30 s of P2MR derivation. Bitcoin Core has the same structure, but its EC derivation takes 1-2 s.
Suggested fix
After TxnCommit succeeds, use a scope guard that always runs ClearMasterKey() and, with #300, the post-encryption rewrite. Catch exceptions from the new-seed setup and return a distinct result, for example "wallet encrypted, but generating the new HD seed failed; the wallet is locked; restart and back it up", instead of a generic -1. Alternatively, abort as qbit-qt does. Roll back the in-memory AddScriptPubKeyMan and active-map changes of an aborted "setup descriptors" transaction, for example through an on_abort listener.
Related: new. #285 and #300 cover only the rewrite, #296 covers ECDSA write failures inside Encrypt (before the commit), and #179/#183 explicitly excluded post-commit failures.
Activity
wallet: keep EncryptWallet's outcome when a step after its commit fails
wallet: keep EncryptWallet's outcome when a step after its commit fails
wallet: keep EncryptWallet's outcome when a step after its commit fails
wallet: remove deleted plaintext keys from the wallet file after encr…
kiwidream commentedon Nov 11, 2026
Fixed on
1.x.xby #300 (ea571b86f5), which ships in v1.0.1: a failure afterEncryptWallet's commit now leaves the wallet locked, removes the aborted seed's descriptors from memory, and is reported as "encrypted, with a warning" instead of-1. qbit-qt keeps its existing behaviour for this case: it logs the error and quits.Closing by hand, because a merge into
1.x.xdoes not close issues automatically.wallet: remove deleted plaintext keys from the wallet file after encr…