mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-13 11:25:02 -05:00
Merge #18458: net: Add missing cs_vNodes lock
fa369651c5
net: Add missing cs_vNodes lock (MarcoFalke) Pull request description: Fixes #18457 ACKs for top commit: promag: Code review ACKfa369651c5
. laanwj: ACKfa369651c5
Tree-SHA512: 60d7000f2f3d480bb0953ce27a0020763e7102da16a0006b619e0a236cfc33cbd4f83d870e9f0546639711cd877c1f9808d419184bbc153bb328885417e0066c
This commit is contained in:
commit
c31bcaf203
5 changed files with 31 additions and 18 deletions
15
src/init.cpp
15
src/init.cpp
|
@ -197,7 +197,20 @@ void Shutdown(NodeContext& node)
|
||||||
// Because these depend on each-other, we make sure that neither can be
|
// Because these depend on each-other, we make sure that neither can be
|
||||||
// using the other before destroying them.
|
// using the other before destroying them.
|
||||||
if (node.peer_logic) UnregisterValidationInterface(node.peer_logic.get());
|
if (node.peer_logic) UnregisterValidationInterface(node.peer_logic.get());
|
||||||
if (node.connman) node.connman->Stop();
|
// Follow the lock order requirements:
|
||||||
|
// * CheckForStaleTipAndEvictPeers locks cs_main before indirectly calling GetExtraOutboundCount
|
||||||
|
// which locks cs_vNodes.
|
||||||
|
// * ProcessMessage locks cs_main and g_cs_orphans before indirectly calling ForEachNode which
|
||||||
|
// locks cs_vNodes.
|
||||||
|
// * CConnman::Stop calls DeleteNode, which calls FinalizeNode, which locks cs_main and calls
|
||||||
|
// EraseOrphansFor, which locks g_cs_orphans.
|
||||||
|
//
|
||||||
|
// Thus the implicit locking order requirement is: (1) cs_main, (2) g_cs_orphans, (3) cs_vNodes.
|
||||||
|
if (node.connman) {
|
||||||
|
node.connman->StopThreads();
|
||||||
|
LOCK2(::cs_main, ::g_cs_orphans);
|
||||||
|
node.connman->StopNodes();
|
||||||
|
}
|
||||||
|
|
||||||
StopTorControl();
|
StopTorControl();
|
||||||
|
|
||||||
|
|
15
src/net.cpp
15
src/net.cpp
|
@ -2387,7 +2387,7 @@ void CConnman::Interrupt()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CConnman::Stop()
|
void CConnman::StopThreads()
|
||||||
{
|
{
|
||||||
if (threadMessageHandler.joinable())
|
if (threadMessageHandler.joinable())
|
||||||
threadMessageHandler.join();
|
threadMessageHandler.join();
|
||||||
|
@ -2399,14 +2399,17 @@ void CConnman::Stop()
|
||||||
threadDNSAddressSeed.join();
|
threadDNSAddressSeed.join();
|
||||||
if (threadSocketHandler.joinable())
|
if (threadSocketHandler.joinable())
|
||||||
threadSocketHandler.join();
|
threadSocketHandler.join();
|
||||||
|
}
|
||||||
|
|
||||||
if (fAddressesInitialized)
|
void CConnman::StopNodes()
|
||||||
{
|
{
|
||||||
|
if (fAddressesInitialized) {
|
||||||
DumpAddresses();
|
DumpAddresses();
|
||||||
fAddressesInitialized = false;
|
fAddressesInitialized = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close sockets
|
// Close sockets
|
||||||
|
LOCK(cs_vNodes);
|
||||||
for (CNode* pnode : vNodes)
|
for (CNode* pnode : vNodes)
|
||||||
pnode->CloseSocketDisconnect();
|
pnode->CloseSocketDisconnect();
|
||||||
for (ListenSocket& hListenSocket : vhListenSocket)
|
for (ListenSocket& hListenSocket : vhListenSocket)
|
||||||
|
@ -2415,10 +2418,10 @@ void CConnman::Stop()
|
||||||
LogPrintf("CloseSocket(hListenSocket) failed with error %s\n", NetworkErrorString(WSAGetLastError()));
|
LogPrintf("CloseSocket(hListenSocket) failed with error %s\n", NetworkErrorString(WSAGetLastError()));
|
||||||
|
|
||||||
// clean up some globals (to help leak detection)
|
// clean up some globals (to help leak detection)
|
||||||
for (CNode *pnode : vNodes) {
|
for (CNode* pnode : vNodes) {
|
||||||
DeleteNode(pnode);
|
DeleteNode(pnode);
|
||||||
}
|
}
|
||||||
for (CNode *pnode : vNodesDisconnected) {
|
for (CNode* pnode : vNodesDisconnected) {
|
||||||
DeleteNode(pnode);
|
DeleteNode(pnode);
|
||||||
}
|
}
|
||||||
vNodes.clear();
|
vNodes.clear();
|
||||||
|
@ -2433,7 +2436,7 @@ void CConnman::DeleteNode(CNode* pnode)
|
||||||
assert(pnode);
|
assert(pnode);
|
||||||
bool fUpdateConnectionTime = false;
|
bool fUpdateConnectionTime = false;
|
||||||
m_msgproc->FinalizeNode(pnode->GetId(), fUpdateConnectionTime);
|
m_msgproc->FinalizeNode(pnode->GetId(), fUpdateConnectionTime);
|
||||||
if(fUpdateConnectionTime) {
|
if (fUpdateConnectionTime) {
|
||||||
addrman.Connected(pnode->addr);
|
addrman.Connected(pnode->addr);
|
||||||
}
|
}
|
||||||
delete pnode;
|
delete pnode;
|
||||||
|
|
17
src/net.h
17
src/net.h
|
@ -188,16 +188,13 @@ public:
|
||||||
~CConnman();
|
~CConnman();
|
||||||
bool Start(CScheduler& scheduler, const Options& options);
|
bool Start(CScheduler& scheduler, const Options& options);
|
||||||
|
|
||||||
// TODO: Remove NO_THREAD_SAFETY_ANALYSIS. Lock cs_vNodes before reading the variable vNodes.
|
void StopThreads();
|
||||||
//
|
void StopNodes();
|
||||||
// When removing NO_THREAD_SAFETY_ANALYSIS be aware of the following lock order requirements:
|
void Stop()
|
||||||
// * CheckForStaleTipAndEvictPeers locks cs_main before indirectly calling GetExtraOutboundCount
|
{
|
||||||
// which locks cs_vNodes.
|
StopThreads();
|
||||||
// * ProcessMessage locks cs_main and g_cs_orphans before indirectly calling ForEachNode which
|
StopNodes();
|
||||||
// locks cs_vNodes.
|
};
|
||||||
//
|
|
||||||
// Thus the implicit locking order requirement is: (1) cs_main, (2) g_cs_orphans, (3) cs_vNodes.
|
|
||||||
void Stop() NO_THREAD_SAFETY_ANALYSIS;
|
|
||||||
|
|
||||||
void Interrupt();
|
void Interrupt();
|
||||||
bool GetNetworkActive() const { return fNetworkActive; };
|
bool GetNetworkActive() const { return fNetworkActive; };
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
class CTxMemPool;
|
class CTxMemPool;
|
||||||
|
|
||||||
extern RecursiveMutex cs_main;
|
extern RecursiveMutex cs_main;
|
||||||
|
extern RecursiveMutex g_cs_orphans;
|
||||||
|
|
||||||
/** Default for -maxorphantx, maximum number of orphan transactions kept in memory */
|
/** Default for -maxorphantx, maximum number of orphan transactions kept in memory */
|
||||||
static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS = 100;
|
static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS = 100;
|
||||||
|
|
|
@ -52,7 +52,6 @@ struct COrphanTx {
|
||||||
NodeId fromPeer;
|
NodeId fromPeer;
|
||||||
int64_t nTimeExpire;
|
int64_t nTimeExpire;
|
||||||
};
|
};
|
||||||
extern RecursiveMutex g_cs_orphans;
|
|
||||||
extern std::map<uint256, COrphanTx> mapOrphanTransactions GUARDED_BY(g_cs_orphans);
|
extern std::map<uint256, COrphanTx> mapOrphanTransactions GUARDED_BY(g_cs_orphans);
|
||||||
|
|
||||||
static CService ip(uint32_t i)
|
static CService ip(uint32_t i)
|
||||||
|
|
Loading…
Add table
Reference in a new issue