Skip to content

The agent that grows with you

fix(gateway): probe a candidate profile's own gateway off the event loop

liuhao10241c88e4e31a53
diff --git a/gateway/run_profile_reconcile.py b/gateway/run_profile_reconcile.py
index ed1440ebeca..59036ccef7d 100644
--- a/gateway/run_profile_reconcile.py
+++ b/gateway/run_profile_reconcile.py
@@ -28,6 +28,10 @@ logger = logging.getLogger(__name__)
_PROFILE_RESCAN_INTERVAL_SECS = 30.0
_PROFILE_SIGNATURE_FILES = ("config.yaml", ".env")
+# Bound for one own-gateway liveness probe (``live_gateway_pid_for_home``): the control-socket
+# read it can end in has no timeout of its own — on Windows the named pipe stalls there until
+# its peer answers — so the await needs one. A healthy identify answers in milliseconds.
+_OWN_GATEWAY_PROBE_TIMEOUT_SECS = 5.0
def profile_serve_signature(home: "Path") -> tuple:
@@ -113,7 +117,22 @@ class GatewayProfileReconcileMixin:
for name in list(current):
if name == active or name in known:
continue
- if live_gateway_pid_for_home(current[name]) is not None:
+ # The probe can end in a control-socket read that has no timeout of its own
+ # (a Windows named pipe stalls there until its peer answers). Inline on the
+ # loop thread it parked shutdown_watchdog liveness probes and the multiplexer
+ # was hard-killed with exit 75 (#132547). Probe off the loop, bounded: a
+ # stalled probe only wedges one bounded housekeeping worker for this cycle
+ # (wait_for cancels the still-queued await), never the event loop.
+ try:
+ pid = await asyncio.wait_for(
+ self._run_housekeeping_in_executor(live_gateway_pid_for_home, current[name]),
+ timeout=_OWN_GATEWAY_PROBE_TIMEOUT_SECS)
+ except asyncio.TimeoutError:
+ # Unprovable this cycle must read as blocked: the multiplexer never races
+ # a possibly-live own gateway. Next cycle probes again.
+ logger.debug("Own-gateway probe for profile '%s' stalled; treating as blocked", name)
+ pid = -1
+ if pid is not None:
blocked.add(name)
if name not in warned:
logger.warning("[MULTIPLEX] Profile '%s' still runs its own gateway; "
diff --git a/tests/gateway/test_multiplex_hot_serve.py b/tests/gateway/test_multiplex_hot_serve.py
index 875bdf5d441..59b52910262 100644
--- a/tests/gateway/test_multiplex_hot_serve.py
+++ b/tests/gateway/test_multiplex_hot_serve.py
@@ -7,6 +7,7 @@ profiles' live adapters. The cron ticker's live enumerator is covered in ``tests
"""
import asyncio
import json
+import threading
from pathlib import Path
from unittest.mock import MagicMock, patch
@@ -111,6 +112,51 @@ async def test_opt_out_rescans_and_opt_in_waits_for_own_gateway_to_stop(tmp_path
assert runner._started.count("solo") == 2
+async def test_stalled_own_gateway_probe_never_wedges_the_loop_or_serves(tmp_path, monkeypatch, caplog):
+ """#132547: the pre-serve own-gateway probe can end in a control-socket read that has no
+ timeout of its own (a Windows named pipe stalls there until its peer answers); inline on
+ the loop thread it parked shutdown_watchdog liveness probes until the multiplexer was
+ hard-killed with exit 75. A stalled probe must leave the event loop turning, keep the
+ profile unserved for that cycle, and the profile must serve once the probe answers."""
+ from gateway import run_profile_reconcile as reconcile_mod
+
+ runner, home = _runner(tmp_path, monkeypatch)
+ _mkprofile(home, "alpha", "DISCORD_BOT_TOKEN=alpha-token\n")
+ with patch("hermes_cli.profiles.get_active_profile_name", return_value="default"):
+ await runner._start_secondary_profile_adapters()
+
+ released = threading.Event()
+
+ def _stalling_probe(profile_home):
+ released.wait(timeout=10) # the control pipe that never answers
+ return None
+
+ monkeypatch.setattr("gateway.status.live_gateway_pid_for_home", _stalling_probe)
+ monkeypatch.setattr(reconcile_mod, "_OWN_GATEWAY_PROBE_TIMEOUT_SECS", 0.2)
+
+ _mkprofile(home, "gamma", "DISCORD_BOT_TOKEN=gamma-token\n")
+
+ async def _liveness_probe():
+ # shutdown_watchdog stand-in: the loop must keep turning while the probe is stalled.
+ for _ in range(4):
+ await asyncio.sleep(0.05)
+
+ heartbeat = asyncio.ensure_future(_liveness_probe())
+ # On the unfixed code an inline probe parks the loop for the full ``released`` wait
+ # and this wait_for times out instead of returning a reconcile result.
+ result = await asyncio.wait_for(runner.reconcile_served_profiles(reason="watcher"), timeout=2.0)
+ assert result["added"] == []
+ assert _served_record(home) == ["default", "alpha"]
+ assert len([r for r in caplog.records if "still runs its own gateway" in r.message]) == 1
+ await asyncio.wait_for(heartbeat, timeout=1.0)
+
+ released.set()
+ result = await runner.reconcile_served_profiles(reason="watcher")
+ assert result["added"] == ["gamma"]
+ assert _served_record(home) == ["default", "alpha", "gamma"]
+
+
@pytest.mark.asyncio
async def test_parked_profile_boot_and_reconcile(tmp_path, monkeypatch, caplog):
runner, home = _runner(tmp_path, monkeypatch)