Skip to content

The agent that grows with you

fix(gateway): keep an active drain when the last platform is parked mid-run

unsupportedpastelsad016629a902
diff --git a/gateway/run_startup.py b/gateway/run_startup.py
index 632ca4298e3..e0f8460c0b2 100644
--- a/gateway/run_startup.py
+++ b/gateway/run_startup.py
@@ -105,7 +105,9 @@ class GatewayStartupMixin:
if adapter.fatal_error_retryable or is_global_startup_conflict(adapter.fatal_error_code):
return False
self._startup_parked_platforms = True
- self._update_runtime_status(self._serving_state())
+ # An active drain or shutdown owns the state; the drain release stamps _serving_state() itself.
+ if not (self._external_drain_active or self._draining or not self._running):
+ self._update_runtime_status(self._serving_state())
logger.error(
"%s is parked after a non-retryable error (fix it, then `hermes gateway restart`). The gateway "
"is DEGRADED — it serves local clients and cron with no messaging platform. %s: %s",
diff --git a/tests/gateway/test_platform_shared_metrics.py b/tests/gateway/test_platform_shared_metrics.py
index 637f1dba895..c61f7996486 100644
--- a/tests/gateway/test_platform_shared_metrics.py
+++ b/tests/gateway/test_platform_shared_metrics.py
@@ -174,6 +174,7 @@ def test_a_delivery_sent_after_the_routed_scope_resets_lands_in_the_owning_profi
class _FatalRunner(GatewayAdapterLifecycleMixin, GatewayStartupMixin):
# The last non-retryable fatal parks the platform through the startup mixin (as in GatewayRunner).
+ _draining = _external_drain_active = False
def __init__(self):
self.adapters, self._failed_platforms, self._running = {}, {}, True
diff --git a/tests/gateway/test_startup_failed_platform_reports_degraded.py b/tests/gateway/test_startup_failed_platform_reports_degraded.py
index b3726587ca2..c1cbdd23421 100644
--- a/tests/gateway/test_startup_failed_platform_reports_degraded.py
+++ b/tests/gateway/test_startup_failed_platform_reports_degraded.py
@@ -7,6 +7,7 @@ status looked healthy, every API client was gone, nothing retried. The startup s
gate — every platform, not just api_server — so the same failure on any other platform is reported
the same way.
"""
+import asyncio
import logging
import time
from unittest.mock import AsyncMock
@@ -190,6 +191,41 @@ async def test_last_platform_lost_non_retryably_mid_run_keeps_the_gateway_up_deg
await real_stop()
+async def test_last_platform_parked_during_an_external_drain_keeps_draining_until_release(
+ monkeypatch, tmp_path
+):
+ """Parking must not overwrite an active drain: status stays ``draining`` while the dashboard's
+ drain holds, and the release reports ``degraded`` (not ``running``)."""
+ drain = {"requested": True}
+ monkeypatch.setattr("gateway.drain_control.drain_requested", lambda: drain["requested"])
+ adapter = _LostMidRunAdapter()
+ runner = _runner(
+ monkeypatch, tmp_path, {Platform.TELEGRAM: PlatformConfig(enabled=True, token="***")},
+ lambda platform, platform_config: adapter,
+ )
+ assert await runner.start() is True
+
+ async def _drain_watcher_settles(active):
+ for _ in range(50): # the watcher polls the marker every second
+ if runner._external_drain_active is active:
+ return
+ await asyncio.sleep(0.1)
+ raise AssertionError(f"external drain never became {active}")
+
+ try:
+ await _drain_watcher_settles(True)
+ await adapter.fail("telegram_auth_error")
+ assert await flush_runtime_status_async()
+ assert read_runtime_status()["gateway_state"] == "draining"
+ drain["requested"] = False
+ await _drain_watcher_settles(False)
+ assert await flush_runtime_status_async()
+ assert read_runtime_status()["gateway_state"] == "degraded"
+ finally:
+ await runner.stop()
+
+
@pytest.mark.asyncio
async def test_mid_run_ownership_conflict_on_the_last_platform_still_stops(monkeypatch, tmp_path):
"""Protection: another gateway owning the token mid-run keeps today's clean stop."""