Repository navigation
Description
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-271at 21fee6e (LOCK2(m_relock_mutex, cs_wallet)now comes before theIsScanningWithPassphrase()check).rescanblockchain(src/wallet/rpc/transactions.cpp:862-871) andimportdescriptors(src/wallet/rpc/backup.cpp:1085-1092) reserve the scan withwith_passphrase=true, also on unencrypted wallets, and holdm_relock_mutexfor the whole RPC. - Scenario: an operator runs
rescanblockchain, which can take hours on mainnet, and thenencryptwallet.- v1.0.0 returned
-4"the wallet is currently being used to rescan ... Please callabortrescan" at once. - v1.0.1 blocks in
LOCK2until the rescan releasesm_relock_mutex. It then races the rescan'sWalletRescanReserverdestructor: it either encrypts the wallet or returns-4after 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) andwalletlock(:208) still fail fast, so the three RPCs now disagree.
- v1.0.0 returned
- 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 of21fee6ee8e:encryptwalletwas sent 1 s into a 10.0 simportdescriptorscall on an unencrypted blank wallet.importdescriptorsholds the samewith_passphrasereserver andm_relock_mutexasrescanblockchain. Theencryptwalletcall 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 withrescanblockchainwas inconclusive because 3,000 regtest blocks rescan in 22 ms. The v1.0.0 behaviour (-4at 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
wallet: fail encryptwallet at once while the wallet is rescanning
wallet: fail encryptwallet at once while the wallet is rescanning
wallet: fail encryptwallet at once while the wallet is rescanning
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:encryptwalletagain fails at once with-4(-15for 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.xdoes not close issues automatically.wallet: remove deleted plaintext keys from the wallet file after encr…