fix(mcp): clear stale thread interrupt before MCP discovery - #10287
Closed
AJV20 wants to merge 1 commit into
Closed
Conversation
Fixes NousResearch#9930 When an agent session is interrupted (Ctrl+C or gateway timeout), the current thread's interrupt flag is set in _interrupted_threads. asyncio executor threads are pooled and reused across sessions, so a thread that carried an interrupt flag from a prior session will immediately cancel any new asyncio work dispatched to it — including MCP server discovery. Fix: in register_mcp_servers(), temporarily clear the interrupt flag on the current thread before running _discover_all(), then restore it afterward in a finally block so the original interrupt state is not lost.
Collaborator
Contributor
|
Merged via #21276 (commit 46d1fc1 on main) — your commit was cherry-picked onto current main with authorship preserved via rebase-merge. E2E-verified live: simulated a stale interrupt on the current thread, ran Note: your PR header said "Fixes #9930" but #9930 is actually a different bug (Python 3.11+ |
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.
Problem
Closes #9930
When an agent session is interrupted (Ctrl+C, gateway timeout, or
/reset), the current thread's interrupt flag is set in_interrupted_threads. asyncio executor threads are pooled and reused across sessions, so a thread that carried an interrupt flag from a prior session will immediately cancel any new asyncio work dispatched to it — including MCP server discovery.This causes all MCP servers to fail with
asyncio.exceptions.CancelledErroron the next session after any interruption, even though the new session has nothing to do with the prior interrupt.Root Cause
register_mcp_servers()calls_run_on_mcp_loop(_discover_all(), timeout=120), which dispatches work to an executor thread. If that thread happens to be one that previously had its interrupt flag set and never got cleared (because the thread was returned to the pool rather than destroyed),is_interrupted()returnsTrueat the start of discovery andCancelledErroris raised immediately.Fix
In
register_mcp_servers(), temporarily clear the interrupt flag on the current thread before running_discover_all(), then restore it in afinallyblock so the original interrupt state is preserved:Testing
Verified by simulating stale interrupt state before calling
register_mcp_servers(): 154 tools from 20 servers discovered successfully in ~3.8s with noCancelledError.