The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): unserve the profile handles off the event loop
liuhao1024918c72e06f73
diff --git a/gateway/run_profile_reconcile.py b/gateway/run_profile_reconcile.pyindex 96bc942b550..ed1440ebeca 100644--- a/gateway/run_profile_reconcile.py+++ b/gateway/run_profile_reconcile.py@@ -263,13 +263,19 @@ class GatewayProfileReconcileMixin:# 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.+ # Both calls BLOCK (the log release stops/joins the QueueListener, which can be inside+ # a ConcurrentRotatingFileHandler emit waiting on another process's rotation lock; the+ # MCP shutdown waits on future.result for the MCP loop), so they run off the loop in a+ # worker thread — otherwise every other served profile's events stall behind them.+ # ``asyncio.to_thread`` copies the context, so the surrounding+ # ``_profile_runtime_scope`` home/secret override stays visible in the worker.with _log_suppressed(logging.DEBUG, "scoped MCP shutdown failed", exc_info=True):from hermes_constants import hermes_home_keyfrom tools.mcp_tool_lifecycle import shutdown_mcp_servers- shutdown_mcp_servers(scope=hermes_home_key(home))+ await asyncio.to_thread(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)+ await asyncio.to_thread(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.pyindex 8d24ab8dc6f..875bdf5d441 100644--- a/tests/gateway/test_multiplex_hot_serve.py+++ b/tests/gateway/test_multiplex_hot_serve.py@@ -270,6 +270,18 @@ async def test_unserve_releases_gateway_held_log_and_mcp_handles(tmp_path, monkerunner, 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")++ def _holds_open_log_file(handler) -> bool:+ # concurrent-log-handler (Windows) keeps the cross-process ``.__agent.lock``+ # handle in ``stream_lock`` — what the deleter's rmtree actually trips over —+ # and closes ``stream`` again after every write; stdlib (POSIX) keeps the log+ # ``stream`` itself open between writes.+ lock_stream = getattr(handler, "stream_lock", None)+ if lock_stream is not None and not lock_stream.closed:+ return True+ stream = getattr(handler, "stream", None)+ return stream is not None and not stream.closed+shutdowns = []monkeypatch.setattr("tools.mcp_tool_lifecycle.shutdown_mcp_servers",@@ -293,6 +305,11 @@ async def test_unserve_releases_gateway_held_log_and_mcp_handles(tmp_path, monke]assert len(routers) == 2 # agent.log and errors.log, as in test_hermes_loggingassert all(gamma_dir.resolve() in handler._profile_handlers for handler in routers)+ gamma_handlers = [handler._profile_handlers[gamma_dir.resolve()] for handler in routers]+ alpha_handlers = [handler._profile_handlers[alpha_dir.resolve()] for handler in routers]+ # Before the reconcile the deleted profile's routed handlers each hold an open fd into+ # its home — the ``.__agent.lock`` lock handle on Windows, the log stream on POSIX.+ assert all(_holds_open_log_file(handler) for handler in gamma_handlers)with patch("hermes_cli.profiles.get_active_profile_name", return_value="default"):await runner._start_secondary_profile_adapters()@@ -308,6 +325,10 @@ async def test_unserve_releases_gateway_held_log_and_mcp_handles(tmp_path, monkeassert 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)+ # The popped handler's fd into the deleted home is actually closed — not just the+ # routing entry removed — while a still-served sibling keeps its handles open.+ assert not any(_holds_open_log_file(handler) for handler in gamma_handlers)+ assert all(_holds_open_log_file(handler) for handler in alpha_handlers)assert "open routed handles for alpha" in (alpha_dir / "logs" / "agent.log").read_text()finally:hermes_logging._reset_queued_handlers()