fix(mcp): preserve loop during probes

This commit is contained in:
helix4u
2026-06-14 02:09:45 -07:00
committed by Teknium
parent 85e6232a07
commit 4936a49a0c
4 changed files with 48 additions and 6 deletions
+18 -2
View File
@@ -3946,7 +3946,7 @@ def probe_mcp_server_tools() -> Dict[str, List[tuple]]:
except Exception as exc:
logger.debug("MCP probe failed: %s", exc)
finally:
_stop_mcp_loop()
_stop_mcp_loop_if_idle()
return result
@@ -4084,10 +4084,25 @@ def _kill_orphaned_mcp_children(include_active: bool = False) -> None:
)
def _stop_mcp_loop():
def _stop_mcp_loop_if_idle() -> bool:
"""Stop the MCP loop only when no registered server still owns it.
Probe paths create temporary MCPServerTask instances that are not placed in
``_servers``. They should clean up an otherwise-idle loop, but must not
tear down the process-global loop when live agent tools are registered on
it. Otherwise a dashboard/CLI probe can make later MCP tool calls fail
with ``MCP event loop is not running``.
"""
return _stop_mcp_loop(only_if_idle=True)
def _stop_mcp_loop(*, only_if_idle: bool = False) -> bool:
"""Stop the background event loop and join its thread."""
global _mcp_loop, _mcp_thread
with _lock:
if only_if_idle and (_servers or _server_connecting):
logger.debug("Leaving MCP event loop running; active servers are registered or connecting")
return False
loop = _mcp_loop
thread = _mcp_thread
_mcp_loop = None
@@ -4104,3 +4119,4 @@ def _stop_mcp_loop():
# graceful shutdown are now orphaned — include active PIDs too
# since the loop is gone and no session can still be in flight.
_kill_orphaned_mcp_children(include_active=True)
return True