Skip to content

Commit c31bcaf

Browse files
committed
Merge #18458: net: Add missing cs_vNodes lock
fa36965 net: Add missing cs_vNodes lock (MarcoFalke) Pull request description: Fixes #18457 ACKs for top commit: promag: Code review ACK fa36965. laanwj: ACK fa36965 Tree-SHA512: 60d7000f2f3d480bb0953ce27a0020763e7102da16a0006b619e0a236cfc33cbd4f83d870e9f0546639711cd877c1f9808d419184bbc153bb328885417e0066c
2 parents 75021e8 + fa36965 commit c31bcaf

File tree

5 files changed

+31
-18
lines changed

5 files changed

+31
-18
lines changed

src/init.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,20 @@ void Shutdown(NodeContext& node)
197197
// Because these depend on each-other, we make sure that neither can be
198198
// using the other before destroying them.
199199
if (node.peer_logic) UnregisterValidationInterface(node.peer_logic.get());
200-
if (node.connman) node.connman->Stop();
200+
// Follow the lock order requirements:
201+
// * CheckForStaleTipAndEvictPeers locks cs_main before indirectly calling GetExtraOutboundCount
202+
// which locks cs_vNodes.
203+
// * ProcessMessage locks cs_main and g_cs_orphans before indirectly calling ForEachNode which
204+
// locks cs_vNodes.
205+
// * CConnman::Stop calls DeleteNode, which calls FinalizeNode, which locks cs_main and calls
206+
// EraseOrphansFor, which locks g_cs_orphans.
207+
//
208+
// Thus the implicit locking order requirement is: (1) cs_main, (2) g_cs_orphans, (3) cs_vNodes.
209+
if (node.connman) {
210+
node.connman->StopThreads();
211+
LOCK2(::cs_main, ::g_cs_orphans);
212+
node.connman->StopNodes();
213+
}
201214

202215
StopTorControl();
203216

src/net.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2387,7 +2387,7 @@ void CConnman::Interrupt()
23872387
}
23882388
}
23892389

2390-
void CConnman::Stop()
2390+
void CConnman::StopThreads()
23912391
{
23922392
if (threadMessageHandler.joinable())
23932393
threadMessageHandler.join();
@@ -2399,14 +2399,17 @@ void CConnman::Stop()
23992399
threadDNSAddressSeed.join();
24002400
if (threadSocketHandler.joinable())
24012401
threadSocketHandler.join();
2402+
}
24022403

2403-
if (fAddressesInitialized)
2404-
{
2404+
void CConnman::StopNodes()
2405+
{
2406+
if (fAddressesInitialized) {
24052407
DumpAddresses();
24062408
fAddressesInitialized = false;
24072409
}
24082410

24092411
// Close sockets
2412+
LOCK(cs_vNodes);
24102413
for (CNode* pnode : vNodes)
24112414
pnode->CloseSocketDisconnect();
24122415
for (ListenSocket& hListenSocket : vhListenSocket)
@@ -2415,10 +2418,10 @@ void CConnman::Stop()
24152418
LogPrintf("CloseSocket(hListenSocket) failed with error %s\n", NetworkErrorString(WSAGetLastError()));
24162419

24172420
// clean up some globals (to help leak detection)
2418-
for (CNode *pnode : vNodes) {
2421+
for (CNode* pnode : vNodes) {
24192422
DeleteNode(pnode);
24202423
}
2421-
for (CNode *pnode : vNodesDisconnected) {
2424+
for (CNode* pnode : vNodesDisconnected) {
24222425
DeleteNode(pnode);
24232426
}
24242427
vNodes.clear();
@@ -2433,7 +2436,7 @@ void CConnman::DeleteNode(CNode* pnode)
24332436
assert(pnode);
24342437
bool fUpdateConnectionTime = false;
24352438
m_msgproc->FinalizeNode(pnode->GetId(), fUpdateConnectionTime);
2436-
if(fUpdateConnectionTime) {
2439+
if (fUpdateConnectionTime) {
24372440
addrman.Connected(pnode->addr);
24382441
}
24392442
delete pnode;

src/net.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,13 @@ class CConnman
188188
~CConnman();
189189
bool Start(CScheduler& scheduler, const Options& options);
190190

191-
// TODO: Remove NO_THREAD_SAFETY_ANALYSIS. Lock cs_vNodes before reading the variable vNodes.
192-
//
193-
// When removing NO_THREAD_SAFETY_ANALYSIS be aware of the following lock order requirements:
194-
// * CheckForStaleTipAndEvictPeers locks cs_main before indirectly calling GetExtraOutboundCount
195-
// which locks cs_vNodes.
196-
// * ProcessMessage locks cs_main and g_cs_orphans before indirectly calling ForEachNode which
197-
// locks cs_vNodes.
198-
//
199-
// Thus the implicit locking order requirement is: (1) cs_main, (2) g_cs_orphans, (3) cs_vNodes.
200-
void Stop() NO_THREAD_SAFETY_ANALYSIS;
191+
void StopThreads();
192+
void StopNodes();
193+
void Stop()
194+
{
195+
StopThreads();
196+
StopNodes();
197+
};
201198

202199
void Interrupt();
203200
bool GetNetworkActive() const { return fNetworkActive; };

src/net_processing.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
class CTxMemPool;
1515

1616
extern RecursiveMutex cs_main;
17+
extern RecursiveMutex g_cs_orphans;
1718

1819
/** Default for -maxorphantx, maximum number of orphan transactions kept in memory */
1920
static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS = 100;

src/test/denialofservice_tests.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ struct COrphanTx {
5252
NodeId fromPeer;
5353
int64_t nTimeExpire;
5454
};
55-
extern RecursiveMutex g_cs_orphans;
5655
extern std::map<uint256, COrphanTx> mapOrphanTransactions GUARDED_BY(g_cs_orphans);
5756

5857
static CService ip(uint32_t i)

0 commit comments

Comments
 (0)