From 24fe5fbd8cea7319366495c21b57509c69414d77 Mon Sep 17 00:00:00 2001 From: imhaoran <117565557+thresh111@users.noreply.github.com> Date: Sat, 18 Apr 2026 21:07:30 +0800 Subject: [PATCH] =?UTF-8?q?fix(mcp):=20prevent=20RuntimeError=20from=20esc?= =?UTF-8?q?aping=20except=20block=20in=20get=5Fcach=E2=80=A6=20(#2252)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(mcp): prevent RuntimeError from escaping except block in get_cached_mcp_tools When `asyncio.get_event_loop()` raises RuntimeError and the fallback `asyncio.run()` also fails, the exception escapes unhandled because Python does not route exceptions raised inside an `except` block to sibling `except` clauses. Wrap the fallback call in its own try/except so failures are logged and the function returns [] as intended. * fix: use logger.exception to preserve stack traces on MCP init failure --- backend/packages/harness/deerflow/mcp/cache.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/backend/packages/harness/deerflow/mcp/cache.py b/backend/packages/harness/deerflow/mcp/cache.py index 38750e135..c1121f59d 100644 --- a/backend/packages/harness/deerflow/mcp/cache.py +++ b/backend/packages/harness/deerflow/mcp/cache.py @@ -118,9 +118,13 @@ def get_cached_mcp_tools() -> list[BaseTool]: loop.run_until_complete(initialize_mcp_tools()) except RuntimeError: # No event loop exists, create one - asyncio.run(initialize_mcp_tools()) - except Exception as e: - logger.error(f"Failed to lazy-initialize MCP tools: {e}") + try: + asyncio.run(initialize_mcp_tools()) + except Exception: + logger.exception("Failed to lazy-initialize MCP tools") + return [] + except Exception: + logger.exception("Failed to lazy-initialize MCP tools") return [] return _mcp_tools_cache or []