fix(mcp): catch RuntimeError in _wait_for_reconnect_or_shutdown cleanup - #71846
Closed
Arasz wants to merge 1 commit into
Closed
fix(mcp): catch RuntimeError in _wait_for_reconnect_or_shutdown cleanup#71846Arasz wants to merge 1 commit into
Arasz wants to merge 1 commit into
Conversation
When the event loop closes during MCP server reload or Ctrl+C exit, parked MCPServerTask coroutines are garbage-collected. Their finally blocks call t.cancel() which invokes loop.call_soon() on the already- closed loop, raising RuntimeError: Event loop is closed. This produced 14+ ignored exception tracebacks on every MCP reload. Fix: wrap t.cancel() in try/except RuntimeError at both cleanup sites in _wait_for_reconnect_or_shutdown. When the loop is already closed, the tasks are dead — breaking out is the correct cleanup. Closes NousResearch#60197 Closes NousResearch#66228
Collaborator
Duplicate of #60032. The current diff applies the same RuntimeError guard to the same two MCP cleanup loops; its added regression test does not introduce a distinct repair mechanism. |
8 tasks
This was referenced Jul 29, 2026
Contributor
|
Thanks for isolating the closed-loop cancellation symptom and adding coverage. Automated hermes-sweeper review found this is already implemented on current
The current diff is therefore redundant; thank you for the focused investigation. |
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.
What
Catches
RuntimeErrorin thefinallyblock of_wait_for_reconnect_or_shutdownat both cleanup sites inmcp_tool.py.Why
When the event loop closes during MCP server reload or Ctrl+C exit, parked
MCPServerTaskcoroutines are garbage-collected. Theirfinallyblocks callt.cancel()which invokesloop.call_soon()on the already-closed loop, raisingRuntimeError: Event loop is closed.This produces 14+ ignored exception tracebacks on every MCP reload — noisy and confusing for users, though functionally harmless.
How
Wrap
t.cancel()intry/except RuntimeErrorat both_wait_for_reconnect_or_shutdowncleanup sites (lines ~2297 and ~2341). When the loop is already closed, the tasks are dead — breaking out of the cleanup loop is the correct behavior.The fix is purely defensive: no behavior change during normal operation, just graceful handling of the shutdown race.
Test plan
test_wait_for_reconnect_or_shutdown_no_error_on_closed_loop— verifies noRuntimeErrorescapes when the loop closes during parktest_wait_for_reconnect_or_shutdown_returns_on_shutdown_event— baseline: normal shutdown path still workstest_mcp_reconnect_retry_reset.py(2/2 pass)hermes chat -q "hello"then Ctrl+C — no tracebackPlatforms tested
macOS (Apple Silicon, Python 3.11.15)
Related issues
Closes #60197
Closes #66228