Skip to content

Commit 793290f

Browse files
committed
Net: Fixed a race condition when disabling the network.
This change addresses a race condition where setnetworkactive=false wouldn't always disconnect all peers. Before this change, the following could happen: 1. Thread A -- Begins connecting to a node. 2. Thread B -- Sets kNetworkActive=false and disconnects connected nodes. 3. Thread A -- Finishes connecting and adds node to list of connected nodes. The node that was connected from Thread A remains connected and active, even though kNetworkActive=false. To fix the race, disconnections when kNetworkActive=false are now handled in the main network loop. fixes #13038
1 parent 1c58250 commit 793290f

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/net.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,17 @@ void CConnman::ThreadSocketHandler()
11631163
//
11641164
{
11651165
LOCK(cs_vNodes);
1166+
1167+
if (!fNetworkActive) {
1168+
// Disconnect any connected nodes
1169+
for (CNode* pnode : vNodes) {
1170+
if (!pnode->fDisconnect) {
1171+
LogPrint(BCLog::NET, "Network not active, dropping peer=%d\n", pnode->GetId());
1172+
pnode->fDisconnect = true;
1173+
}
1174+
}
1175+
}
1176+
11661177
// Disconnect unused nodes
11671178
std::vector<CNode*> vNodesCopy = vNodes;
11681179
for (CNode* pnode : vNodesCopy)
@@ -2198,14 +2209,6 @@ void CConnman::SetNetworkActive(bool active)
21982209

21992210
fNetworkActive = active;
22002211

2201-
if (!fNetworkActive) {
2202-
LOCK(cs_vNodes);
2203-
// Close sockets to all nodes
2204-
for (CNode* pnode : vNodes) {
2205-
pnode->CloseSocketDisconnect();
2206-
}
2207-
}
2208-
22092212
uiInterface.NotifyNetworkActiveChanged(fNetworkActive);
22102213
}
22112214

0 commit comments

Comments
 (0)