fix: attempted to exit cancel scope in a different task (mcp) #7268
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.
This pull request introduces several changes to improve the cleanup process and error handling in the
MCPStdioClient
andMCPSseClient
classes in thesrc/backend/base/langflow/base/mcp/util.py
file. Additionally, it adds context management methods to theMCPComponent
class in thesrc/backend/base/langflow/components/tools/mcp_component.py
file.Improvements to Cleanup Process and Error Handling:
_cleanup_lock
and_is_cleaning
flag to bothMCPStdioClient
andMCPSseClient
classes to ensure that cleanup operations are not executed concurrently. [1] [2]cleanup
method in bothMCPStdioClient
andMCPSseClient
classes to handle the cleanup of sessions and exit stacks. This method is called before establishing new connections and in case of exceptions during the connection process. [1] [2]connect_to_server
methods by adding specific exception handling forasyncio.CancelledError
,GeneratorExit
, and general exceptions to ensure proper cleanup and provide informative error messages. [1] [2]Context Management:
__aenter__
and__aexit__
methods to theMCPComponent
class to support asynchronous context management, ensuring that cleanup operations are performed when the component is no longer needed.cleanup
method in theMCPComponent
class to explicitly handle the cleanup ofstdio_client
andsse_client
instances.These changes enhance the stability and reliability of the MCP clients by ensuring that resources are properly managed and cleaned up, reducing the risk of resource leaks and improving error diagnostics.