From 70ba08db906b07b428846fda45bed10c49b41f40 Mon Sep 17 00:00:00 2001 From: Jaeyeong Choi <144660874+Jaeyeong-CHOI@users.noreply.github.com> Date: Thu, 12 Nov 2026 14:17:24 +0900 Subject: [PATCH] fix(python): bracket IPv6 hosts in managed server URLs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `ManagedServer` built its readiness URL and its OpenAI base URL as `http://{host}:{port}/…`. With an IPv6 literal such as `::1` that is `http://::1:8080/health`, which httpx rejects (`InvalidURL: Invalid port`), so `LLM(model, host="::1")` failed during startup although the server was given a valid `--host`. Both URLs now come from one origin that brackets a raw IPv6 literal. The host passed to the server command line is unchanged, and an already bracketed literal, an IPv4 address, a hostname and a Unix socket are formatted as before. --- python/src/mlxcel/_server.py | 19 +++++++--- python/tests/fake_server.py | 10 ++++- python/tests/test_lifecycle.py | 68 ++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 7 deletions(-) diff --git a/python/src/mlxcel/_server.py b/python/src/mlxcel/_server.py index 700db3657..00d2a2f16 100644 --- a/python/src/mlxcel/_server.py +++ b/python/src/mlxcel/_server.py @@ -153,6 +153,17 @@ def __init__( ) self.uds_path = path + @property + def _http_origin(self) -> str: + """HTTP origin, bracketing IPv6 literals without altering the bind host.""" + if self.uds_path is not None: + return "http://mlxcel" + assert self.host is not None and self.port is not None + host = self.host + if ":" in host and not host.startswith("["): + host = f"[{host}]" + return f"http://{host}:{self.port}" + @property def base_url(self) -> str: """OpenAI base URL (with the ``/v1`` suffix) for this transport. @@ -160,15 +171,11 @@ def base_url(self) -> str: For a Unix socket the host portion is a placeholder; the actual routing happens through the httpx ``uds`` transport. """ - if self.uds_path is not None: - return "http://mlxcel/v1" - return f"http://{self.host}:{self.port}/v1" + return f"{self._http_origin}/v1" @property def _health_url(self) -> str: - if self.uds_path is not None: - return "http://mlxcel/health" - return f"http://{self.host}:{self.port}/health" + return f"{self._http_origin}/health" def _build_command(self) -> List[str]: """Build the ``mlxcel serve`` argv. diff --git a/python/tests/fake_server.py b/python/tests/fake_server.py index 863ac88a9..9e2266a84 100644 --- a/python/tests/fake_server.py +++ b/python/tests/fake_server.py @@ -19,6 +19,7 @@ import argparse import json import os +import socket import sys import time from http.server import BaseHTTPRequestHandler @@ -202,6 +203,10 @@ class ThreadingTCPServer(ThreadingMixIn, TCPServer): allow_reuse_address = True +class ThreadingIPv6Server(ThreadingTCPServer): + address_family = socket.AF_INET6 + + def main() -> None: global READY_AFTER, MODEL_ID @@ -250,8 +255,11 @@ def main() -> None: sys.stderr.write(f"Starting mlxcel server on unix:{uds}\n") else: host = args.host or "127.0.0.1" + if host.startswith("[") and host.endswith("]"): + host = host[1:-1] port = args.port or 8080 - server = ThreadingTCPServer((host, port), Handler) + server_type = ThreadingIPv6Server if ":" in host else ThreadingTCPServer + server = server_type((host, port), Handler) sys.stderr.write(f"Starting mlxcel server on {host}:{port}\n") sys.stderr.flush() diff --git a/python/tests/test_lifecycle.py b/python/tests/test_lifecycle.py index 069a7358a..d272038d4 100644 --- a/python/tests/test_lifecycle.py +++ b/python/tests/test_lifecycle.py @@ -7,8 +7,10 @@ from __future__ import annotations +import asyncio import logging import os +import socket import stat import sys import time @@ -102,6 +104,72 @@ def test_managed_tcp_lifecycle(fake_binary: str) -> None: assert server._proc is not None and server._proc.poll() is not None +@pytest.mark.parametrize( + "host", ["127.0.0.1", "localhost", "::1", "[::1]", "2001:db8::1", "[2001:db8::1]"] +) +def test_managed_tcp_urls_preserve_host_and_port(host: str) -> None: + server = ManagedServer("fake/model", binary=sys.executable, host=host, port=18080) + try: + for raw_url, path in [(server.base_url, "/v1"), (server._health_url, "/health")]: + url = httpx.URL(raw_url) + assert url.host == host.strip("[]") + assert url.port == 18080 + assert url.path == path + # URL brackets must not change the address passed to the server CLI. + command = server._build_command() + assert command[command.index("--host") + 1] == host + finally: + server.close() + + +@pytest.fixture() +def ipv6_port() -> int: + if not socket.has_ipv6: + pytest.skip("IPv6 unavailable") + try: + with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as probe: + probe.bind(("::1", 0)) + return int(probe.getsockname()[1]) + except OSError as exc: + pytest.skip(f"IPv6 loopback unavailable: {exc}") + + +@pytest.mark.parametrize("host", ["::1", "[::1]"]) +def test_managed_ipv6_client( + fake_binary: str, ipv6_port: int, host: str, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setenv("NO_PROXY", "*") + with mlxcel.LLM( + "fake/model", binary=fake_binary, host=host, port=ipv6_port, startup_timeout=10.0 + ) as client: + assert client.model == "fake-model" + assert client.generate("hi") == "hello world" + assert client._server is not None + proc = client._server._proc + assert proc is not None and proc.poll() is None + assert proc.poll() is not None + + +@pytest.mark.parametrize("host", ["::1", "[::1]"]) +def test_async_managed_ipv6_client( + fake_binary: str, ipv6_port: int, host: str, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setenv("NO_PROXY", "*") + + async def go() -> None: + async with mlxcel.AsyncLLM( + "fake/model", binary=fake_binary, host=host, port=ipv6_port, startup_timeout=10.0 + ) as client: + assert await client.generate("hi") == "hello world" + assert client.model == "fake-model" + assert client._server is not None + proc = client._server._proc + assert proc is not None and proc.poll() is None + assert proc.poll() is not None + + asyncio.run(go()) + + def test_early_exit_raises_with_stderr(fake_binary: str, tmp_path: Path) -> None: sock = _short_socket(tmp_path) server = ManagedServer(