fix(mcp): add timeout to background thread join in stop() to prevent hangs#1787
Closed
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
Closed
fix(mcp): add timeout to background thread join in stop() to prevent hangs#1787giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
Conversation
…hangs When an Agent holding an MCPClient goes out of scope inside a function, the Agent.__del__ finalizer calls MCPClient.stop() which calls _background_thread.join(). If the background thread cannot exit promptly (e.g. transport subprocess teardown is slow), join() blocks indefinitely, causing the entire process to hang on exit. Add a timeout (equal to startup_timeout, default 30s) to the join() call. If the thread does not exit within the timeout, log a warning and proceed with cleanup. The thread is already a daemon thread (daemon=True), so it will be cleaned up by the interpreter on process exit. Fixes strands-agents#1732 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Author
|
Closing — focusing on higher-impact contributions. |
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.
Summary
Fixes #1732
When an
Agentholding anMCPClientgoes out of scope inside a function, theAgent.__del__finalizer callsMCPClient.stop()which calls_background_thread.join(). If the background thread cannot exit promptly (e.g. transport subprocess teardown is slow),join()blocks indefinitely, causing the entire process to hang on exit.Root Cause
_background_thread.join()at line 356 ofmcp_client.pyhas no timeout. When called from the GC finalizer (Agent.__del__→ToolRegistry.cleanup()→MCPClient.remove_consumer()→MCPClient.stop()), the transport subprocess may not shut down quickly, causing the join to block forever.Fix
Add
timeout=self._startup_timeout(default 30s) to thejoin()call. If the thread does not exit within the timeout, log a warning and continue cleanup. The thread is already a daemon thread (daemon=True), so it will be cleaned up by the interpreter on process exit.Changes
src/strands/tools/mcp/mcp_client.pyself._background_thread.join()toself._background_thread.join(timeout=self._startup_timeout)is_alive()check after join to log a warning if the thread didn't exittests/strands/tools/mcp/test_mcp_client.pyjoin(timeout=...)test_stop_does_not_hang_when_join_times_out— verifies cleanup proceeds even when thread is still alive after join timeoutTest Results