Skip to content

Review finding: encryptwallet waits for a running rescan instead of failing with -4 #317

Closed

Description

@kiwidream

Summary

encryptwallet now waits for a running rescan instead of failing with -4, so its "call abortrescan" error is effectively unreachable.

Severity: low. 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.

Observed

Confidence: confirmed (wait-then-encrypt reproduced via importdescriptors; rescanblockchain shares the reserver and lock pattern by code).

  • Location: src/wallet/rpc/encrypt.cpp:263-271 at 21fee6e (LOCK2(m_relock_mutex, cs_wallet) now comes before the IsScanningWithPassphrase() check). rescanblockchain (src/wallet/rpc/transactions.cpp:862-871) and importdescriptors (src/wallet/rpc/backup.cpp:1085-1092) reserve the scan with with_passphrase=true, also on unencrypted wallets, and hold m_relock_mutex for the whole RPC.
  • Scenario: an operator runs rescanblockchain, which can take hours on mainnet, and then encryptwallet.
    • v1.0.0 returned -4 "the wallet is currently being used to rescan ... Please call abortrescan" at once.
    • v1.0.1 blocks in LOCK2 until the rescan releases m_relock_mutex. It then races the rescan's WalletRescanReserver destructor: it either encrypts the wallet or returns -4 after the full wait.
      A client whose -rpcclienttimeout (default 900 s) is shorter than the rescan sees a transport timeout, and the server then encrypts the wallet anyway, so the operator does not know the outcome. walletpassphrasechange (encrypt.cpp:145) and walletlock (:208) still fail fast, so the three RPCs now disagree.
  • Evidence: diff of wallet: close races around CWallet::EncryptWallet #183 (git show 7ce1208c6c -- src/wallet/rpc/encrypt.cpp); lock scopes traced. Local probe on a Release build of 21fee6ee8e: encryptwallet was sent 1 s into a 10.0 s importdescriptors call on an unencrypted blank wallet. importdescriptors holds the same with_passphrase reserver and m_relock_mutex as rescanblockchain. The encryptwallet call did not return -4. It blocked for 9.36 s, then encrypted the wallet, returning 0.34 s after the import finished. A first attempt with rescanblockchain was inconclusive because 3,000 regtest blocks rescan in 22 ms. The v1.0.0 behaviour (-4 at once) is established from the diff; v1.0.0 was not run.

Impact

This is an RPC error contract that scripts parse. The #181 note "Other encryptwallet failures keep their previous error codes (#183)" is not accurate for this case. It is low severity because it needs an overlapping rescan, and the passphrase used is still the one the caller sent.

Suggested fix

Before LOCK2, add a fast check of IsScanningWithPassphrase(), and of IsCrypted(), returning the old codes. Keep the authoritative IsCrypted() re-check under the lock that #183 added, so the overlapping-encryption case still returns -15. Alternatively, TRY_LOCK(m_relock_mutex) and return -4 when a scan holds it. Add a functional case that starts a rescan and calls encryptwallet, and correct the release-note sentence.

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: encryptwallet again fails at once with -4 (-15 for an encrypted wallet) while a rescan or descriptor import is running, instead of waiting and then encrypting.

    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