Skip to content

The agent that grows with you

fix(gateway): enable the migration survivor before any unit is removed (#124922, salvage #124943)

teknium127f7c9226d11
diff --git a/hermes_cli/gateway_migrate.py b/hermes_cli/gateway_migrate.py
index e045671a72e..d97355d982e 100644
--- a/hermes_cli/gateway_migrate.py
+++ b/hermes_cli/gateway_migrate.py
@@ -347,19 +347,14 @@ def _service_op(kind: str, system: bool, verb: str, home: Path, *, run_as_user:
gw.systemd_install(system=system, run_as_user=run_as_user, non_interactive=True)
return
if verb == "enable":
- # Boot enablement of the survivor. The migration's uninstall of every secondary just
- # destroyed their boot enablement (systemd: disable + default.target.wants unlink), so
- # the survivor's is now load-bearing for the whole host — and `restart` on a disabled
- # unit succeeds, so without this the host reboots with no gateway at all while the
- # migration reports success (#124922). Best-effort with a loud failure line: failing
- # the apply here would trigger a rollback that cannot do any better.
- if kind == "launchd":
- # A plist in ~/Library/LaunchAgents is loaded at login (RunAtLoad); the migration
- # never removes the survivor's plist, so its boot-start artifact is intact.
- return
- if kind == "windows":
- # The survivor's Scheduled Task is its own enablement; the migration never removes
- # it, and reinstalling to re-flip start-on-login would escalate UAC mid-migration.
+ # Boot enablement only: the migration's uninstall of every secondary destroys their
+ # boot enablement (systemd: disable + default.target.wants unlink), so the survivor's is
+ # load-bearing for the whole host — and `restart` on a disabled unit succeeds, so a
+ # reboot came up with no gateway while the migration reported success (#124922).
+ # launchd (RunAtLoad plist) and the Windows logon task are their own enablement and the
+ # migration never removes the survivor's. Best-effort with a loud line: failing here
+ # would trigger a rollback that cannot do any better.
+ if kind != "systemd":
return
enabled = gw._run_systemctl(["enable", gw.get_service_name()], system=system, check=False, timeout=30)
if getattr(enabled, "returncode", 0) != 0:
@@ -880,22 +875,18 @@ def _restart_default(
) -> str:
"""Bring the default gateway up on the new flag value; returns a one-line description.
- The survivor is also (re-)enabled at boot: the migration just made its enablement
- load-bearing by uninstalling every secondary unit — ``systemd_uninstall`` disables them, and
- the survivor's own unit may have been disabled all along (a host that predates enable-on-boot
- installs). ``restart`` alone on a disabled unit succeeds and reports nothing, which is how a
- reboot then came up with NO gateway while the migration said ✓ (#124922).
+ Boot enablement is NOT this function's job: an existing survivor unit is enabled by
+ :func:`_enable_default_at_boot` before the first removal, and the install branch enables
+ through ``systemd_install``'s own enable-on-startup.
"""
if plan_default.service is not None:
kind, system = plan_default.service
_service_op(kind, system, "restart", default_home)
- _service_op(kind, system, "enable", default_home)
return f"restarted the default gateway via {kind}"
if target is not None:
kind, system = target
_service_op(kind, system, "install", default_home, run_as_user=run_as_user)
_service_op(kind, system, "start", default_home)
- _service_op(kind, system, "enable", default_home)
return f"installed and started the default gateway via {kind}"
verb = "restarted" if plan_default.pid is not None else "started"
if plan_default.pid is not None:
@@ -905,6 +896,19 @@ def _restart_default(
return f"{verb} the default gateway (detached; no service manager was in use)"
+def _enable_default_at_boot(plan: MigrationPlan) -> None:
+ """Make the survivor boot-startable BEFORE anything is removed. Every secondary unit the fold
+ uninstalls takes its boot enablement with it, and the pre-existing default unit may have been
+ disabled all along; ``restart`` on a disabled unit succeeds, so the old order (remove, then
+ restart) turned N boot-startable gateways into 0 and reported success (#124922). ``enable``
+ never starts the unit, so doing it first cannot double-bind a platform while secondaries run,
+ and an apply interrupted anywhere after it still leaves a host that comes back after a reboot.
+ """
+ if plan.default.service is not None:
+ kind, system = plan.default.service
+ _service_op(kind, system, "enable", plan.default_home)
+
+
def _remove_secondary_gateways(plan: MigrationPlan) -> None:
for p in plan.standalone_secondaries:
for kind, system in p.services:
@@ -1029,6 +1033,7 @@ def apply_migration(plan: MigrationPlan, *, served_wait: float = _SERVED_WAIT_SE
try:
_write_multiplex_flag(plan.default_home, True)
print(f" ✓ default: gateway.multiplex_profiles: true ({plan.default_home / 'config.yaml'})")
+ _enable_default_at_boot(plan)
_remove_secondary_gateways(plan) # on resume: whatever an apply killed mid-removal left installed
print(f" ✓ {_restart_default(plan.default, target, plan.default_home, run_as_user=run_as_user)}")
except Exception as exc:
diff --git a/tests/hermes_cli/test_gateway_migrate_multiplex.py b/tests/hermes_cli/test_gateway_migrate_multiplex.py
index e4513fe3738..a317291ab55 100644
--- a/tests/hermes_cli/test_gateway_migrate_multiplex.py
+++ b/tests/hermes_cli/test_gateway_migrate_multiplex.py
@@ -41,6 +41,9 @@ def fleet(tmp_path, monkeypatch):
pids={"coder": 4101, "ops": 4102},
ops=[],
refused_at_start={},
+ # (profile, kind, system) of units whose boot enablement survived; secondaries boot-start,
+ # the pre-existing default unit starts DISABLED unless a test/migration enables it.
+ enabled={("coder", "systemd", False), ("ops", "systemd", False)},
)
def _service_op(kind, system, verb, home, *, run_as_user=None):
@@ -57,8 +60,12 @@ def fleet(tmp_path, monkeypatch):
state.services[name] = remaining
else:
state.services.pop(name, None)
+ state.enabled.discard((name, kind, system))
elif verb == "install":
state.services[name] = (kind, system)
+ state.enabled.add((name, kind, system))
+ elif verb == "enable":
+ state.enabled.add((name, kind, system))
elif verb in ("start", "restart") and name == "default":
(root / "gateway.pid").write_text(json.dumps({"pid": os.getpid(), "hermes_home": str(root)}))
runtime_path = root / "gateway_state.json"
@@ -855,6 +862,25 @@ def test_half_migrated_host_converges_on_a_re_run(fleet, capsys):
assert gm.build_migration_plan().already_multiplexed
+def test_migrate_enables_the_survivor_before_it_removes_anything(fleet, capsys):
+ """Systemctl recorder: a pre-existing, DISABLED survivor unit is enabled BEFORE the first
+ secondary uninstall (uninstall = stop + disable + wants-unlink), so an apply interrupted at any
+ later step still leaves a boot-startable gateway. Base restarted the survivor after the removals
+ and never enabled it: N boot-startable gateways became 0 while the migration reported ✓."""
+ fleet.services["default"] = ("systemd", False) # unit exists; NOT in fleet.enabled
+ assert ("default", "systemd", False) not in fleet.enabled
+
+ with pytest.raises(SystemExit) as exc:
+ gm.cmd_migrate(SimpleNamespace(multiplex=True, dry_run=False, yes=True))
+ assert exc.value.code == 0
+
+ first_removal = min(i for i, op in enumerate(fleet.ops) if op[1] in ("stop", "uninstall"))
+ assert fleet.ops.index(("default", "enable")) < first_removal
+ # A reboot has exactly one gateway: the survivor, and none of the folded secondaries.
+ assert fleet.enabled == {("default", "systemd", False)}
+ assert fleet.ops[-1] == ("default", "restart") and "serves 3 profiles" in capsys.readouterr().out
+
+
def test_plan_names_every_process_it_will_sigterm_before_it_signals_anything(fleet, capsys):
"""A running gateway is never stopped silently: the dry run names the pids and changes nothing."""
gm.cmd_migrate(SimpleNamespace(multiplex=True, dry_run=True, yes=True))