We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 005926c commit a8f72c1Copy full SHA for a8f72c1
src/mcp/client/session_group.py
@@ -165,10 +165,11 @@ async def __aexit__(
165
if self._owns_exit_stack:
166
await self._exit_stack.aclose()
167
168
- # Concurrently close session stacks.
169
- async with anyio.create_task_group() as tg:
170
- for exit_stack in self._session_exit_stacks.values():
171
- tg.start_soon(exit_stack.aclose)
+ # Sequentially close session stacks to preserve AnyIO task contexts.
+ # Concurrent teardown spawns task groups that cross cancel scopes, leading
+ # to RuntimeError: Attempted to exit cancel scope in a different task.
+ for exit_stack in list(self._session_exit_stacks.values()):
172
+ await exit_stack.aclose()
173
174
@property
175
def sessions(self) -> list[mcp.ClientSession]:
0 commit comments