Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: attempted to exit cancel scope in a different task (mcp) #7268

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

phact
Copy link
Collaborator

@phact phact commented Mar 25, 2025

This pull request introduces several changes to improve the cleanup process and error handling in the MCPStdioClient and MCPSseClient classes in the src/backend/base/langflow/base/mcp/util.py file. Additionally, it adds context management methods to the MCPComponent class in the src/backend/base/langflow/components/tools/mcp_component.py file.

Improvements to Cleanup Process and Error Handling:

  • Added a _cleanup_lock and _is_cleaning flag to both MCPStdioClient and MCPSseClient classes to ensure that cleanup operations are not executed concurrently. [1] [2]
  • Introduced a cleanup method in both MCPStdioClient and MCPSseClient 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]
  • Enhanced error handling in the connect_to_server methods by adding specific exception handling for asyncio.CancelledError, GeneratorExit, and general exceptions to ensure proper cleanup and provide informative error messages. [1] [2]

Context Management:

  • Added __aenter__ and __aexit__ methods to the MCPComponent class to support asynchronous context management, ensuring that cleanup operations are performed when the component is no longer needed.
  • Introduced a cleanup method in the MCPComponent class to explicitly handle the cleanup of stdio_client and sse_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.

@phact phact requested a review from Cristhianzl March 25, 2025 15:41
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Mar 25, 2025
@Cristhianzl Cristhianzl changed the title mcp fix: attempted to exit cancel scope in a different task fix: attempted to exit cancel scope in a different task (mcp) Mar 25, 2025
@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Mar 25, 2025
Copy link
Member

@Cristhianzl Cristhianzl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Mar 25, 2025
@phact phact enabled auto-merge March 25, 2025 15:59
Copy link

codspeed-hq bot commented Mar 25, 2025

CodSpeed Performance Report

Merging #7268 will degrade performances by 19.57%

Comparing cancel-task-mcp (cb7769a) with main (58e0d42)

Summary

❌ 1 regressions
✅ 18 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
test_cancel_nonexistent_build 7.7 ms 9.6 ms -19.57%

@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Mar 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants