Skip to content

The agent that grows with you

fix(gateway): the shutdown close gate also counts API-server runs

kshitijk4poor7d3050811d9f
diff --git a/gateway/run_shutdown.py b/gateway/run_shutdown.py
index 2a3f5550174..ce8714ea7fe 100644
--- a/gateway/run_shutdown.py
+++ b/gateway/run_shutdown.py
@@ -1906,17 +1906,18 @@ class GatewayShutdownMixin:
"open for SQLite to recover on next open", _exec_live, _exec_quiesce_budget,
)
return
- # Cron jobs run on the scheduler's own pool, not self._executor, so the join above never sees
- # them. A writer that outlived the cron drain is mid-write for the same #101093 reasons; the
- # drain already spent its budget, so no second wait — leave the handles open (#102198).
- _cron_live = self._active_cron_job_count()
- if _cron_live:
+ logger.info("Shutdown phase: executor quiesced at +%.2fs", ctx.elapsed())
+ # Cron jobs run on the scheduler's own pool and API-server runs on the loop's default executor,
+ # not self._executor, so the join above never sees them. A writer that outlived the drain is
+ # mid-write for the same #101093 reasons; the drain already spent its budget, so no second wait —
+ # leave the handles open (#102198).
+ _outside_live = self._active_cron_job_count() + self._active_api_run_count()
+ if _outside_live:
logger.warning(
- "Shutdown phase: %d cron job(s) still running after the cron drain — skipping the SessionDB "
- "close/checkpoint, leaving state.db open for the cron writer (#102198)", _cron_live,
+ "Shutdown phase: %d cron/API-server job(s) still running after the drain — skipping the "
+ "SessionDB close/checkpoint, leaving state.db open for the live writer (#102198)", _outside_live,
)
return
- logger.info("Shutdown phase: executor quiesced at +%.2fs", ctx.elapsed())
_step = GatewayShutdownMixin._quiet_step
# Close SQLite session DBs so --replace's new gateway does not hit 'database is locked'.
# ``_session_db`` is an AsyncSessionDB facade — unwrap; ``session_store`` holds ``_db``.
diff --git a/tests/gateway/test_shutdown_executor_quiesce.py b/tests/gateway/test_shutdown_executor_quiesce.py
index 1ca8d304a02..225a19a10c9 100644
--- a/tests/gateway/test_shutdown_executor_quiesce.py
+++ b/tests/gateway/test_shutdown_executor_quiesce.py
@@ -214,18 +214,19 @@ async def test_stuck_worker_skips_the_session_db_close():
@pytest.mark.asyncio
-async def test_stuck_cron_writer_skips_the_session_db_close(monkeypatch):
- """A cron job that outlived the cron drain must not have state.db closed under it (#102198).
[email protected]("counter", ["_active_cron_job_count", "_active_api_run_count"])
+async def test_live_writer_outside_the_executor_skips_the_session_db_close(monkeypatch, counter):
+ """A cron job or API-server run that outlived the drain must not have state.db closed under it (#102198).
- Cron jobs run on the scheduler's own pool, so the executor join above the
- close block never sees them; the close has to consult the cron count too.
+ Both run outside ``self._executor`` (scheduler pool / loop default executor), so the executor
+ join above the close block never sees them; the close has to consult their counters too.
The executor must still be sealed on this path (#101118).
"""
import hermes_state_registry
events = []
gw = _FakeGateway(events)
- monkeypatch.setattr(gw, "_active_cron_job_count", lambda: 1)
+ monkeypatch.setattr(gw, counter, lambda: 1)
monkeypatch.setattr(
hermes_state_registry, "close_all", lambda: events.append("close_all") or 0
)
@@ -233,9 +234,9 @@ async def test_stuck_cron_writer_skips_the_session_db_close(monkeypatch):
await gw_mod.GatewayRunner.stop(gw)
assert "close:session_db" not in events and "close_all" not in events, (
- f"SessionDB closed despite a live cron writer: {events}"
+ f"SessionDB closed despite a live {counter} writer: {events}"
)
- assert gw._executor_closing is True, "executor left unsealed on the cron-busy path"
+ assert gw._executor_closing is True, "executor left unsealed on the outside-writer path"
def test_shutdown_executor_defaults_to_no_wait():