Unverified Commit 2410e320 authored by Jingru's avatar Jingru Committed by GitHub
Browse files

fix `get_ip` error in pure ipv6 environment (#2931)

parent 48a8f4a7
...@@ -162,9 +162,16 @@ def make_async(func: Callable[..., T]) -> Callable[..., Awaitable[T]]: ...@@ -162,9 +162,16 @@ def make_async(func: Callable[..., T]) -> Callable[..., Awaitable[T]]:
def get_ip() -> str: def get_ip() -> str:
# try ipv4
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80)) # Doesn't need to be reachable try:
return s.getsockname()[0] s.connect(("dns.google", 80)) # Doesn't need to be reachable
return s.getsockname()[0]
except OSError:
# try ipv6
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
s.connect(("dns.google", 80))
return s.getsockname()[0]
def get_distributed_init_method(ip: str, port: int) -> str: def get_distributed_init_method(ip: str, port: int) -> str:
...@@ -172,9 +179,16 @@ def get_distributed_init_method(ip: str, port: int) -> str: ...@@ -172,9 +179,16 @@ def get_distributed_init_method(ip: str, port: int) -> str:
def get_open_port() -> int: def get_open_port() -> int:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: # try ipv4
s.bind(("", 0)) try:
return s.getsockname()[1] with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("", 0))
return s.getsockname()[1]
except OSError:
# try ipv6
with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s:
s.bind(("", 0))
return s.getsockname()[1]
def set_cuda_visible_devices(device_ids: List[int]) -> None: def set_cuda_visible_devices(device_ids: List[int]) -> None:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment