Skip to content

Review finding: an exception after EncryptWallet commits leaves the wallet unlocked and reports failure #305

Closed

Description

@kiwidream

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-1262 at 21fee6e (after TxnCommit: ClearMasterKey(); Unlock(passphrase, false); SetupDescriptorScriptPubKeyMans(...); ClearMasterKey(); GetDatabase().Rewrite();, with no try/catch or scope guard). The RPC is src/wallet/rpc/encrypt.cpp:288-290.
  • Scenario: encryptwallet (RPC) commits the encryption, then SetupDescriptorScriptPubKeyMans spends about 30 s writing about 2,000 encrypted PQC keys in one transaction. If anything fails there, it throws. That includes TopUpWithDB with throw_on_persistence_error=true on a write failure such as a full disk, a failed TxnBegin ("cannot process db transaction for descriptors setup"), and "Could not encrypt new descriptors". The exception skips both ClearMasterKey() calls and Rewrite(). The RPC returns -1 with the exception text, although:
    1. the wallet is encrypted on disk with the caller's passphrase;
    2. vMasterKey is still set, so the wallet stays unlocked until restart or walletlock, and no relock is scheduled. getwalletinfo reports unlocked_until: 0, which its help text defines as "locked";
    3. 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);
    4. the "setup descriptors" transaction is aborted by ~WalletBatch, but the managers it already added stay in m_spk_managers and 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 following getnewaddress hands 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

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

    @kiwidream
    MemberAuthor

    Fixed on 1.x.x by #300 (ea571b86f5), which ships in v1.0.1: a failure after EncryptWallet'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.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