Skip to content

Commit f13a22a

Browse files
promagryanofsky
andcommitted
wallet: Call load handlers without cs_wallet locked
Co-authored-by: Russell Yanofsky <[email protected]>
1 parent 47fe744 commit f13a22a

File tree

3 files changed

+5
-9
lines changed

3 files changed

+5
-9
lines changed

src/wallet/context.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ using LoadWalletFn = std::function<void(std::unique_ptr<interfaces::Wallet> wall
3434
struct WalletContext {
3535
interfaces::Chain* chain{nullptr};
3636
ArgsManager* args{nullptr}; // Currently a raw pointer because the memory is not managed by this struct
37+
// It is unsafe to lock this after locking a CWallet::cs_wallet mutex because
38+
// this could introduce inconsistent lock ordering and cause deadlocks.
3739
Mutex wallets_mutex;
3840
std::vector<std::shared_ptr<CWallet>> wallets GUARDED_BY(wallets_mutex);
3941
std::list<LoadWalletFn> wallet_load_fns GUARDED_BY(wallets_mutex);

src/wallet/test/wallet_tests.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -783,18 +783,14 @@ BOOST_FIXTURE_TEST_CASE(CreateWallet, TestChain100Setup)
783783
// deadlock during the sync and simulates a new block notification happening
784784
// as soon as possible.
785785
addtx_count = 0;
786-
auto handler = HandleLoadWallet(context, [&](std::unique_ptr<interfaces::Wallet> wallet) EXCLUSIVE_LOCKS_REQUIRED(wallet->wallet()->cs_wallet, context.wallets_mutex) {
786+
auto handler = HandleLoadWallet(context, [&](std::unique_ptr<interfaces::Wallet> wallet) {
787787
BOOST_CHECK(rescan_completed);
788788
m_coinbase_txns.push_back(CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
789789
block_tx = TestSimpleSpend(*m_coinbase_txns[2], 0, coinbaseKey, GetScriptForRawPubKey(key.GetPubKey()));
790790
m_coinbase_txns.push_back(CreateAndProcessBlock({block_tx}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
791791
mempool_tx = TestSimpleSpend(*m_coinbase_txns[3], 0, coinbaseKey, GetScriptForRawPubKey(key.GetPubKey()));
792792
BOOST_CHECK(m_node.chain->broadcastTransaction(MakeTransactionRef(mempool_tx), DEFAULT_TRANSACTION_MAXFEE, false, error));
793-
LEAVE_CRITICAL_SECTION(context.wallets_mutex);
794-
LEAVE_CRITICAL_SECTION(wallet->wallet()->cs_wallet);
795793
SyncWithValidationInterfaceQueue();
796-
ENTER_CRITICAL_SECTION(wallet->wallet()->cs_wallet);
797-
ENTER_CRITICAL_SECTION(context.wallets_mutex);
798794
});
799795
wallet = TestLoadWallet(context);
800796
BOOST_CHECK_EQUAL(addtx_count, 4);

src/wallet/wallet.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2759,8 +2759,6 @@ std::shared_ptr<CWallet> CWallet::Create(WalletContext& context, const std::stri
27592759
// Try to top up keypool. No-op if the wallet is locked.
27602760
walletInstance->TopUpKeyPool();
27612761

2762-
LOCK(walletInstance->cs_wallet);
2763-
27642762
if (chain && !AttachChain(walletInstance, *chain, rescan_required, error, warnings)) {
27652763
return nullptr;
27662764
}
@@ -2772,9 +2770,9 @@ std::shared_ptr<CWallet> CWallet::Create(WalletContext& context, const std::stri
27722770
}
27732771
}
27742772

2775-
walletInstance->SetBroadcastTransactions(args.GetBoolArg("-walletbroadcast", DEFAULT_WALLETBROADCAST));
2776-
27772773
{
2774+
LOCK(walletInstance->cs_wallet);
2775+
walletInstance->SetBroadcastTransactions(args.GetBoolArg("-walletbroadcast", DEFAULT_WALLETBROADCAST));
27782776
walletInstance->WalletLogPrintf("setKeyPool.size() = %u\n", walletInstance->GetKeyPoolSize());
27792777
walletInstance->WalletLogPrintf("mapWallet.size() = %u\n", walletInstance->mapWallet.size());
27802778
walletInstance->WalletLogPrintf("m_address_book.size() = %u\n", walletInstance->m_address_book.size());

0 commit comments

Comments
 (0)