Skip to content

Commit

Permalink
Honor global ipv6 setting when finding free ports
Browse files Browse the repository at this point in the history
  • Loading branch information
danijar committed Sep 14, 2024
1 parent 0a7312d commit f8e0cab
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion portal/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '3.2.1'
__version__ = '3.2.2'

import multiprocessing as mp
try:
Expand Down
10 changes: 8 additions & 2 deletions portal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import psutil

from . import contextlib


def run(workers, duration=None):
[None if x.started else x.start() for x in workers]
Expand Down Expand Up @@ -96,9 +98,13 @@ def free_port():
# Return a port that is currently free. This function is not thread or
# process safe, because there is no way to guarantee that the port will still
# be free at the time it will be used.
sock = socket.socket()
if contextlib.context.serverkw.get('ipv6', False):
family, addr = socket.AF_INET6, ('localhost', 0, 0, 0)
else:
family, addr = socket.AF_INET, ('localhost', 0)
sock = socket.socket(family, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('localhost', 0))
sock.bind(addr)
port = sock.getsockname()[1]
sock.close()
return port
Expand Down

0 comments on commit f8e0cab

Please sign in to comment.