Skip to content

The agent that grows with you

fix(gateway): a confirmed downgrade stop exits with the service-restart code

赵桂雄a160f8778476
diff --git a/gateway/runtime_downgrade_exit.py b/gateway/runtime_downgrade_exit.py
index b1460cd387a..33a0152f51f 100644
--- a/gateway/runtime_downgrade_exit.py
+++ b/gateway/runtime_downgrade_exit.py
@@ -19,6 +19,8 @@ import os
from pathlib import Path
from typing import Callable, Optional
+from gateway.restart import GATEWAY_SERVICE_RESTART_EXIT_CODE
+
logger = logging.getLogger(__name__)
POLL_S = 30.0
@@ -84,6 +86,14 @@ async def runtime_downgrade_watcher(runner, *, poll_s: float = POLL_S, skew_fn=N
if seen >= _CONFIRMATIONS:
logger.error("%s", reason)
runner._exit_reason = reason
+ # A confirmed downgrade past a free update lock means the tree on disk is
+ # the operator's settled choice. Exit with the service-restart code so the
+ # supervisor relaunches and the settled tree serves — systemd's
+ # Restart=always already does this on any exit; the launchd plist
+ # (KeepAlive SuccessfulExit=false) parks exit 0, which would leave the
+ # gateway down until a human notices. The stop itself stays a plain stop:
+ # the relaunch runs whatever release is on disk, not this process.
+ runner._exit_code = GATEWAY_SERVICE_RESTART_EXIT_CODE
# Detached, strongly held: stop() cancels every background task, this watcher included,
# and a stop awaited from here would be cancelled with it (the #12875 class).
runner._runtime_downgrade_stop = asyncio.get_running_loop().create_task(_stop_or_exit(runner))
diff --git a/tests/gateway/test_runtime_downgrade_exit.py b/tests/gateway/test_runtime_downgrade_exit.py
index 9defa955bd4..f0890172a53 100644
--- a/tests/gateway/test_runtime_downgrade_exit.py
+++ b/tests/gateway/test_runtime_downgrade_exit.py
@@ -4,12 +4,14 @@ Downgrading is unsupported (#106742, jquesnelle item 6). After a manual ``git ch
release>`` under a running head gateway, the older client cannot attach to it, and older
``gateway run`` refuses because this process still holds the profile. Nothing on the older side
can stop it, so it stops itself: only on a proven, confirmed move to pre-runtime code, never
-mid-update, and without asking for a restart.
+mid-update, and without asking for a restart — the stop is a plain stop, but it exits with the
+service-restart code so the supervisor relaunches and the settled tree on disk serves.
"""
import asyncio
import subprocess
from gateway import runtime_downgrade_exit as rde
+from gateway.restart import GATEWAY_SERVICE_RESTART_EXIT_CODE
_SKEW = ("aaaaaaaaaa", "bbbbbbbbbb")
@@ -17,6 +19,9 @@ _SKEW = ("aaaaaaaaaa", "bbbbbbbbbb")
class _Runner:
def __init__(self):
self._running, self.stopped, self.restart_kwargs = True, 0, None
+ self._exit_code: int | None = None
+
+ exit_code = property(lambda self: self._exit_code)
async def stop(self, **kwargs):
self.stopped += 1
@@ -38,7 +43,8 @@ def _watch(skews, *, update=False, predates=lambda: True):
def test_a_confirmed_move_to_a_pre_runtime_tree_stops_without_a_restart(tmp_path):
runner = _watch([_SKEW, _SKEW])
- assert runner.stopped == 1 and runner.restart_kwargs == {} # a plain stop: no exit-75 respawn of this code
+ assert runner.stopped == 1 and runner.restart_kwargs == {} # a plain stop: the relaunch runs the tree on disk, not this process
+ assert runner.exit_code == GATEWAY_SERVICE_RESTART_EXIT_CODE # the supervisor relaunches the settled tree; the launchd plist parks exit 0 (SuccessfulExit=false)
assert "predates the gateway runtime" in runner._exit_reason
# A single blip, a live update, or a move to code that still has the runtime keep it up.
assert _watch([_SKEW, None, _SKEW, None]).stopped == 0