The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): settle who owns the Webapp's port before mounting the dashboard
emozilla6e667b17d063
diff --git a/gateway/run_api.py b/gateway/run_api.pyindex 321b63febbd..88d26836198 100644--- a/gateway/run_api.py+++ b/gateway/run_api.py@@ -195,6 +195,11 @@ async def start_gateway_api(runner, *, host: str = "127.0.0.1", port: int = 0, w_ACTIVE_RUNNER = runnertry:listener.bind(address)+ if port:+ # Under SO_REUSEADDR another socket may bind a fixed port too, and the first to listen+ # owns it. Listening now settles that before the dashboard mounts, so a lost race falls+ # back at once instead of after the mount is up.+ listener.listen(config.backlog)listener.setblocking(False)state.bound_port = listener.getsockname()[1]# Loading the ASGI graph may fail too; it owns the same bound socket.diff --git a/tests/gateway/fixtures/webapp_fallback_peer.py b/tests/gateway/fixtures/webapp_fallback_peer.pyindex 1b8cb0065bb..e0f4df3ed10 100644--- a/tests/gateway/fixtures/webapp_fallback_peer.py+++ b/tests/gateway/fixtures/webapp_fallback_peer.py@@ -8,6 +8,9 @@ Run as ``python webapp_fallback_peer.py '<spec json>'`` with an isolated ``HERME- ``fail_after_mount``: the listener's startup fails once, after the dashboard mount is up, as whenanother process starts listening on the port first;- ``hold_port``: another socket already listens on the Webapp's port;+- ``rival_listens_during_mount``: another socket bound the port with SO_REUSEADDR before the gateway+ and starts listening while the gateway mounts the dashboard (Linux only);+- ``connect_during_mount``: a client connects to the Webapp's port while the dashboard mounts;- ``built``: whether the Webapp bundle has its ``index.html``;- ``probe_host``: a Host header to send (a public hostname), or null.@@ -136,6 +139,34 @@ async def probe(spec):rival = socket.socket()rival.bind(("127.0.0.1", port))rival.listen()+ if spec.get("rival_listens_during_mount") or spec.get("connect_during_mount"):+ # Another process that bound the port with SO_REUSEADDR and starts listening while the+ # gateway mounts the dashboard (Linux lets both bind until one listens); or a client that+ # connects then.+ from gateway.run_api import GatewayDashboardMount++ if spec.get("rival_listens_during_mount"):+ rival = socket.socket()+ rival.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)+ rival.bind(("127.0.0.1", port))+ real_ensure = GatewayDashboardMount.ensure++ async def ensure(self):+ if rival is not None and "rival_listen" not in report:+ try:+ rival.listen()+ report["rival_listen"] = "listening"+ except OSError as exc:+ report["rival_listen"] = errno.errorcode.get(exc.errno, str(exc.errno))+ if spec.get("connect_during_mount") and "connect_during_mount" not in report:+ try:+ socket.create_connection(("127.0.0.1", port), timeout=5).close()+ report["connect_during_mount"] = "connected"+ except OSError as exc:+ report["connect_during_mount"] = errno.errorcode.get(exc.errno, str(exc))+ return await real_ensure(self)++ GatewayDashboardMount.ensure = ensurerunner = GatewayRunner()runner.session_control_server = GatewayControlServer(home) # verbs only; not listeningdiff --git a/tests/gateway/test_webapp_listener_fallback.py b/tests/gateway/test_webapp_listener_fallback.pyindex 21b1d5c4ae5..dad0c9e818a 100644--- a/tests/gateway/test_webapp_listener_fallback.py+++ b/tests/gateway/test_webapp_listener_fallback.py@@ -64,3 +64,24 @@ def test_the_private_listener_after_a_failed_webapp_mount_is_mains(lost_listen_rassert report["state"]["web_dist_set"] is Falseassert report["state"]["session_token_is_browser_credential"] is Falseassert "WEBAPP_BUNDLE" not in report["index"]["body"]++[email protected](sys.platform != "linux", reason="only Linux lets two SO_REUSEADDR sockets bind one address")+def test_the_webapp_owns_its_port_before_mounting_the_dashboard(tmp_path):+ # Two processes may both bind the Webapp's port under SO_REUSEADDR; the first to listen owns it.+ # The gateway listens as soon as it binds, so a rival that tries while the dashboard mounts loses.+ port = _free_port()+ spec = {"webapp": {"enabled": True, "port": port}, "rival_listens_during_mount": True}+ report = _run(tmp_path, spec)+ assert report["start_error"] is None, report.get("start_traceback")+ assert report["rival_listen"] == "EADDRINUSE"+ assert report["status"] == {"state": "serving", "url": f"http://127.0.0.1:{port}", "reason": None}+++def test_the_webapp_port_accepts_connections_before_the_dashboard_mounts(tmp_path):+ # Listening right after the bind settles who owns the port before the slow mount starts.+ port = _free_port()+ report = _run(tmp_path, {"webapp": {"enabled": True, "port": port}, "connect_during_mount": True})+ assert report["start_error"] is None, report.get("start_traceback")+ assert report["connect_during_mount"] == "connected"+ assert report["status"]["state"] == "serving"