From 751b91446e2f3df02952c26a8c279b4ca34474fb Mon Sep 17 00:00:00 2001 From: annguyenNous Date: Thu, 4 Jun 2026 08:24:25 +0700 Subject: [PATCH] fix(mcp): ensure server.shutdown() on probe iteration failure Wrap the _tools iteration in _probe_single_server() in try/finally so that server.shutdown() is called even if iterating tool metadata raises. Without this, the MCP server connection leaks until the event loop is torn down by _stop_mcp_loop(). --- hermes_cli/mcp_config.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/hermes_cli/mcp_config.py b/hermes_cli/mcp_config.py index 377c6b20b00..bb8f8948759 100644 --- a/hermes_cli/mcp_config.py +++ b/hermes_cli/mcp_config.py @@ -225,13 +225,15 @@ def _probe_single_server( server = await asyncio.wait_for( _connect_server(name, config), timeout=connect_timeout ) - for t in server._tools: - desc = getattr(t, "description", "") or "" - # Truncate long descriptions for display - if len(desc) > 80: - desc = desc[:77] + "..." - tools_found.append((t.name, desc)) - await server.shutdown() + try: + for t in server._tools: + desc = getattr(t, "description", "") or "" + # Truncate long descriptions for display + if len(desc) > 80: + desc = desc[:77] + "..." + tools_found.append((t.name, desc)) + finally: + await server.shutdown() try: _run_on_mcp_loop(_probe(), timeout=connect_timeout + 10)