fix(gateway): scope early duplicate guard to pid file
This commit is contained in:
@@ -3865,22 +3865,24 @@ def _guard_existing_gateway_process_conflict(replace: bool = False) -> None:
|
|||||||
is expensive: it pulls in model_tools/plugin discovery first. On small
|
is expensive: it pulls in model_tools/plugin discovery first. On small
|
||||||
instances, a supervisor or dashboard loop repeatedly running bare
|
instances, a supervisor or dashboard loop repeatedly running bare
|
||||||
``hermes gateway run`` can burn memory/CPU just to fail with "already
|
``hermes gateway run`` can burn memory/CPU just to fail with "already
|
||||||
running" after plugin discovery. This cheap CLI-side preflight preserves the
|
running" after plugin discovery. This cheap PID-file preflight preserves the
|
||||||
same user-facing contract while avoiding that startup work.
|
same user-facing contract while avoiding that startup work without scanning
|
||||||
|
unrelated gateway processes from other HERMES_HOME roots.
|
||||||
"""
|
"""
|
||||||
if replace or _running_under_gateway_supervisor():
|
if replace or _running_under_gateway_supervisor():
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
snapshot = get_gateway_runtime_snapshot()
|
from gateway.status import get_running_pid
|
||||||
|
|
||||||
|
pid = get_running_pid()
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.debug("Existing-gateway process probe failed", exc_info=True)
|
logger.debug("Existing-gateway process probe failed", exc_info=True)
|
||||||
return
|
return
|
||||||
if not snapshot.gateway_pids:
|
if pid is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
pid_text = _format_gateway_pids(snapshot.gateway_pids, limit=1)
|
|
||||||
print_error(
|
print_error(
|
||||||
f"Another gateway instance is already running (PID {pid_text})."
|
f"Another gateway instance is already running (PID {pid})."
|
||||||
)
|
)
|
||||||
print(" Use 'hermes gateway restart' to replace it,")
|
print(" Use 'hermes gateway restart' to replace it,")
|
||||||
print(" or 'hermes gateway stop' first.")
|
print(" or 'hermes gateway stop' first.")
|
||||||
|
|||||||
@@ -129,13 +129,6 @@ def _running_snapshot(manager="systemd (user)"):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _process_snapshot(*pids: int, manager="manual process"):
|
|
||||||
return gateway.GatewayRuntimeSnapshot(
|
|
||||||
manager=manager,
|
|
||||||
gateway_pids=tuple(pids),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_run_gateway_refuses_when_service_supervising(monkeypatch, capsys):
|
def test_run_gateway_refuses_when_service_supervising(monkeypatch, capsys):
|
||||||
"""A shell `gateway run --replace` must not become a second writer."""
|
"""A shell `gateway run --replace` must not become a second writer."""
|
||||||
calls = []
|
calls = []
|
||||||
@@ -230,11 +223,7 @@ def test_run_gateway_refuses_existing_process_before_importing_gateway_run(monke
|
|||||||
|
|
||||||
_install_fake_gateway_run(monkeypatch, fake_start_gateway)
|
_install_fake_gateway_run(monkeypatch, fake_start_gateway)
|
||||||
_clear_supervisor_markers(monkeypatch)
|
_clear_supervisor_markers(monkeypatch)
|
||||||
monkeypatch.setattr(
|
monkeypatch.setattr("gateway.status.get_running_pid", lambda: 17907)
|
||||||
gateway,
|
|
||||||
"get_gateway_runtime_snapshot",
|
|
||||||
lambda: _process_snapshot(17907),
|
|
||||||
)
|
|
||||||
|
|
||||||
with pytest.raises(SystemExit) as exc_info:
|
with pytest.raises(SystemExit) as exc_info:
|
||||||
gateway.run_gateway()
|
gateway.run_gateway()
|
||||||
@@ -255,11 +244,7 @@ def test_run_gateway_replace_skips_existing_process_preflight(monkeypatch):
|
|||||||
|
|
||||||
_install_fake_gateway_run(monkeypatch, fake_start_gateway)
|
_install_fake_gateway_run(monkeypatch, fake_start_gateway)
|
||||||
_clear_supervisor_markers(monkeypatch)
|
_clear_supervisor_markers(monkeypatch)
|
||||||
monkeypatch.setattr(
|
monkeypatch.setattr("gateway.status.get_running_pid", lambda: 17907)
|
||||||
gateway,
|
|
||||||
"get_gateway_runtime_snapshot",
|
|
||||||
lambda: _process_snapshot(17907),
|
|
||||||
)
|
|
||||||
monkeypatch.setattr(gateway.asyncio, "run", lambda coro: True)
|
monkeypatch.setattr(gateway.asyncio, "run", lambda coro: True)
|
||||||
|
|
||||||
gateway.run_gateway(replace=True)
|
gateway.run_gateway(replace=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user