diff --git a/src/coinjoin/client.cpp b/src/coinjoin/client.cpp index 1675a6408e831..131a03d1b60b1 100644 --- a/src/coinjoin/client.cpp +++ b/src/coinjoin/client.cpp @@ -1895,34 +1895,41 @@ void CCoinJoinClientManager::GetJsonInfo(UniValue& obj) const } void CoinJoinWalletManager::Add(CWallet& wallet) { - m_wallet_manager_map.try_emplace( - wallet.GetName(), - std::make_unique(wallet, *this, m_dmnman, m_mn_metaman, m_mn_sync, m_queueman, m_is_masternode) - ); + { + LOCK(cs_wallet_manager_map); + m_wallet_manager_map.try_emplace( + wallet.GetName(), + std::make_unique(wallet, *this, m_dmnman, m_mn_metaman, m_mn_sync, + m_queueman, m_is_masternode) + ); + } g_wallet_init_interface.InitCoinJoinSettings(*this); } void CoinJoinWalletManager::DoMaintenance() { - for (auto& [wallet_str, walletman] : m_wallet_manager_map) { + for (auto& [wallet_str, walletman] : raw()) { walletman->DoMaintenance(m_chainstate, m_connman, m_mempool); } } void CoinJoinWalletManager::Remove(const std::string& name) { - m_wallet_manager_map.erase(name); + { + LOCK(cs_wallet_manager_map); + m_wallet_manager_map.erase(name); + } g_wallet_init_interface.InitCoinJoinSettings(*this); } void CoinJoinWalletManager::Flush(const std::string& name) { - auto clientman = Get(name); - assert(clientman != nullptr); + auto clientman = Assert(Get(name)); clientman->ResetPool(); clientman->StopMixing(); } CCoinJoinClientManager* CoinJoinWalletManager::Get(const std::string& name) const { + LOCK(cs_wallet_manager_map); auto it = m_wallet_manager_map.find(name); return (it != m_wallet_manager_map.end()) ? it->second.get() : nullptr; } diff --git a/src/coinjoin/client.h b/src/coinjoin/client.h index ba77e565cf264..e6e5046f71306 100644 --- a/src/coinjoin/client.h +++ b/src/coinjoin/client.h @@ -93,7 +93,11 @@ class CoinJoinWalletManager { CCoinJoinClientManager* Get(const std::string& name) const; - const wallet_name_cjman_map& raw() const { return m_wallet_manager_map; } + const wallet_name_cjman_map& raw() const + { + LOCK(cs_wallet_manager_map); + return m_wallet_manager_map; + } private: CChainState& m_chainstate; @@ -105,7 +109,9 @@ class CoinJoinWalletManager { const std::unique_ptr& m_queueman; const bool m_is_masternode; - wallet_name_cjman_map m_wallet_manager_map; + + mutable RecursiveMutex cs_wallet_manager_map; + wallet_name_cjman_map m_wallet_manager_map GUARDED_BY(cs_wallet_manager_map); }; class CCoinJoinClientSession : public CCoinJoinBaseSession