Skip to content

The agent that grows with you

fix(gateway): stay up degraded when every platform fails non-retryably at start

unsupportedpastels5c0370c97585
diff --git a/gateway/run_startup.py b/gateway/run_startup.py
index 3a72869114e..69d1babbd7d 100644
--- a/gateway/run_startup.py
+++ b/gateway/run_startup.py
@@ -92,6 +92,8 @@ class GatewayStartupMixin:
# A configured platform failed non-retryably this boot and is parked: every "we are serving"
# status stamp (startup, drain release, scale-to-zero wake) must say ``degraded``, not ``running``.
_startup_parked_platforms: bool = False
+ # A startup platform failure was a live foreign token/profile holder (exit 78 when nothing else runs).
+ _startup_ownership_conflict: bool = False
def _serving_state(self) -> str:
return "degraded" if self._startup_parked_platforms else "running"
@@ -1363,7 +1365,10 @@ class GatewayStartupMixin:
continue
# A live foreign token holder is an ownership conflict, not a blip: retryable only for
# MID-RUN reconnects; at startup route it non-retryable so the gateway exits 78, not deaf.
- _retryable = adapter.fatal_error_retryable and not is_global_startup_conflict(adapter.fatal_error_code)
+ _conflict = is_global_startup_conflict(adapter.fatal_error_code)
+ if _conflict:
+ self._startup_ownership_conflict = True
+ _retryable = adapter.fatal_error_retryable and not _conflict
self._update_platform_runtime_status(
platform.value, platform_state="retrying" if _retryable else "fatal",
error_code=adapter.fatal_error_code, error_message=adapter.fatal_error_message,
@@ -1447,11 +1452,23 @@ class GatewayStartupMixin:
len(startup_nonretryable_errors), "; ".join(startup_nonretryable_errors),
)
return False
- if startup_nonretryable_errors and not startup_retryable_errors:
+ if startup_nonretryable_errors and not startup_retryable_errors and self._startup_ownership_conflict:
+ # Another gateway owns a token/profile: staying up would be a second, deaf owner.
reason = "; ".join(startup_nonretryable_errors)
logger.error("Gateway hit a non-retryable startup conflict: %s", reason)
self._startup_fail_fatal_config(reason)
return True
+ if startup_nonretryable_errors and not startup_retryable_errors:
+ # Every platform is parked, but local clients and cron still need this gateway: exiting 78
+ # would take them all down over one unusable bot. Stay up degraded, like the mixed case.
+ self._startup_parked_platforms = True
+ logger.error(
+ "%d configured platform(s) failed to start and are parked (fix the reported error, then "
+ "`hermes gateway restart`): %s. The gateway is DEGRADED — it serves local clients and "
+ "cron with no messaging platform.",
+ len(startup_nonretryable_errors), "; ".join(startup_nonretryable_errors),
+ )
+ return False
if startup_nonretryable_errors:
# Mixed (some fatal, some transient): exiting 78 would take the gateway PERMANENTLY down
# over a blip. Log the fatal side loudly and fall through to the degraded/retry path.
@@ -1661,6 +1678,7 @@ class GatewayStartupMixin:
startup_nonretryable_errors: list[str] = []
startup_retryable_errors: list[str] = []
self._startup_parked_platforms = False # fresh boot: no platform has failed yet
+ self._startup_ownership_conflict = False
(
_aborted, enabled_platform_count, _multiplex_skipped_platforms, _pending_connects
) = await self._start_prefilter_platforms()
diff --git a/tests/gateway/test_startup_failed_platform_reports_degraded.py b/tests/gateway/test_startup_failed_platform_reports_degraded.py
index f876e19ed67..b706a708ecc 100644
--- a/tests/gateway/test_startup_failed_platform_reports_degraded.py
+++ b/tests/gateway/test_startup_failed_platform_reports_degraded.py
@@ -115,6 +115,33 @@ async def test_parked_platform_is_logged_at_error_and_the_run_is_degraded(monkey
await runner.stop()
+async def test_sole_parked_platform_keeps_the_gateway_up_degraded(monkeypatch, tmp_path, caplog):
+ """Every enabled platform failing non-retryably (no ownership conflict) must not exit 78: local
+ clients and cron need the gateway, so the platform is parked and the run reports ``degraded``."""
+ runner = _runner(
+ monkeypatch, tmp_path,
+ {Platform.API_SERVER: PlatformConfig(enabled=True, extra={"port": 8642})},
+ lambda platform, platform_config: _PortTakenAdapter(),
+ )
+
+ with caplog.at_level(logging.ERROR):
+ ok = await runner.start()
+ try:
+ assert ok is True
+ assert (runner.should_exit_cleanly, runner.exit_code) == (False, None)
+ assert any(
+ record.levelno >= logging.ERROR and "api_server" in record.getMessage()
+ for record in caplog.records
+ )
+ state = read_runtime_status()
+ assert state["gateway_state"] == "degraded"
+ assert state["platforms"]["api_server"]["state"] == "fatal"
+ assert state["platforms"]["api_server"]["error_code"] == "api_server_port_in_use"
+ finally:
+ await runner.stop()
+
+
@pytest.mark.asyncio
async def test_every_platform_connected_still_reports_a_normal_run(monkeypatch, tmp_path, caplog):
"""Protection: a clean startup keeps the normal ``running`` state and logs no ERROR."""