Skip to content

Chatbot example: clean up servers in correct order #353

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

Merged
merged 1 commit into from
May 23, 2025

Conversation

clareliguori
Copy link
Contributor

@clareliguori clareliguori commented Mar 23, 2025

The servers's async context must be exited in the reverse order it was entered, unrolling the context stack.

Motivation and Context

Fixes #252

How Has This Been Tested?

$ python main.py 
You: quit
2025-03-23 08:46:26,961 - INFO - 
Exiting...
$ echo $?
0

Breaking Changes

No

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

@pagoenka
Copy link

Can confirm that this fixes the issues like - #252, but this will cleanup the servers sequentially

@ihrpr ihrpr added this to the r-05-25 milestone Apr 29, 2025
Copy link

@mcp-shadow mcp-shadow bot left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution!

Suggestion: APPROVE

Comment: This PR correctly addresses issue #252 by ensuring servers are cleaned up in the reverse order they were initialized, which is a fundamental requirement when working with async context managers. The error occurs because the original code was using asyncio.gather to clean up servers in parallel, potentially violating the LIFO (Last-In-First-Out) principle of async context stack management.

The solution is simple but effective - replacing the parallel cleanup with a sequential cleanup in reverse order using reversed(self.servers). This approach respects the async context lifecycle and prevents the "Attempted to exit cancel scope in a different task than it was entered in" error.

While there is a minor performance trade-off by cleaning up servers sequentially instead of in parallel, correctness must take precedence over performance in this case. The change is minimal, well-tested, and directly addresses the reported issue without introducing new risks.

Copy link
Contributor

@ihrpr ihrpr left a comment

Choose a reason for hiding this comment

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

thank you!

@ihrpr ihrpr merged commit 4cd4fe0 into modelcontextprotocol:main May 23, 2025
10 checks passed
@clareliguori clareliguori deleted the chatbot-cleanup branch May 23, 2025 13:39
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.

Error during cleanup of server sqlite: Attempted to exit cancel scope in a different task than it was entered in
3 participants