When establishing outbound connections [TestNode
——–> P2PConnection
], P2PConnection
listens for a single connection from TestNode
on a port which is fixed based on p2p_idx
.
If we reuse the same port when disconnecting and establishing connections again, we might hit this scenario where:
- disconnection is done on python side for
P2PConnection
- disconnection not complete on c++ side for
TestNode
- we’re trying to establish a new connection on same port again
Prevent this scenario from happening by ensuring disconnection on c++ side for TestNode as well.
One way to reproduce this on master would be adding a sleep statement before disconnection happens on c++ side.
0diff --git a/src/net.cpp b/src/net.cpp
1index e388f05b03..62507d1f39 100644
2--- a/src/net.cpp
3+++ b/src/net.cpp
4@@ -2112,6 +2112,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
5 if (!pnode->fDisconnect) {
6 LogPrint(BCLog::NET, "socket closed for peer=%d\n", pnode->GetId());
7 }
8+ std::this_thread::sleep_for(std::chrono::milliseconds(1000));
9 pnode->CloseSocketDisconnect();
10 }
11 else if (nBytes < 0)