fix(mcp): silence Event loop is closed on reload parked teardown - #69564
Closed
buckster123 wants to merge 1 commit into
Closed
buckster123 wants to merge 1 commit into
buckster123 wants to merge 1 commit into
Conversation
Parked MCPServerTask.run helpers cancelled child wait tasks in finally after the shared MCP loop was already closed during reload, which surfaced as: Exception ignored in: <coroutine object MCPServerTask.run ...> RuntimeError: Event loop is closed Root causes: 1. start() can raise after run() parks for revival, leaving an orphaned task never recorded in _servers so reload never reaps it. 2. wait-helper finally blocks called Future.cancel() without guarding against a closed loop. Fix: - _safe_cancel_await for lifecycle / parked / lazy-reconnect wait helpers - track parked-failed starts in _parked_failed_servers and reap them on full MCP teardown - cancel leftover loop tasks before stop in _stop_mcp_loop
Contributor
Collaborator
|
Thanks for the careful MCP lifecycle investigation. This is now implemented on current
This is an automated hermes-sweeper review. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
/reload-mcp(and any full MCP teardown path) could print a noisy ignored exception when tearing down parked MCP servers:run()parks after a permanent/initial connect failure:start()raises, the server is not added to_servers, then reload closes the MCP loop while the parked wait helper'sfinallystill callsFuture.cancel().Changes
_safe_cancel_await— cancel + await wait-helper futures without raising if the loop is already closed. Used by:_wait_for_lifecycle_event_wait_for_reconnect_or_shutdown_wait_for_lazy_reconnect_parked_failed_servers— ifstart()fails but leavesrun()alive (parked for revival), track it so full MCP teardown still callsserver.shutdown()._stop_mcp_loop— cancel leftover tasks on the loop beforeloop.stop(), so any remaining orphans exit while the loop is still alive.Test plan
uv run --extra dev python -m pytest tests/tools/test_mcp_stability.py -q→ 30 passed/reload-mcp— noEvent loop is closedtraceback; healthy servers reconnect.Context
Observed while hot-swapping Hermes MCP config (dropping failed servers + swapping Cerebro to a Rust binary). Reload still succeeded (
Reconnected: blender, cerebro, sonus); the traceback was cosmetic but scary.