Skip to content

Commit 8f80092

Browse files
author
MarcoFalke
committed
Merge bitcoin#21563: net: Restrict period when cs_vNodes mutex is locked
8c8237a net, refactor: Fix style in CConnman::StopNodes (Hennadii Stepanov) 229ac18 net: Combine two loops into one, and update comments (Hennadii Stepanov) a3d090d net: Restrict period when cs_vNodes mutex is locked (Hennadii Stepanov) Pull request description: This PR restricts the period when the `cs_vNodes` mutex is locked, prevents the only case when `cs_vNodes` could be locked before the `::cs_main`. This change makes the explicit locking of recursive mutexes in the explicit order redundant. ACKs for top commit: jnewbery: utACK 8c8237a vasild: ACK 8c8237a ajtowns: utACK 8c8237a - logic seems sound MarcoFalke: review ACK 8c8237a 👢 Tree-SHA512: a8277924339622b188b12d260a100adf5d82781634cf974320cf6007341f946a7ff40351137c2f5369aed0d318f38aac2d32965c9b619432440d722a4e78bb73
2 parents 66fd3b2 + 8c8237a commit 8f80092

File tree

4 files changed

+15
-27
lines changed

4 files changed

+15
-27
lines changed

src/init.cpp

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

212199
StopTorControl();
213200

src/net.cpp

+14-11
Original file line numberDiff line numberDiff line change
@@ -2635,23 +2635,26 @@ void CConnman::StopNodes()
26352635
}
26362636
}
26372637

2638-
// Close sockets
2639-
LOCK(cs_vNodes);
2640-
for (CNode* pnode : vNodes)
2638+
// Delete peer connections.
2639+
std::vector<CNode*> nodes;
2640+
WITH_LOCK(cs_vNodes, nodes.swap(vNodes));
2641+
for (CNode* pnode : nodes) {
26412642
pnode->CloseSocketDisconnect();
2642-
for (ListenSocket& hListenSocket : vhListenSocket)
2643-
if (hListenSocket.socket != INVALID_SOCKET)
2644-
if (!CloseSocket(hListenSocket.socket))
2645-
LogPrintf("CloseSocket(hListenSocket) failed with error %s\n", NetworkErrorString(WSAGetLastError()));
2646-
2647-
// clean up some globals (to help leak detection)
2648-
for (CNode* pnode : vNodes) {
26492643
DeleteNode(pnode);
26502644
}
2645+
2646+
// Close listening sockets.
2647+
for (ListenSocket& hListenSocket : vhListenSocket) {
2648+
if (hListenSocket.socket != INVALID_SOCKET) {
2649+
if (!CloseSocket(hListenSocket.socket)) {
2650+
LogPrintf("CloseSocket(hListenSocket) failed with error %s\n", NetworkErrorString(WSAGetLastError()));
2651+
}
2652+
}
2653+
}
2654+
26512655
for (CNode* pnode : vNodesDisconnected) {
26522656
DeleteNode(pnode);
26532657
}
2654-
vNodes.clear();
26552658
vNodesDisconnected.clear();
26562659
vhListenSocket.clear();
26572660
semOutbound.reset();

src/test/fuzz/process_message.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ void fuzz_target(FuzzBufferType buffer, const std::string& LIMIT_TO_MESSAGE_TYPE
100100
g_setup->m_node.peerman->SendMessages(&p2p_node);
101101
}
102102
SyncWithValidationInterfaceQueue();
103-
LOCK2(::cs_main, g_cs_orphans); // See init.cpp for rationale for implicit locking order requirement
104103
g_setup->m_node.connman->StopNodes();
105104
}
106105

src/test/fuzz/process_messages.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,5 @@ FUZZ_TARGET_INIT(process_messages, initialize_process_messages)
8080
}
8181
}
8282
SyncWithValidationInterfaceQueue();
83-
LOCK2(::cs_main, g_cs_orphans); // See init.cpp for rationale for implicit locking order requirement
8483
g_setup->m_node.connman->StopNodes();
8584
}

0 commit comments

Comments
 (0)