Merge bitcoin/bitcoin#29898: test: Fix intermittent issue in p2p_handshake.py

6b02c11d66 test: Fix intermittent issue in p2p_handshake.py (stratospher)

Pull request description:

  When establishing outbound connections [`TestNode` --------> `P2PConnection`], `P2PConnection` listens for a single connection from `TestNode` on a [port which is fixed based on `p2p_idx`](312f54278f/test/functional/test_framework/p2p.py (L746)).

  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.

  ```diff
  diff --git a/src/net.cpp b/src/net.cpp
  index e388f05b03..62507d1f39 100644
  --- a/src/net.cpp
  +++ b/src/net.cpp
  @@ -2112,6 +2112,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
                   if (!pnode->fDisconnect) {
                       LogPrint(BCLog::NET, "socket closed for peer=%d\n", pnode->GetId());
                   }
  +                std::this_thread::sleep_for(std::chrono::milliseconds(1000));
                   pnode->CloseSocketDisconnect();
               }
               else if (nBytes < 0)
  ```

ACKs for top commit:
  maflcko:
    lgtm ACK 6b02c11d66
  mzumsande:
    Tested ACK 6b02c11d66
  BrandonOdiwuor:
    Tested ACK 6b02c11d66
  theStack:
    Tested ACK 6b02c11d66
  glozow:
    ACK 6b02c11d66

Tree-SHA512: 69509edb61ba45739fd585b6cc8a254f412975c124a5b5a52688288ecaaffd264dd76019b8290cc34c26c3ac2dfe477965ee5a11d7aabdd8e4d2a75229a4a068
This commit is contained in:
glozow 2024-04-22 17:06:05 +01:00
commit b3106be84f
No known key found for this signature in database
GPG Key ID: BA03F4DBE0C63FB4
1 changed files with 1 additions and 0 deletions

View File

@ -41,6 +41,7 @@ class P2PHandshakeTest(BitcoinTestFramework):
peer.sync_with_ping()
peer.peer_disconnect()
peer.wait_for_disconnect()
self.wait_until(lambda: len(node.getpeerinfo()) == 0)
def test_desirable_service_flags(self, node, service_flag_tests, desirable_service_flags, expect_disconnect):
"""Check that connecting to a peer either fails or succeeds depending on its offered