fix(mcp): prevent RuntimeError from escaping except block in get_cach… (#2252)

* 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
This commit is contained in:
imhaoran 2026-04-18 21:07:30 +08:00 committed by GitHub
parent be4663505a
commit 24fe5fbd8c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -118,9 +118,13 @@ def get_cached_mcp_tools() -> list[BaseTool]:
loop.run_until_complete(initialize_mcp_tools()) loop.run_until_complete(initialize_mcp_tools())
except RuntimeError: except RuntimeError:
# No event loop exists, create one # No event loop exists, create one
asyncio.run(initialize_mcp_tools()) try:
except Exception as e: asyncio.run(initialize_mcp_tools())
logger.error(f"Failed to lazy-initialize MCP tools: {e}") except Exception:
logger.exception("Failed to lazy-initialize MCP tools")
return []
except Exception:
logger.exception("Failed to lazy-initialize MCP tools")
return [] return []
return _mcp_tools_cache or [] return _mcp_tools_cache or []