Skip to content

The agent that grows with you

fix(gateway): keep serving native clients when the Webapp's sign-in gate refuses

emozilla2a6febe314a8
diff --git a/gateway/run_runtime.py b/gateway/run_runtime.py
index d2c3cb74691..a09c5cb6c73 100644
--- a/gateway/run_runtime.py
+++ b/gateway/run_runtime.py
@@ -505,14 +505,17 @@ async def start_gateway_runtime_api(runner):
# Without the Webapp the listener is main's: the same call, a private ephemeral port.
runner.session_api = await (run_api.start_gateway_api(runner, host=host, port=port, webapp=webapp)
if webapp is not None else run_api.start_gateway_api(runner))
- except Exception as exc:
+ except (Exception, SystemExit) as exc:
if webapp is None:
raise
- # The Webapp's listener could not start (port taken, address unavailable, a wildcard bind
- # with no dashboard sign-in provider): native clients still need this gateway, so it keeps
- # main's private listener and reports why the Webapp is not served.
- cause = (exc.strerror or exc) if isinstance(exc, OSError) else exc
- reason = f"could not listen on {host}:{port} ({cause})"
+ # The Webapp's listener could not start (port taken, address unavailable, or the dashboard's
+ # auth gate exiting on a wildcard bind with no sign-in provider): native clients still need
+ # this gateway, so it keeps main's private listener and reports why the Webapp is not served.
+ if isinstance(exc, SystemExit):
+ reason = str(exc.code).strip() # the gate's own message names the fix
+ else:
+ cause = (exc.strerror or exc) if isinstance(exc, OSError) else exc
+ reason = f"could not listen on {host}:{port} ({cause})"
logger.error("Webapp unavailable: %s", reason)
runner.webapp_status = {"state": "unavailable", "url": None, "reason": reason}
runner.session_api = await run_api.start_gateway_api(runner)
diff --git a/tests/gateway/test_webapp_listener.py b/tests/gateway/test_webapp_listener.py
index 47f1bd332fa..944b9d32d77 100644
--- a/tests/gateway/test_webapp_listener.py
+++ b/tests/gateway/test_webapp_listener.py
@@ -213,11 +213,21 @@ def test_a_launch_link_refuses_a_malformed_profile_name(monkeypatch, tmp_path, p
assert refused["ok"] is False and "profile" in refused["error"]
-def test_a_webapp_listener_that_cannot_start_still_leaves_native_clients_a_listener(monkeypatch, tmp_path):
- refusal = RuntimeError("A non-loopback listener needs a dashboard sign-in provider")
[email protected]("refusal", [
+ # The dashboard's auth gate refuses a gated bind with no sign-in provider by exiting.
+ SystemExit("Refusing to bind dashboard to 0.0.0.0 — the auth gate engages on non-loopback binds "
+ "(0.0.0.0), but no auth providers are registered."),
+ RuntimeError("gateway API lifespan startup failed"),
+])
+def test_a_webapp_listener_that_cannot_start_still_leaves_native_clients_a_listener(monkeypatch, tmp_path, refusal):
runner, calls, _ = _start_runtime_api(
monkeypatch, tmp_path, "webapp:\n enabled: true\n host: 0.0.0.0\n", refuse=refusal)
assert [(host, port) for host, port, _ in calls] == [("0.0.0.0", webapp.DEFAULT_PORT), ("127.0.0.1", 0)]
assert runner.webapp_status["state"] == "unavailable"
- assert "sign-in provider" in runner.webapp_status["reason"]
+ assert str(refusal) in runner.webapp_status["reason"]
assert runner.session_runtime_descriptor["api_origin"] == "http://127.0.0.1:50001"
+
+
+def test_cancelling_the_gateway_while_the_webapp_listener_starts_cancels_it(monkeypatch, tmp_path):
+ with pytest.raises(asyncio.CancelledError):
+ _start_runtime_api(monkeypatch, tmp_path, "webapp:\n enabled: true\n", refuse=asyncio.CancelledError())