Skip to content

The agent that grows with you

test(gateway): cover TimeoutStopSec including the cron drain floor

HexLab981282362803cc
diff --git a/tests/gateway/test_cron_drain_floor.py b/tests/gateway/test_cron_drain_floor.py
index f4065ac02cf..faa97bd1284 100644
--- a/tests/gateway/test_cron_drain_floor.py
+++ b/tests/gateway/test_cron_drain_floor.py
@@ -25,6 +25,7 @@ from gateway.restart import (
DEFAULT_GATEWAY_CRON_DRAIN_TIMEOUT,
parse_cron_drain_timeout,
resolve_cron_drain_budget,
+ resolve_systemd_timeout_stop_sec,
)
from tests.gateway.restart_test_helpers import make_restart_runner
@@ -156,3 +157,40 @@ class TestResolveCronDrainBudget:
assert resolve_cron_drain_budget(
None, "30", watchdog_delay=60.0, elapsed=None
) == 30.0
+
+
+class TestResolveSystemdTimeoutStopSec:
+ """#94759: TimeoutStopSec must cover cron drain, not just chat drain."""
+
+ def test_default_cron_floor_beats_the_old_drain_only_formula(self):
+ # Old unit: max(60, 0+30) = 60. Stop path may wait 30+10=40s, then
+ # still needs the 30s teardown headroom — 70s, not 60s.
+ timeout = resolve_systemd_timeout_stop_sec(
+ 0.0, DEFAULT_GATEWAY_CRON_DRAIN_TIMEOUT
+ )
+ assert timeout == int(
+ max(
+ 60,
+ DEFAULT_GATEWAY_CRON_DRAIN_TIMEOUT + CRON_DRAIN_CLEANUP_RESERVE_S + 30,
+ )
+ )
+ assert timeout > 60
+
+ def test_configured_drain_still_extends_the_deadline_directly(self):
+ assert resolve_systemd_timeout_stop_sec(60.0, 30.0) == 90
+ assert resolve_systemd_timeout_stop_sec(180.0, 30.0) == 210
+
+ def test_larger_cron_floor_raises_timeout_stop_sec(self):
+ # 60s cron + 10s reserve + 30s headroom = 100s
+ assert resolve_systemd_timeout_stop_sec(0.0, 60.0) == 100
+
+ def test_zero_cron_floor_is_an_opt_out_not_a_hidden_default(self):
+ assert resolve_systemd_timeout_stop_sec(0.0, 0.0) == 60
+
+ def test_cron_floor_never_shortens_a_long_drain(self):
+ assert resolve_systemd_timeout_stop_sec(180.0, 30.0) == resolve_systemd_timeout_stop_sec(
+ 180.0, 0.0
+ )
+
+ def test_garbage_inputs_degrade_to_the_floor(self):
+ assert resolve_systemd_timeout_stop_sec("soon", None) == 60
diff --git a/tests/hermes_cli/test_gateway_service.py b/tests/hermes_cli/test_gateway_service.py
index 13137f03168..bd2536ca36e 100644
--- a/tests/hermes_cli/test_gateway_service.py
+++ b/tests/hermes_cli/test_gateway_service.py
@@ -14,9 +14,11 @@ grp = pytest.importorskip("grp")
import hermes_cli.gateway as gateway_cli
from gateway import status
from gateway.restart import (
+ DEFAULT_GATEWAY_CRON_DRAIN_TIMEOUT,
DEFAULT_GATEWAY_RESTART_DRAIN_TIMEOUT,
GATEWAY_FATAL_CONFIG_EXIT_CODE,
GATEWAY_SERVICE_RESTART_EXIT_CODE,
+ resolve_systemd_timeout_stop_sec,
)
@@ -184,12 +186,91 @@ class TestRequireServiceInstalled:
gateway_cli._require_service_installed("start")
+class TestGetCronDrainTimeout:
+ def test_missing_config_falls_back_to_default(self, monkeypatch):
+ monkeypatch.delenv("HERMES_CRON_DRAIN_TIMEOUT", raising=False)
+ monkeypatch.setattr(gateway_cli, "read_raw_config", lambda: {})
+ assert (
+ gateway_cli._get_cron_drain_timeout() == DEFAULT_GATEWAY_CRON_DRAIN_TIMEOUT
+ )
+
+ def test_zero_in_config_is_opt_out(self, monkeypatch):
+ monkeypatch.delenv("HERMES_CRON_DRAIN_TIMEOUT", raising=False)
+ monkeypatch.setattr(
+ gateway_cli,
+ "read_raw_config",
+ lambda: {"agent": {"cron_drain_timeout": 0}},
+ )
+ assert gateway_cli._get_cron_drain_timeout() == 0.0
+
+ def test_env_overrides_config(self, monkeypatch):
+ monkeypatch.setenv("HERMES_CRON_DRAIN_TIMEOUT", "45")
+ monkeypatch.setattr(
+ gateway_cli,
+ "read_raw_config",
+ lambda: {"agent": {"cron_drain_timeout": 90}},
+ )
+ assert gateway_cli._get_cron_drain_timeout() == 45.0
+
+
class TestGeneratedSystemdUnits:
def _expected_timeout_stop_sec(self) -> str:
- timeout = int(max(60, DEFAULT_GATEWAY_RESTART_DRAIN_TIMEOUT + 30))
+ timeout = resolve_systemd_timeout_stop_sec(
+ DEFAULT_GATEWAY_RESTART_DRAIN_TIMEOUT,
+ DEFAULT_GATEWAY_CRON_DRAIN_TIMEOUT,
+ )
return f"TimeoutStopSec={timeout}"
+ def test_timeout_stop_sec_covers_default_cron_drain_floor(self, monkeypatch):
+ """#94759: default restart_drain_timeout=0 still leaves a 30s cron
+ floor plus cleanup reserve. The old max(60, drain+30)=60 unit
+ SIGKILLed that in-budget drain."""
+ monkeypatch.delenv("HERMES_RESTART_DRAIN_TIMEOUT", raising=False)
+ monkeypatch.delenv("HERMES_CRON_DRAIN_TIMEOUT", raising=False)
+ monkeypatch.setattr(gateway_cli, "_get_restart_drain_timeout", lambda: 0.0)
+ monkeypatch.setattr(
+ gateway_cli,
+ "_get_cron_drain_timeout",
+ lambda: DEFAULT_GATEWAY_CRON_DRAIN_TIMEOUT,
+ )
+ unit = gateway_cli.generate_systemd_unit(system=False)
+ expected = resolve_systemd_timeout_stop_sec(
+ 0.0, DEFAULT_GATEWAY_CRON_DRAIN_TIMEOUT
+ )
+ assert f"TimeoutStopSec={expected}" in unit
+ assert expected > 60
+ assert self._expected_timeout_stop_sec() in unit
+
+ def test_timeout_stop_sec_follows_a_raised_cron_drain_timeout(self, monkeypatch):
+ monkeypatch.setattr(gateway_cli, "_get_restart_drain_timeout", lambda: 0.0)
+ monkeypatch.setattr(gateway_cli, "_get_cron_drain_timeout", lambda: 60.0)
+
+ unit = gateway_cli.generate_systemd_unit(system=False)
+ assert f"TimeoutStopSec={resolve_systemd_timeout_stop_sec(0.0, 60.0)}" in unit
+
+ def test_timeout_stop_sec_keeps_the_floor_when_cron_drain_is_opted_out(
+ self, monkeypatch
+ ):
+ monkeypatch.setattr(gateway_cli, "_get_restart_drain_timeout", lambda: 0.0)
+ monkeypatch.setattr(gateway_cli, "_get_cron_drain_timeout", lambda: 0.0)
+
+ unit = gateway_cli.generate_systemd_unit(system=False)
+ assert "TimeoutStopSec=60" in unit
+
+ def test_unit_stop_budget_beats_drain_only_formula_with_real_loaders(
+ self, monkeypatch
+ ):
+ monkeypatch.delenv("HERMES_RESTART_DRAIN_TIMEOUT", raising=False)
+ monkeypatch.delenv("HERMES_CRON_DRAIN_TIMEOUT", raising=False)
+ drain = gateway_cli._get_restart_drain_timeout()
+ cron = gateway_cli._get_cron_drain_timeout()
+ unit = gateway_cli.generate_systemd_unit(system=False)
+ expected = resolve_systemd_timeout_stop_sec(drain, cron)
+ assert f"TimeoutStopSec={expected}" in unit
+ old_drain_only = int(max(60, int(drain or 0) + 30))
+ if cron > 0 and drain <= 0:
+ assert expected > old_drain_only
def test_user_unit_does_not_leak_profile_node_symlink_target(self, tmp_path, monkeypatch):
# Regression for the multi-profile gateway restart-loop flap (#48700):