Skip to content

fix(mcp): catch RuntimeError when cancelling tasks during /exit (#60197) - #73190

Closed
JonthanaHanh wants to merge 1 commit into
NousResearch:mainfrom
JonthanaHanh:fix/mcp-event-loop-closed
Closed

fix(mcp): catch RuntimeError when cancelling tasks during /exit (#60197)#73190
JonthanaHanh wants to merge 1 commit into
NousResearch:mainfrom
JonthanaHanh:fix/mcp-event-loop-closed

Conversation

@JonthanaHanh

Copy link
Copy Markdown
Contributor

Fixes #60197. During /exit, event loop closes before MCP tasks finish. t.cancel() calls call_soon() on closed loop raising RuntimeError. Moved t.cancel() inside try block to catch RuntimeError. Applied to both locations. 743 MCP tests passed.

…Research#60197)

When /exit closes the asyncio event loop, MCP server tasks still
running in _wait_for_reconnect_or_shutdown call t.cancel() which
invokes call_soon() on the closed loop, raising RuntimeError.

Fix: move t.cancel() inside the try block so RuntimeError is caught.
Applied to both locations (keepalive loop and reconnect handler).
@alt-glitch alt-glitch added type/bug Something isn't working tool/mcp MCP client and OAuth P2 Medium — degraded but workaround exists duplicate This issue or pull request already exists labels Jul 28, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

This was generated by AI during triage.

Duplicate of #60032. This applies the same RuntimeError handling around MCP task cancellation at the same shutdown sites; #60032 is the earlier open canonical fix.

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

Labels

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