aboutsummaryrefslogtreecommitdiff
path: root/src/test/util/net.cpp
diff options
context:
space:
mode:
authorSebastian Falbesoner <sebastian.falbesoner@gmail.com>2024-07-04 20:12:13 +0200
committerSebastian Falbesoner <sebastian.falbesoner@gmail.com>2024-07-09 21:35:53 +0200
commit66673f1c1302c986e344c7f44bb0b352213d5dc8 (patch)
treee7ac85185907a622643216885529b7a0a836d343 /src/test/util/net.cpp
parent1c11089c7f11d3fe6e6dcf856c1330a1721048cf (diff)
downloadbitcoin-66673f1c1302c986e344c7f44bb0b352213d5dc8.tar.xz
net: fix race condition in self-connect detection
Initiating an outbound network connection currently involves the following steps after the socket connection is established (see `CConnman::OpenNetworkConnection` method): 1. set up node state 2. queue VERSION message 3. add new node to vector `m_nodes` If we connect to ourself, it can happen that the sent VERSION message (step 2) is received and processed locally *before* the node object is added to the connection manager's `m_nodes` vector (step 3). In this case, the self-connect remains undiscovered, as the detection doesn't find the outbound peer in `m_nodes` yet (see `CConnman::CheckIncomingNonce`). Fix this by swapping the order of 2. and 3., by taking the `PushNodeVersion` call out of `InitializeNode` and doing that in the `SendMessages` method instead, which is only called for `CNode` instances in `m_nodes`. Thanks go to vasild, mzumsande, dergoegge and sipa for suggestions on how to fix this.
Diffstat (limited to 'src/test/util/net.cpp')
-rw-r--r--src/test/util/net.cpp3
1 files changed, 2 insertions, 1 deletions
diff --git a/src/test/util/net.cpp b/src/test/util/net.cpp
index 9b38d85d58..e4b2ac5743 100644
--- a/src/test/util/net.cpp
+++ b/src/test/util/net.cpp
@@ -28,7 +28,8 @@ void ConnmanTestMsg::Handshake(CNode& node,
auto& connman{*this};
peerman.InitializeNode(node, local_services);
- FlushSendBuffer(node); // Drop the version message added by InitializeNode.
+ peerman.SendMessages(&node);
+ FlushSendBuffer(node); // Drop the version message added by SendMessages.
CSerializedNetMsg msg_version{
NetMsg::Make(NetMsgType::VERSION,