Fix/issue 915 client session group#2443
Open
VrtxOmega wants to merge 2 commits intomodelcontextprotocol:mainfrom
Open
Fix/issue 915 client session group#2443VrtxOmega wants to merge 2 commits intomodelcontextprotocol:mainfrom
VrtxOmega wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
added 2 commits
April 14, 2026 02:23
…eError in ClientSessionGroup on connection disconnects
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.
Fixes #915 by sequentially closing
ClientSessionGroupsession stacks during teardown, preventinganyiocancellation scope violations that previously masked underlying connection errors.Motivation and Context
When
ClientSessionGroupencounters a connection failure (e.g., when a remote server is unavailable), it triggers__aexit__due to the exiting context manager. Prior to this fix,__aexit__attempted to concurrently close all previously established session exit stacks using ananyiotask group (async with anyio.create_task_group()).Because this teardown occurred while unwinding an exception, AnyIO correctly threw
RuntimeError: Attempted to exit cancel scope in a different task. This completely masked the original connection error (likeConnectError) and confused consumers. By iterating through_session_exit_stacksand sequentially awaitingaclose(), we preserve AnyIO task contexts and allow the true exception to propagate cleanly.How Has This Been Tested?
ConnectErrorand arbitrary exceptions are now successfully caught by the user instead of throwingRuntimeError: Attempted to exit cancel scope in a different task.pytest tests/shared/test_session_group.pytest suite locally to ensure no regressions in default scope teardown.Breaking Changes
None. Users will now properly receive
ExceptionGrouporConnectErroron failure rather than cryptic AnyIO scope errors.Types of changes
Checklist
Additional context
Fixes #915
This maintains scope integrity for asynchronous connections since the sessions were already created sequentially in the main task; they can safely be brought down sequentially.