Skip to content

Commit 7fe8f0d

Browse files
committed
Probe IPv6 first
1 parent 1afbd61 commit 7fe8f0d

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

tests/durabletask/_port_utils.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,20 @@ def find_free_port() -> int:
2121
releases it and returns the chosen number. There is an inherent race
2222
between releasing the socket and the caller binding the port, but in
2323
practice it is reliable for tests and far safer than fixed ports.
24+
25+
The in-memory backend binds the IPv6 wildcard (``[::]``), so the probe
26+
uses an IPv6 socket to mirror that bind and avoid returning a port that
27+
is free on IPv4 but already in use on IPv6. If IPv6 is unavailable, it
28+
falls back to IPv4.
2429
"""
30+
if socket.has_ipv6:
31+
try:
32+
with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s:
33+
s.bind(("::", 0))
34+
return s.getsockname()[1]
35+
except OSError:
36+
pass # IPv6 not usable on this host; fall back to IPv4.
37+
2538
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
26-
s.bind(("localhost", 0))
39+
s.bind(("127.0.0.1", 0))
2740
return s.getsockname()[1]

0 commit comments

Comments
 (0)