fix(mcp): prevent MCPClient from hanging on exit#1847
Open
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
Open
fix(mcp): prevent MCPClient from hanging on exit#1847giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
Conversation
…ad is unresponsive MCPClient.stop() called _background_thread.join() without a timeout, causing the process to hang indefinitely when the background thread could not be signaled (e.g., event loop already closed during interpreter shutdown or finalizer invocation). Changes: - Add cleanup_timeout parameter (default 5.0s) to MCPClient.__init__ - Use timeout in _background_thread.join() to prevent indefinite hangs - Catch RuntimeError from run_coroutine_threadsafe when the event loop is already closed - Log a warning when the thread does not stop within the timeout Fixes strands-agents#1732 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
Bug
MCPClient.stop()hangs indefinitely when the background thread cannot be stopped, causing scripts to freeze on exit.Reported in: #1732
Root cause
_background_thread.join()was called without a timeout. Whenstop()is triggered fromAgent.__del__()(garbage collector) or during interpreter shutdown, the event loop may already be closed, sorun_coroutine_threadsafe(_set_close_event(), loop)silently fails (raisesRuntimeError). The background thread never receives the shutdown signal, andjoin()blocks forever.Fix
cleanup_timeoutparameter (default 5.0 seconds) toMCPClient.__init__()join(timeout=cleanup_timeout)instead of indefinitejoin()RuntimeErrorfromrun_coroutine_threadsafewhen the event loop is already closedTesting
test_stop_does_not_hang_when_background_thread_unresponsive— verifies timeout prevents hangtest_stop_handles_closed_event_loop— verifies graceful handling of closed event loopjoin()assertions to verify timeout parameterFixes #1732