Skip to content

fix(mcp): catch RuntimeError when canceling tasks on closed event loop during /exit - #60205

Closed
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:liuhao/cron-bugfix-60197
Closed

fix(mcp): catch RuntimeError when canceling tasks on closed event loop during /exit#60205
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:liuhao/cron-bugfix-60197

Conversation

@liuhao1024

Copy link
Copy Markdown
Contributor

What does this PR do?

Catches RuntimeError when canceling MCP server tasks on a closed event loop during /exit. When calling /exit, the shutdown sequence closes the asyncio event loop, but MCP tasks still running in _wait_for_reconnect_or_shutdown call t.cancel()call_soon(), causing RuntimeError: Event loop is closed exceptions that are printed as "Exception ignored" noise.

The fix moves t.cancel() inside the try block and catches RuntimeError alongside CancelledError. This silences the noise without changing functional behavior (the exceptions were already being caught and ignored).

Related Issue

Fixes #60197

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)

Changes Made

  • tools/mcp_tool.py: Move t.cancel() inside the try block and catch RuntimeError alongside CancelledError in two locations (line 1837 and 1882)

How to Test

  1. Run the MCP tests: python -m pytest tests/tools/test_mcp_tool.py -q
  2. All 203 tests pass
  3. Observed result: calling /exit on an MCP-enabled session no longer emits "Exception ignored" RuntimeError noise

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/ -q and all tests pass
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: macOS

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — or N/A
  • I've updated cli-config.yaml.example if I added/changed config keys — or N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide
  • I've updated tool descriptions/schemas if I changed tool behavior — or N/A

For New Skills

  • N/A

…p during /exit

When calling /exit, MCP server tasks still running in
_wait_for_reconnect_or_shutdown call t.cancel() -> call_soon() after
the event loop is closed, causing RuntimeError exceptions.

Move t.cancel() inside the try block and catch RuntimeError alongside
CancelledError. This silences the 'Exception ignored' noise without
changing functional behavior (the exceptions were already being caught
and ignored).

Fixes NousResearch#60197
@alt-glitch alt-glitch added type/bug Something isn't working comp/tools Tool registry, model_tools, toolsets tool/mcp MCP client and OAuth duplicate This issue or pull request already exists P2 Medium — degraded but workaround exists labels Jul 7, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

This was generated by AI during triage.
Duplicate of #60032 (earliest open, identical fix at the same two t.cancel() sites: cancel inside try + catch RuntimeError). Same mechanism as #60213; #60104 is a different approach (drain parked tasks before loop.close()). All target #60197.

@liuhao1024

Copy link
Copy Markdown
Contributor Author

Thanks @alt-glitch for the duplicate detection. After comparing the diffs:

Since #60032 is earlier and provides the same fix with minimal code changes, maintainers should merge #60032 instead. Closing this PR as a duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/tools Tool registry, model_tools, toolsets duplicate This issue or pull request already exists P2 Medium — degraded but workaround exists tool/mcp MCP client and OAuth type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] RuntimeError: Event loop is closed during /exit (MCPServerTask.shutdown)

2 participants