|
47 | 47 | logger = logging.getLogger(__name__) |
48 | 48 |
|
49 | 49 |
|
50 | | -def _normalize_communicator_host(host: str) -> str: |
51 | | - """Return the bare host literal required by TCPStore/NCCL communicators.""" |
| 50 | +def _strip_ipv6_brackets(host: str) -> str: |
| 51 | + """Return an IPv6 literal without URL-only brackets.""" |
52 | 52 | if host.startswith("[") and host.endswith("]"): |
53 | 53 | return host[1:-1] |
54 | 54 | return host |
55 | 55 |
|
56 | 56 |
|
| 57 | +def _resolve_communicator_host(host: str) -> str: |
| 58 | + """Return the TCPStore/NCCL host while preserving legacy hostname resolution.""" |
| 59 | + host = _strip_ipv6_brackets(host) |
| 60 | + for family in (socket.AF_INET, socket.AF_INET6): |
| 61 | + try: |
| 62 | + socket.inet_pton(family, host) |
| 63 | + return host |
| 64 | + except OSError: |
| 65 | + pass |
| 66 | + return socket.gethostbyname(host) |
| 67 | + |
| 68 | + |
57 | 69 | def _format_http_host(host: str) -> str: |
58 | 70 | """Bracket an IPv6 literal when embedding it in an HTTP URL.""" |
59 | | - host = _normalize_communicator_host(host) |
| 71 | + host = _strip_ipv6_brackets(host) |
60 | 72 | return f"[{host}]" if ":" in host else host |
61 | 73 |
|
62 | 74 |
|
@@ -167,11 +179,11 @@ def __init__( |
167 | 179 | if base_url is not None: |
168 | 180 | # Parse the base_url to extract host and port |
169 | 181 | parsed_url = urlparse(base_url) |
170 | | - self.host = _normalize_communicator_host(parsed_url.hostname) |
| 182 | + self.host = _resolve_communicator_host(parsed_url.hostname) |
171 | 183 | scheme = parsed_url.scheme or "http" |
172 | 184 | self.base_url = f"{scheme}://{parsed_url.netloc}{parsed_url.path}" |
173 | 185 | else: |
174 | | - self.host = _normalize_communicator_host(host) |
| 186 | + self.host = _resolve_communicator_host(host) |
175 | 187 | self.server_port = server_port |
176 | 188 | self.base_url = f"http://{_format_http_host(self.host)}:{self.server_port}" |
177 | 189 | self.group_port = group_port |
|
0 commit comments