The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): a Webapp bound to :: takes IPv4 clients too, on Windows as on Linux
emozilladac5a228f22e
diff --git a/gateway/run_api.py b/gateway/run_api.pyindex 576c13d01ca..390723b404a 100644--- a/gateway/run_api.py+++ b/gateway/run_api.py@@ -186,10 +186,16 @@ class GatewayDashboardMount:await web.app(scope, receive, send)-def _listener_socket(family, kind, proto, port: int) -> socket.socket:+def _listener_socket(family, kind, proto, port: int, *, dual_stack: bool = False) -> socket.socket:"""The listener's unbound socket; a fixed *port* may bind again at once (``_REUSE_FIXED_PORT``)- or is held exclusively (``_EXCLUSIVE_FIXED_PORT``)."""+ or is held exclusively (``_EXCLUSIVE_FIXED_PORT``). A *dual_stack* IPv6 socket (the ``::``+ wildcard) takes IPv4 connections too."""listener = socket.socket(family, kind, proto)+ if dual_stack:+ # "::" means every address, as Linux binds it by default. Windows binds IPv6 only otherwise:+ # IPv4 clients would never reach the Webapp, and the exclusive hold below would cover the+ # IPv6 port alone, leaving 0.0.0.0 on the same port to another program.+ listener.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)if port and _REUSE_FIXED_PORT:listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)elif port and _EXCLUSIVE_FIXED_PORT:@@ -238,7 +244,8 @@ async def start_gateway_api(runner, *, host: str = "127.0.0.1", port: int = 0, wfamily, kind, proto, _, address = socket.getaddrinfo(host, port, type=socket.SOCK_STREAM,)[0]- listener = _listener_socket(family, kind, proto, port)+ listener = _listener_socket(family, kind, proto, port,+ dual_stack=family == socket.AF_INET6 and address[0] == "::")_ACTIVE_RUNNER = runnertry:listener.bind(address)diff --git a/tests/gateway/test_webapp_listener.py b/tests/gateway/test_webapp_listener.pyindex e7ed8468f25..0d0f4ea2279 100644--- a/tests/gateway/test_webapp_listener.py+++ b/tests/gateway/test_webapp_listener.py@@ -389,6 +389,40 @@ def test_on_windows_the_webapps_fixed_port_never_lets_another_process_bind_it():assert _reuse_address(0) is False # main's private listener+def test_a_dual_stack_listener_takes_ipv4_too():+ """The ``::`` wildcard means every address, IPv4 included, on every platform: Windows otherwise+ binds IPv6 only, so IPv4 clients (a LAN browser at http://192.168.x.y:<port>) never reach the+ Webapp, and its exclusive hold on the port leaves 0.0.0.0 free for another program."""+ import socket++ from gateway import run_api+ with run_api._listener_socket(socket.AF_INET6, socket.SOCK_STREAM, 0, webapp.DEFAULT_PORT,+ dual_stack=True) as wildcard:+ assert wildcard.getsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY) == 0++[email protected](("host", "dual_stack"), [("::", True), ("::1", False), ("127.0.0.1", False)])+def test_the_webapp_listener_is_dual_stack_only_on_the_ipv6_wildcard(monkeypatch, host, dual_stack):+ """Only a ``::`` bind takes IPv4 as well; ``::1`` stays the IPv6 loopback address it names."""+ from gateway import run_api++ asked = []++ class Stop(Exception):+ pass++ def listener_socket(family, kind, proto, port, **options):+ asked.append(options.get("dual_stack", False))+ raise Stop++ monkeypatch.setattr(run_api, "_listener_socket", listener_socket)+ with pytest.raises(Stop):+ asyncio.run(run_api.start_gateway_api(SimpleNamespace(), host=host, port=webapp.DEFAULT_PORT,+ webapp=webapp.WebappMount(dist=None, home=None)))+ assert asked == [dual_stack]+++def test_a_fixed_port_binds_again_while_the_last_gateways_connections_linger():# The server closing first leaves its side of the connection in TIME_WAIT on the fixed port.import socket