Repository navigation
Description
Existing issue search
- I have searched the existing issues.
Summary
Deferred and scheduled P2MR descriptor top-up can mutate CWallet::m_cached_spks without holding cs_wallet. The cache update also occurs before the surrounding SQLite transaction commits.
This permits concurrent unsynchronized access to the wallet's script cache and can publish in-memory ownership state that is not yet durable.
Current behaviour
DescriptorScriptPubKeyMan::TopUpWithInternalHintResult():
- Acquires
cs_desc_man. - Begins a wallet database transaction.
- Calls
TopUpWithDBPreparedResult(). - Commits the transaction after that function returns.
Before returning, TopUpWithDBPreparedResult() calls:
m_storage.TopUpCallback(new_spks, this);
NotifyCanGetAddressesChanged();CWallet::TopUpCallback() calls CacheNewScriptPubKeys(), which modifies the m_cached_spks unordered map without acquiring cs_wallet.
Historically, many top-up callers already held cs_wallet. The following newer paths explicitly release it before descriptor top-up:
CWallet::RunPendingInitialKeyPoolTopUpStep()CWallet::RunP2MRKeyPoolRefillStep()
Other wallet threads can therefore read m_cached_spks under cs_wallet while a top-up thread modifies the map without that lock. The update also occurs before TxnCommit(), so commit failure can leave cache state inconsistent with persisted descriptor state.
Impact
Potential consequences include:
- concurrent unordered-map access and undefined behaviour;
- incorrect or inconsistent wallet ownership lookup;
- cache entries referring to descriptor state that did not commit; and
- a new lock inversion if the problem is addressed by acquiring
cs_walletinsideTopUpCallback()while still holdingcs_desc_manand the SQLite writer.
Expected behaviour
Every read and mutation of m_cached_spks should follow one explicit synchronization contract. New script ownership should not be published until its descriptor and key material have committed successfully.
Commit failure must not leave newly generated scripts visible in the wallet cache.
Scope boundary
#138 tracks the Qt notification deadlock and synchronous unlock-time top-up. This issue does not duplicate those behaviours. It tracks the separate unsynchronized mutation and pre-commit publication of CWallet::m_cached_spks.
A fix for #138 that only queues NotifyCanGetAddressesChanged does not resolve this cache race.
Suggested direction
Stage the newly generated script set during descriptor top-up, then publish it using a synchronization design that guarantees:
- The database transaction committed successfully.
- No thread can read
m_cached_spksconcurrently with a mutation. - Publication does not require acquiring
cs_walletin descriptor/database-to-wallet order. - Wallet transaction processing cannot permanently miss a committed script during the handoff.
Possible implementations include returning staged cache updates to the wallet layer or protecting the cache with a dedicated synchronization mechanism. The exact design should preserve the canonical wallet, descriptor, and database lock order.
Steps to reproduce in a deterministic test
- Start a deferred or scheduled P2MR descriptor top-up without
cs_walletheld. - Pause immediately before
TopUpCallback()publishes the new scripts. - Concurrently call
IsMine(),GetScriptPubKeyMans(), or block transaction processing undercs_wallet. - Release the top-up and assert both operations finish without a race or deadlock.
- Repeat with an injected
TxnCommit()failure.
Run the concurrency case under ThreadSanitizer and debug lock-order checking.
Acceptance criteria
m_cached_spksis never mutated concurrently with a reader.- Cache publication occurs only after successful persistence.
- Commit failure does not publish new script-cache entries.
- The fix does not acquire
cs_walletwhile holdingcs_desc_manor the SQLite writer. - Deferred initial and low-watermark P2MR top-ups remain functional.
- Concurrent block processing, RPC wallet access, and descriptor top-up complete without deadlock.
- Tests cover successful commit, failed commit, and concurrent cache access.
Relevant code
src/wallet/scriptpubkeyman.cpp:TopUpWithInternalHintResult,TopUpWithDBPreparedResultsrc/wallet/wallet.cpp:RunPendingInitialKeyPoolTopUpStep,RunP2MRKeyPoolRefillStep,CacheNewScriptPubKeys,TopUpCallbacksrc/wallet/wallet.h:m_cached_spks
Version and platform
Source-inspected on qbit v1.0.0. The issue is platform-independent.
Activity
kiwidream commentedon Sep 18, 2026
Fixed on
1.x.xby #144 (mergeb1902dc50d), which did not auto-close this because it targets a non-default branch. Ata2efbc19d6:m_cached_spksisGUARDED_BY(cs_wallet)andCWallet::TopUpCallbacklocks it; descriptor top-up stages the new scripts, commits the database transaction, then publishes with no descriptor or database lock held (TopUpWithInternalHintResultNoNotify); commit failure cancels the publication and rolls back memory. Covered by theDescriptorTopUp*cases inwallet_tests(commit success, commit failure, concurrent readers, concurrent transaction processing) andwallet_p2mr_deferred_keypool_tests; the TSan and sanitizer legs of Full Validation are green on this head. Closing.