Merge bitcoin/bitcoin#34186: test: use dynamic port allocation in proxy tests

ce63d37ebe test: use dynamic port allocation to avoid test conflicts (woltx)

Pull request description:

  Use `port=0` for dynamic port allocation in test framework components to avoid intermittent "address already in use" errors when running tests concurrently or when ports are stuck in TIME_WAIT state. Example: https://github.com/bitcoin/bitcoin/pull/29415#discussion_r2634509304

  Changes:

    - Update `socks5.py` and `p2p.py` to support dynamic port allocation
    - Convert `feature_proxy.py` and `feature_anchors.py` to use `port=0`

ACKs for top commit:
  achow101:
    ACK ce63d37ebe
  vasild:
    ACK ce63d37ebe
  mzumsande:
    re-ACK ce63d37ebe

Tree-SHA512: 4efcedca3bde209fbd1bdc2a4ae04b7d53515587d86e421ce61064f78c675c71b45d9782b514c5e7cfc0e92842c947d49f7a3fddb03fe619fcdec9b565f0ecbd
This commit is contained in:
Ava Chow
2026-01-14 14:40:10 -08:00
4 changed files with 16 additions and 11 deletions

View File

@@ -787,13 +787,14 @@ class NetworkThread(threading.Thread):
cls.protos[(addr, port)] = None
return response
if (addr, port) not in cls.listeners:
if port == 0 or (addr, port) not in cls.listeners:
# When creating a listener on a given (addr, port) we only need to
# do it once. If we want different behaviors for different
# connections, we can accomplish this by providing different
# `proto` functions
listener = await cls.network_event_loop.create_server(peer_protocol, addr, port)
port = listener.sockets[0].getsockname()[1]
logger.debug("Listening server on %s:%d should be started" % (addr, port))
cls.listeners[(addr, port)] = listener

View File

@@ -203,6 +203,9 @@ class Socks5Server():
self.s = socket.socket(conf.af)
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.s.bind(conf.addr)
# When port=0, the OS assigns an available port. Update conf.addr
# to reflect the actual bound address so callers can use it.
self.conf.addr = self.s.getsockname()
self.s.listen(5)
self.running = False
self.thread = None