Python: fix(core): handle anyio cancel scope errors during MCP connection cleanup#3277
Merged
moonbox3 merged 3 commits intomicrosoft:mainfrom Jan 21, 2026
Merged
Python: fix(core): handle anyio cancel scope errors during MCP connection cleanup#3277moonbox3 merged 3 commits intomicrosoft:mainfrom
moonbox3 merged 3 commits intomicrosoft:mainfrom
Conversation
Member
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a critical bug where using multiple MCP tools with AG-UI streaming would fail with a RuntimeError when attempting to close cancel scopes across task boundaries. The fix introduces a new _safe_close_exit_stack() helper method that gracefully handles anyio cancel scope errors during connection cleanup.
Changes:
- Added
_safe_close_exit_stack()method to handle cross-task cancel scope errors - Replaced all direct
_exit_stack.aclose()calls with the safe wrapper - Added comprehensive tests for the new error handling behavior
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| python/packages/core/agent_framework/_mcp.py | Implements new _safe_close_exit_stack() method with error handling for anyio cancel scope errors and updates all call sites to use the safe wrapper |
| python/packages/core/tests/core/test_mcp.py | Adds three new test cases covering the main scenario, alternate error messages, and re-raising unrelated errors |
eavanvalkenburg
approved these changes
Jan 19, 2026
giles17
approved these changes
Jan 20, 2026
giles17
approved these changes
Jan 21, 2026
arisng
pushed a commit
to arisng/agent-framework
that referenced
this pull request
Feb 2, 2026
…tion cleanup (microsoft#3277) * fix(core): handle anyio cancel scope errors during MCP connection cleanup * Address Copilot feedback
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.
Motivation and Context
When using multiple MCP tools with AG-UI streaming, the second MCP tool call would fail with:
RuntimeError: Attempted to exit cancel scope in a different task than it was entered inThis occurred because:
AsyncExitStackinMCPToolstores context managers from the MCP library'sstreamable_http_clientanyio.create_task_group()internallyconnect(reset=True)orclose()is called from within an AG-UI streaming loop (a different async task than where the connection was established), anyio detects a task boundary violationTo fix the issue, this PR adds a
_safe_close_exit_stack()helper method that:RuntimeErrorvariants and logs a warning instead of crashingCancelledErrorthat can occur during cleanup_exit_stack.aclose()callsDescription
Contribution Checklist