Skip to content

Commit 4bc391d

Browse files
committed
fix(vllm): preserve hostname resolution for communicator
1 parent 3a9c2f7 commit 4bc391d

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

trl/generation/vllm_client.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,28 @@
4747
logger = logging.getLogger(__name__)
4848

4949

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."""
5252
if host.startswith("[") and host.endswith("]"):
5353
return host[1:-1]
5454
return host
5555

5656

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+
5769
def _format_http_host(host: str) -> str:
5870
"""Bracket an IPv6 literal when embedding it in an HTTP URL."""
59-
host = _normalize_communicator_host(host)
71+
host = _strip_ipv6_brackets(host)
6072
return f"[{host}]" if ":" in host else host
6173

6274

@@ -167,11 +179,11 @@ def __init__(
167179
if base_url is not None:
168180
# Parse the base_url to extract host and port
169181
parsed_url = urlparse(base_url)
170-
self.host = _normalize_communicator_host(parsed_url.hostname)
182+
self.host = _resolve_communicator_host(parsed_url.hostname)
171183
scheme = parsed_url.scheme or "http"
172184
self.base_url = f"{scheme}://{parsed_url.netloc}{parsed_url.path}"
173185
else:
174-
self.host = _normalize_communicator_host(host)
186+
self.host = _resolve_communicator_host(host)
175187
self.server_port = server_port
176188
self.base_url = f"http://{_format_http_host(self.host)}:{self.server_port}"
177189
self.group_port = group_port

0 commit comments

Comments
 (0)