Skip to content

The agent that grows with you

fix(gateway): release gateway-held log and MCP handles when unserving a profile

liuhao1024b4abdaedc032
diff --git a/gateway/run_profile_reconcile.py b/gateway/run_profile_reconcile.py
index bf70d9f177b..96bc942b550 100644
--- a/gateway/run_profile_reconcile.py
+++ b/gateway/run_profile_reconcile.py
@@ -214,6 +214,9 @@ class GatewayProfileReconcileMixin:
async def _unserve_profile(self, name: str, home: "Path") -> None:
"""Stop and unroute one profile: cancel its reconnects, tear down its adapters, drop its
bookkeeping and release this process's handles into its home so the deleter's rmtree succeeds.
+ The releasing process is not always the deleter (#130244): under multiplexing THIS gateway
+ process routes the profile's logs and owns its scoped MCP servers, so both must be released
+ here too or the deleter's rmtree still fails on ``logs/.__agent.lock`` / ``mcp-stderr.log``.
The whole teardown runs inside the removed profile's own runtime scope: adapter disconnect
hooks, the agent-cache eviction (provider/memory shutdown) and the state/memory handle
@@ -256,6 +259,17 @@ class GatewayProfileReconcileMixin:
with _log_suppressed(logging.DEBUG, "memory-store release failed", exc_info=True):
from plugins.memory.holographic.store import MemoryStore
MemoryStore.release_all_under(home)
+ # The gateway process is a second holder the CLI-side delete cannot reach: its routed
+ # log files (logs/.__agent.lock, logs/.__errors.lock) and its scoped MCP servers'
+ # mcp-stderr.log handle live HERE, so unserve must release them in this process too
+ # (#130244) — the same two calls ``delete_profile`` makes for the deleting process.
+ with _log_suppressed(logging.DEBUG, "scoped MCP shutdown failed", exc_info=True):
+ from hermes_constants import hermes_home_key
+ from tools.mcp_tool_lifecycle import shutdown_mcp_servers
+ shutdown_mcp_servers(scope=hermes_home_key(home))
+ with _log_suppressed(logging.DEBUG, "profile log handler release failed", exc_info=True):
+ from hermes_logging import release_profile_log_handlers
+ release_profile_log_handlers(home)
logger.info("[MULTIPLEX] Profile '%s' unserved — %d adapter(s) stopped and unrouted", name, len(adapters))
diff --git a/tests/gateway/test_multiplex_hot_serve.py b/tests/gateway/test_multiplex_hot_serve.py
index de5e6b40371..8d24ab8dc6f 100644
--- a/tests/gateway/test_multiplex_hot_serve.py
+++ b/tests/gateway/test_multiplex_hot_serve.py
@@ -251,6 +251,69 @@ async def test_deleted_profile_is_torn_down_and_unrouted_others_untouched(tmp_pa
assert alpha_adapter.disconnected is False
+async def test_unserve_releases_gateway_held_log_and_mcp_handles(tmp_path, monkeypatch):
+ """#130244: the multiplexer process itself routes per-profile logs and owns the profile's
+ scoped MCP servers, so unserving must release BOTH here — the CLI-side delete only
+ releases the deleting process's copies and otherwise leaves the gateway holding
+ ``logs/.__agent.lock`` / ``logs/.__errors.lock`` / ``mcp-stderr.log`` (WinError 32)."""
+ import logging as _logging
+
+ import hermes_logging
+ from hermes_constants import (
+ hermes_home_key,
+ mark_named_profile_deleted,
+ reset_hermes_home_override,
+ set_hermes_home_override,
+ )
+
+ runner, home = _runner(tmp_path, monkeypatch)
+ alpha_dir = _mkprofile(home, "alpha", "DISCORD_BOT_TOKEN=alpha-token\n")
+ gamma_dir = _mkprofile(home, "gamma", "DISCORD_BOT_TOKEN=gamma-token\n")
+ shutdowns = []
+ monkeypatch.setattr(
+ "tools.mcp_tool_lifecycle.shutdown_mcp_servers",
+ lambda **kw: shutdowns.append(kw.get("scope")),
+ )
+ hermes_logging._reset_queued_handlers() # a prior test's routers would route to stale homes
+ try:
+ hermes_logging.setup_logging(hermes_home=home)
+ assert hermes_logging.enable_profile_log_routing([home, alpha_dir, gamma_dir]) is True
+ logger = _logging.getLogger("agent.unserve-handle-release")
+ for profile_home in (alpha_dir, gamma_dir):
+ token = set_hermes_home_override(profile_home)
+ try:
+ logger.warning("open routed handles for %s", profile_home.name)
+ finally:
+ reset_hermes_home_override(token)
+ hermes_logging.flush_log_queue()
+ routers = [
+ handler for handler in hermes_logging._queued_file_handlers
+ if isinstance(handler, hermes_logging._ProfileRoutingFileHandler)
+ ]
+ assert len(routers) == 2 # agent.log and errors.log, as in test_hermes_logging
+ assert all(gamma_dir.resolve() in handler._profile_handlers for handler in routers)
+
+ with patch("hermes_cli.profiles.get_active_profile_name", return_value="default"):
+ await runner._start_secondary_profile_adapters()
+ mark_named_profile_deleted(gamma_dir) # what ``delete_profile`` does before rmtree
+ result = await runner.reconcile_served_profiles()
+
+ assert result["removed"] == ["gamma"]
+ # The gateway-side scoped MCP servers (whose children write mcp-stderr.log) are stopped
+ # for exactly the deleted profile's scope, never a sibling's.
+ assert shutdowns == [hermes_home_key(gamma_dir)]
+ # This process's routed log files for the deleted home are closed and forgotten while a
+ # still-served sibling keeps its handles.
+ assert all(gamma_dir.resolve() not in handler._profile_handlers for handler in routers)
+ assert all(gamma_dir.resolve() not in handler._profile_homes for handler in routers)
+ assert all(alpha_dir.resolve() in handler._profile_handlers for handler in routers)
+ assert "open routed handles for alpha" in (alpha_dir / "logs" / "agent.log").read_text()
+ finally:
+ hermes_logging._reset_queued_handlers()
+ hermes_logging._logging_initialized = False
+
+
@pytest.mark.asyncio
async def test_transient_start_failure_is_retried_on_next_reconcile(tmp_path, monkeypatch):
"""A hot-added profile whose adapter start raises a transient error (secret backend