Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

lorenss-m
Copy link
Contributor

Changes

Fixes Docker containers and subprocesses not terminating when closing multiple MCP-use clients. Also adds logging callback support.

Implementation Details

  1. Fixed asyncio cancel scope error during cleanup

    • Stop connection manager first (closes ClientSession in correct task)
    • Clear session reference to prevent duplicate cleanup
    • Add graceful shutdown via _stop_event instead of task cancellation
  2. Added logging callback support

    • Pass LoggingFnT through all connectors and client initialization

Example

# Before: Containers kept running, "cancel scope" errors
await client1.close_all_sessions()  # Error!
await client2.close_all_sessions()  # Error!

# After: Clean shutdown
await client1.close_all_sessions()  # Works!
await client2.close_all_sessions()  # Works!

Testing

  • Verified Docker containers terminate immediately on close
  • No more "Attempted to exit cancel scope in a different task" errors
  • Tested with multiple concurrent clients

Backwards Compatibility

Fully backwards compatible - all changes are internal or optional parameters.

@pietrozullo pietrozullo requested a review from Copilot August 17, 2025 20:53
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes subprocess and Docker container cleanup issues when multiple MCP clients are closed, resolving "cancel scope" errors and ensuring proper resource termination. It also adds logging callback support throughout the connector architecture.

  • Replaces task cancellation with graceful stop events to prevent asyncio cancel scope errors
  • Reorders cleanup to stop connection managers before closing client sessions
  • Adds logging callback parameter support through connectors
  • Improves error handling for MCP protocol errors vs transport errors

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
mcp_use/task_managers/base.py Replaces task cancellation with stop event mechanism for graceful shutdown
mcp_use/connectors/http.py Adds MCP protocol error handling and fixes connection manager cleanup call
mcp_use/connectors/base.py Reorders cleanup sequence to prevent cancel scope mismatches

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Co-authored-by: Copilot <[email protected]>
Signed-off-by: lorenss <[email protected]>
@pietrozullo pietrozullo merged commit 9c6dc85 into mcp-use:main Aug 27, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants