aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2018-07-22 16:31:11 +0200
committerWladimir J. van der Laan <laanwj@gmail.com>2018-07-22 16:33:00 +0200
commita6f00ce66fe4f629ee52bff61bb4caf2c27dd970 (patch)
tree02103f1d1052a017bae907a69aeed174f5a2ada3
parent6deac922e34dc3751a8b36c053579c9df9ad9b11 (diff)
parent793290f940a9af18d4f0292a263d976a066dff65 (diff)
downloadbitcoin-a6f00ce66fe4f629ee52bff61bb4caf2c27dd970.tar.xz
Merge #13212: Net: Fixed a race condition when disabling the network.
793290f940a9af18d4f0292a263d976a066dff65 Net: Fixed a race condition when disabling the network. (lmanners) Pull request description: 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 Tree-SHA512: 6d0b7a78ae956358e796efcc034cb532c2e0d824a52ae822a3899eefc7df76327519d1f2d77362c9fddf34ef860abd095d7490d7cc02d0ba7543bf1e8c8f2535
-rw-r--r--src/net.cpp19
1 files changed, 11 insertions, 8 deletions
diff --git a/src/net.cpp b/src/net.cpp
index 34dc4aec5c..67a73fa9fc 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -1163,6 +1163,17 @@ void CConnman::ThreadSocketHandler()
//
{
LOCK(cs_vNodes);
+
+ if (!fNetworkActive) {
+ // Disconnect any connected nodes
+ for (CNode* pnode : vNodes) {
+ if (!pnode->fDisconnect) {
+ LogPrint(BCLog::NET, "Network not active, dropping peer=%d\n", pnode->GetId());
+ pnode->fDisconnect = true;
+ }
+ }
+ }
+
// Disconnect unused nodes
std::vector<CNode*> vNodesCopy = vNodes;
for (CNode* pnode : vNodesCopy)
@@ -2198,14 +2209,6 @@ void CConnman::SetNetworkActive(bool active)
fNetworkActive = active;
- if (!fNetworkActive) {
- LOCK(cs_vNodes);
- // Close sockets to all nodes
- for (CNode* pnode : vNodes) {
- pnode->CloseSocketDisconnect();
- }
- }
-
uiInterface.NotifyNetworkActiveChanged(fNetworkActive);
}