fix(update): wait for fleet state publication after supervised restart

A systemd unit can be active before the replacement gateway finishes
bootstrap and publishes gateway_state.json. The 30s poll in
_collect_fleet_snapshot() then returned [] with rows_expected=true,
causing _verify_fleet_after_update() to mark the restart incomplete and
exit(1) before clearing fleet_restart_pending. Every later CLI
startup/doctor therefore printed a false "did not restart" warning even
though the live gateway already served expected_sha.

Allow the default systemd startup budget plus publication slack by
extending the bounded settle window to 120s. Keeps fail-closed on
stale/down/empty after the deadline, only widens the window for slow
bootstraps (e.g. Raspberry Pi).

Fixes #111272
This commit is contained in:
joaomarcos
2026-09-14 21:47:38 -03:00
committed by Teknium
parent c91770b268
commit c99fee4e0a
2 changed files with 107 additions and 1 deletions
+6 -1
View File
@@ -28,6 +28,11 @@ _FLEET_RESTART_PENDING_NAME = "fleet_restart_pending"
_FRESH_RESTART_SUPERVISORS = frozenset({"systemd", "launchd", "service", "s6"})
# A supervisor can report a restarted unit active before the gateway finishes its
# bootstrap and publishes ``gateway_state.json``. Keep the readiness poll bounded,
# but allow the default systemd startup budget plus status-publication slack.
_FLEET_PROBE_SETTLE_TIMEOUT_SECONDS = 120.0
_SYSTEMD_SCOPES = (("user", ["systemctl", "--user"]), ("system", ["systemctl"]))
_LIST_GATEWAY_UNITS = ["list-units", "hermes-gateway*", "hermes-serve*", "--plain", "--no-legend", "--no-pager"]
@@ -1378,7 +1383,7 @@ def _collect_fleet_snapshot(restart, rows_expected: bool) -> list:
from hermes_cli.update_receipt import collect_fleet_versions
if not rows_expected:
return collect_fleet_versions(pre_restart_pids=restart.pre_restart_gateway_pids)
_fleet_deadline = _time.monotonic() + 30.0
_fleet_deadline = _time.monotonic() + _FLEET_PROBE_SETTLE_TIMEOUT_SECONDS
while True:
_time.sleep(2.0)
snapshot = collect_fleet_versions(pre_restart_pids=restart.pre_restart_gateway_pids)
@@ -0,0 +1,101 @@
"""Regression for #111272: wait for a supervised gateway to publish its state.
A systemd restart can report the unit active before the replacement gateway
writes ``gateway_state.json``. The old 30-second fleet snapshot window then
returned no rows, treated a successful restart as incomplete, and left
``fleet_restart_pending`` behind. This test uses a fake monotonic clock and a
late current row to pin the bounded readiness window without starting a live
service.
"""
from __future__ import annotations
from types import SimpleNamespace
from hermes_cli import update_cmd
import hermes_cli.update_cmd_fleet as update_cmd_fleet
from hermes_constants import get_hermes_home
class _FakeClock:
def __init__(self) -> None:
self.now = 0.0
self.sleeps: list[float] = []
def monotonic(self) -> float:
return self.now
def sleep(self, seconds: float) -> None:
self.sleeps.append(seconds)
self.now += seconds
def test_snapshot_waits_for_late_current_gateway_state(monkeypatch) -> None:
"""A current successor published after 30 seconds still completes verification."""
clock = _FakeClock()
expected = {"profile": "default", "pid": 202, "code_sha": "new", "state": "current"}
snapshots = iter([[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [expected]])
monkeypatch.setattr(update_cmd_fleet._time, "monotonic", clock.monotonic)
monkeypatch.setattr(update_cmd_fleet._time, "sleep", clock.sleep)
monkeypatch.setattr(
"hermes_cli.update_receipt.collect_fleet_versions",
lambda **_kwargs: next(snapshots),
)
restart = SimpleNamespace(pre_restart_gateway_pids=[101])
result = update_cmd_fleet._collect_fleet_snapshot(restart, rows_expected=True)
assert result == [expected]
assert clock.now > 30.0
assert clock.now <= update_cmd_fleet._FLEET_PROBE_SETTLE_TIMEOUT_SECONDS
assert len(clock.sleeps) > 15
def test_verifier_clears_marker_after_late_current_gateway_state(monkeypatch) -> None:
"""A late current row reaches the normal success-only marker cleanup."""
clock = _FakeClock()
expected = {"profile": "default", "pid": 202, "code_sha": "new", "state": "current"}
snapshots = iter([[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [expected]])
marker = get_hermes_home() / "fleet_restart_pending"
marker.write_text("started=1\npid=101\nexpected_sha=new\n", encoding="utf-8")
monkeypatch.setattr(update_cmd_fleet._time, "monotonic", clock.monotonic)
monkeypatch.setattr(update_cmd_fleet._time, "sleep", clock.sleep)
monkeypatch.setattr(
"hermes_cli.update_receipt.collect_fleet_versions",
lambda **_kwargs: next(snapshots),
)
monkeypatch.setattr(
"hermes_cli.update_receipt.print_fleet_version_matrix",
lambda _fleet: False,
)
monkeypatch.setattr(update_cmd_fleet, "_print_legacy_units_warning", lambda: None)
monkeypatch.setattr(update_cmd, "_finish_dashboard_update_cleanup", lambda *a, **k: None)
monkeypatch.setattr(update_cmd, "_surviving_pre_update_serve_runtimes", lambda _plan: [])
monkeypatch.setattr(update_cmd, "_warn_stale_serve_runtimes", lambda _rows: None)
monkeypatch.setattr(update_cmd, "_m", lambda: SimpleNamespace(_fleet_probe_expected_runtimes=lambda *a: True))
monkeypatch.setattr(update_cmd_fleet, "_clear_fleet_restart_pending_marker", lambda: marker.unlink())
restart = SimpleNamespace(
incomplete=False,
pre_restart_gateway_pids=[101],
restarted_services=["hermes-gateway.service"],
relaunched_profiles=[],
externally_supervised_profiles=[],
killed_pids=set(),
failed_or_stale_units=[],
fleet_probe_signals=lambda: ([101], set()),
)
update_cmd_fleet._verify_fleet_after_update(
restart,
_pre_update_plan=None,
_windows_gateway_resume=None,
node_failures=[],
update_complete=True,
)
assert restart.incomplete is False
assert not marker.exists()