Skip to content

The agent that grows with you

fix(gateway): a fire that waited on startup is refused when the gateway finishes starting into a drain

kshitijk4poor932d7d40d150
diff --git a/gateway/platforms/api_server_fire_startup.py b/gateway/platforms/api_server_fire_startup.py
index aff58fb99f0..d2f255f3c2b 100644
--- a/gateway/platforms/api_server_fire_startup.py
+++ b/gateway/platforms/api_server_fire_startup.py
@@ -32,9 +32,13 @@ async def refuse_until_started(runner: Any, job_id: str, *, received_at: float)
counts against the budget."""
loop = asyncio.get_running_loop()
deadline = received_at + FIRE_STARTUP_WAIT_SECONDS
- while runner is not None and not getattr(runner, "_running", True):
- # A gateway stopping mid-boot never reaches _running: refuse now, not after the full wait.
+ while runner is not None:
+ # Re-checked after every poll: the handler's drain check ran before this wait, and a gateway that
+ # starts draining meanwhile (a restart keeps _running True; a stop mid-boot never sets it) must not
+ # claim the fire.
draining = getattr(runner, "_draining", False) or getattr(runner, "_external_drain_active", False)
+ if not draining and getattr(runner, "_running", True):
+ return None
if draining or loop.time() >= deadline:
return web.json_response(
{"error": "gateway unreachable; retry", "job_id": job_id}, status=503, headers={"Retry-After": "60"})
diff --git a/tests/gateway/test_cron_fire_startup_gate.py b/tests/gateway/test_cron_fire_startup_gate.py
index 20e9413a448..d25899ed826 100644
--- a/tests/gateway/test_cron_fire_startup_gate.py
+++ b/tests/gateway/test_cron_fire_startup_gate.py
@@ -124,6 +124,25 @@ async def test_gateway_that_starts_draining_mid_boot_is_refused_without_the_full
assert provider.claimed == [] and provider.fired == []
+async def test_gateway_that_finishes_starting_into_a_drain_is_refused(adapter, provider, monkeypatch):
+ """A restart drain keeps ``_running`` True, so readiness alone must not admit a fire that waited."""
+ monkeypatch.setattr(api_server_fire_startup, "FIRE_STARTUP_WAIT_SECONDS", 30.0)
+ runner = SimpleNamespace(_draining=False, _external_drain_active=False, _running=False, adapters={})
+
+ async def _start_into_drain():
+ await asyncio.sleep(0.3)
+ runner._running = True
+ runner._draining = True
+
+ flipper = asyncio.ensure_future(_start_into_drain())
+ resp = await asyncio.wait_for(_post_fire(adapter, runner), timeout=10.0)
+ await flipper
+
+ assert resp.status == 503
+ assert provider.claimed == [] and provider.fired == []
+
+
@pytest.mark.asyncio
async def test_started_gateway_is_accepted_immediately(adapter, provider):
runner = SimpleNamespace(_draining=False, _external_drain_active=False, _running=True, adapters={"relay": object()})