1 major finding · 2 minor findings · 2 suggestions
Findings
[major] · A failed transaction-update write destroys the existing wallet entry
A database write failure during an existing transaction update destroys its extracted node, leaving cached WalletTXO references dangling while the RPC caller can keep the wallet loaded.
Location: src/wallet/wallet.cpp:1113 (head).
Details and suggested fix
AddToWallet extracts the owning node before calling CWalletTx::Update. If WriteWtxVariant or WriteTxMetadata fails, Update throws and this catch returns without reinserting the node. Its destructor destroys the existing CWalletTx, unlike the previous map-owned implementation. The transaction's cached WalletTXO references and mapTxSpends txids remain, so later balance reads can access freed memory and spend-graph operations can encounter missing entries. For example, importprunedfunds can update a known inactive transaction to confirmed; a failed metadata write becomes an RPC error without unloading the wallet. Retrying is insufficient because RefreshTXOsFromTx leaves existing outpoints untouched. Restore the existing node on every failure exit, preferably with a scope guard, and test the retained transaction and cached references after an injected write failure. A failed first insertion is different: its spend records and output cache have not yet been populated, so that case need not retain the node.
[minor] · Coinbase abandonment can reuse an invalidated descendant iterator
Disconnecting a mature wallet coinbase with a multi-input descendant can reuse an invalidated worklist iterator and assert in MultiIndex safe-mode builds.
Location: src/wallet/wallet.cpp:1139 (head).
Details and suggested fix
A descendant spending two outputs of the same wallet ancestor is pushed onto this worklist twice. Processing the first copy extracts its node, invalidating the other queued iterator; reinserting the node does not make that iterator valid again. The previous traversal stored CWalletTx pointers and did not extract their owning entries. blockDisconnected reaches this branch when abandoning a wallet coinbase, so a mature coinbase with a split-and-consolidate descendant graph can trigger it after disconnecting 100 or more blocks through a deep reorg or the trusted invalidateblock RPC. MultiIndex safe mode, enabled in CI configurations including i686, asserts on the stale iterator. Ordinary builds have undefined behavior, although reuse of the same node may conceal it in practice. Queue txids with a visited set and obtain a fresh iterator for each transaction, as RecursiveUpdateTxState does. Add a regression test for this descendant graph.
[minor] · CommitTransaction dereferences a missing parent without checking
With -deprecatedrpc=removeprunedfunds, concurrent parent removal between signing and commit turns a previously checked lookup failure into an unchecked end() dereference.
Location: src/wallet/wallet.cpp:2198 (head).
Details and suggested fix
The send RPC signs through FillPSBT, which releases cs_wallet before FinishTransaction calls CommitTransaction. An authenticated caller using removeprunedfunds can erase a selected parent in that interval when the deprecated RPC is enabled. CommitTransaction takes the lock again, but AddToWallet does not restore the parent, so parent_it can equal end() here. Previously mapWallet.at() threw std::out_of_range; dereferencing the unchecked iterator instead has undefined behavior. Normal sends without concurrent removal retain their parents, and no current non-racy caller committing a complete transaction with non-wallet parents was found. Use a checked lookup that throws when the parent is absent to preserve the previous failure mode.
Optional improvements
[suggestion] · Non-key transaction updates rely on manual node restoration
Manual extraction for non-key updates makes transaction ownership depend on every path reaching reinsertion; a shared restoration helper would remove this repeated exception-safety obligation.
Location: src/wallet/wallet.cpp:1980 (head).
Details and suggested fix
State, TRUC-parent, replacement, and metadata updates now repeat extract/mutate/reinsert even when neither txid nor nOrderPos changes. An early return or escaping exception destroys a detached owning node while WalletTXO references and spend-index records remain; the AddToWallet write-error return is a concrete example. Centralize this operation in a helper with scope-based reinsertion, preserving reindexing for nOrderPos changes. In ResubmitWalletTransactions, retain the ordered candidate selection but detach and restore one transaction at a time instead of holding every candidate outside the container across submission. No current exception escaping SubmitTxMemoryPoolAndRelay was identified: rejection reaches reinsertion, and allocation failure terminates bitcoind. The rebroadcast recommendation concerns robustness, not an observed failure. Boost modify() is not a drop-in exception-safety fix because a throwing modifier erases the element.
[suggestion] · Direct index access loses the wallet lock-checking contract
The unannotated index references bypass m_txs's Clang thread-safety guard for direct accesses, although the current callers inspected still hold cs_wallet.
Location: src/wallet/wallet.h:526 (head).
Details and suggested fix
mapWallet previously carried GUARDED_BY(cs_wallet), but only m_txs has that annotation now. The recommended m_txs_by_txid and m_txs_by_pos references have no lock contract, and Clang does not infer one from their guarded underlying container. Future direct index accesses can therefore omit the lock without the previous diagnostic. GetWalletTx still requires cs_wallet, so this gap concerns direct index access rather than every lookup. Replace the public reference access paths with index accessors marked EXCLUSIVE_LOCKS_REQUIRED(cs_wallet), using const accessors for readers. That preserves the explicit-index style while restoring compile-time checking.