Skip to content

The agent that grows with you

test: cover the live-cron-writer path in the existing shutdown quiesce test

kshitijk4poorecf169f99907
diff --git a/tests/gateway/test_shutdown_executor_quiesce.py b/tests/gateway/test_shutdown_executor_quiesce.py
index 4f0233f0ecc..1ca8d304a02 100644
--- a/tests/gateway/test_shutdown_executor_quiesce.py
+++ b/tests/gateway/test_shutdown_executor_quiesce.py
@@ -213,6 +213,31 @@ async def test_stuck_worker_skips_the_session_db_close():
assert "worker_write" in events, "worker never finished"
+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).
+
+ 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.
+ 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(
+ hermes_state_registry, "close_all", lambda: events.append("close_all") or 0
+ )
+
+ 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}"
+ )
+ assert gw._executor_closing is True, "executor left unsealed on the cron-busy path"
+
+
def test_shutdown_executor_defaults_to_no_wait():
"""The no-argument call keeps the historical fire-and-forget contract."""
gw = _FakeGateway([])
diff --git a/tests/gateway/test_shutdown_writer_quiesce.py b/tests/gateway/test_shutdown_writer_quiesce.py
deleted file mode 100644
index dc18b913f28..00000000000
--- a/tests/gateway/test_shutdown_writer_quiesce.py
+++ /dev/null
@@ -1,154 +0,0 @@
-"""Tests for cron/deferred worker quiesce before closing state.db."""
-
-import asyncio
-import concurrent.futures
-import threading
-import time
-from collections import OrderedDict
-from unittest import mock
-
-import pytest
-
-import gateway.run as gw_mod
-import gateway.run_shutdown as gw_shutdown_mod
-
-
-class _FakeSessionDB:
- def __init__(self, events, name):
- self._events = events
- self._name = name
-
- def close(self):
- self._events.append(f"close:{self._name}")
-
-
-class _FakeGateway:
- def __init__(self, events):
- self._events = events
- self._running = True
- self._draining = False
- self._restart_requested = False
- self._restart_detached = False
- self._restart_via_service = False
- self._stop_task = None
- self._exit_cleanly = False
- self._exit_with_failure = False
- self._exit_reason = None
- self._exit_code = None
- self._restart_drain_timeout = 0.01
- self._running_agents = {}
- self._running_agents_ts = {}
- self._agent_cache = OrderedDict()
- self._agent_cache_lock = threading.Lock()
- self.adapters = {}
- self._background_tasks = set()
- self._failed_platforms = []
- self._shutdown_event = asyncio.Event()
- self._pending_messages = {}
- self._pending_approvals = {}
- self._busy_ack_ts = {}
- self._executor_lock = threading.Lock()
- self._executor_closing = False
- self._executor = concurrent.futures.ThreadPoolExecutor(
- max_workers=2, thread_name_prefix="quiesce-test"
- )
- self._session_db = _FakeSessionDB(events, "session_db")
- self.session_store = None
-
- # State for our tests
- self.cron_count = 0
- self.deferred_count = 0
-
- def _running_agent_count(self):
- return len(self._running_agents)
-
- def _active_cron_job_count(self):
- return self.cron_count
-
- def _active_deferred_agent_worker_count(self):
- return self.deferred_count
-
- def _active_api_run_count(self):
- return 0
-
- def _update_runtime_status(self, *_a, **_kw):
- pass
-
- def _clear_plugin_message_injector(self):
- pass
-
- async def _run_in_executor_with_context(self, func, *args):
- return func(*args)
-
- async def _cleanup_agent_resources_off_loop(self, agent, *, context=""):
- self._cleanup_agent_resources(agent)
-
- async def _notify_active_sessions_of_shutdown(self):
- pass
-
- async def _cancel_secondary_profile_reconnect_tasks(self):
- pass
-
- async def _drain_active_agents(self, timeout, cron_timeout=None):
- return {}, False
-
- async def _finalize_shutdown_agents(self, agents):
- pass
-
- def _cleanup_agent_resources(self, agent):
- pass
-
- def _evict_cached_agent(self, key):
- pass
-
- def _release_running_agent_state(self, session_key, **_kwargs):
- self._running_agents.pop(session_key, None)
- self._running_agents_ts.pop(session_key, None)
- return False
-
- def close_all_session_db_handles(self):
- pass
-
-
-async def test_stuck_cron_writer_skips_the_session_db_close():
- events = []
- gw = _FakeGateway(events)
- gw.cron_count = 1
-
- with mock.patch("gateway.run_shutdown.resolve_cron_drain_budget", return_value=0.0):
- await gw_mod.GatewayRunner.stop(gw)
-
- assert "close:session_db" not in events, "SessionDB closed despite stuck cron worker"
-
-
-async def test_stuck_deferred_worker_skips_the_session_db_close():
- events = []
- gw = _FakeGateway(events)
- gw.deferred_count = 1
-
- with mock.patch("gateway.run_shutdown.resolve_cron_drain_budget", return_value=0.0):
- await gw_mod.GatewayRunner.stop(gw)
-
- assert "close:session_db" not in events, "SessionDB closed despite stuck deferred worker"
-
-
-async def test_writers_finish_before_timeout_allows_session_db_close():
- events = []
- gw = _FakeGateway(events)
- gw.cron_count = 1
- gw.deferred_count = 1
-
- def finish_work():
- time.sleep(0.05)
- gw.cron_count = 0
- gw.deferred_count = 0
-
- threading.Thread(target=finish_work, daemon=True).start()
-
- with mock.patch("gateway.run_shutdown.resolve_cron_drain_budget", return_value=1.0):
- await gw_mod.GatewayRunner.stop(gw)
-
- assert "close:session_db" in events, "SessionDB was not closed even though writers finished"