Skip to content

wallet: close pre-existing races around CWallet::EncryptWallet #179

Closed

Description

@kiwidream

Report

These races are in CWallet::EncryptWallet and code near it. They were found while reviewing #178 (asynchronous Encrypt Wallet for #172). They predate that PR, and it does not change wallet code. #178 does make the GUI and RPC paths overlap more easily, because the GUI no longer blocks while encryption runs.

  1. IsCrypted() is checked before LOCK2. EncryptWallet returns early if the wallet is already crypted, but it reads that state without holding cs_wallet. An encryptwallet RPC that overlaps the GUI worker during the key-derivation window can therefore pass the check twice, double-encrypt, and hit assert(false).
  2. mapMasterKeys stays populated when TxnBegin fails. If the database transaction cannot begin, the new master key has already been inserted in memory. The wallet is left reporting IsCrypted() although nothing was written.
  3. IsHDEnabled / GetActiveScriptPubKeyMans read the SPKM maps without cs_wallet. Encryption replaces those managers under the lock, so an unlocked reader can see them half-replaced. qt: keep Encrypt Wallet responsive during existing-wallet encryption #178 works around this in the GUI by skipping the HD-status query while encrypting.

Expected

  • The already-crypted check and the encryption run under one lock acquisition, so a second concurrent caller fails cleanly with "already encrypted".
  • A failed TxnBegin leaves no in-memory master key behind.
  • The SPKM accessors hold, or assert, the wallet lock.

Scope / acceptance

  • Wallet-layer fixes only. Add a unit or functional test for each item that can be reproduced deterministically; for example, the overlapping encryptwallet calls can be driven by a test hook or sync point.
  • No change to the wallet format or to encryption semantics.

Release tracking

This is not a v1.0.1 blocker unless the maintainer decides otherwise (#174). Each item needs a concurrent encrypt call, a DB error, or an unlocked reader during encryption.

Activity

  1. kiwidream commented on Sep 25, 2026

    @kiwidream
    MemberAuthor

    Added to the v1.0.1 scope in #174. Work is in progress; the PR will link back here.

  2. added a commit that references this issue on Sep 28, 2026
    7ce1208
  3. added a commit that references this issue on Nov 12, 2026
    6d6cc7d
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions