fix(tests): convert create_mcp_client_session to asynccontextmanager

The multi-user-basic integration job was consistently failing with
`CancelledError: Cancelled via cancel scope ... by <async_generator_athrow>`
followed by a cascade of `anyio.ClosedResourceError` in every subsequent
test. Root cause: `create_mcp_client_session` was declared as an async
generator driven by `async for session in ...:`, so Python's generator
finalizer (`aclose`) ran under pytest-asyncio's cleanup task instead of
the task that owned the nested `streamablehttp_client` cancel scope.
anyio then raised when the inner task group saw its scope being exited
from a foreign task, leaving the memory object streams half-closed and
poisoning the rest of the session.

Switching to `@asynccontextmanager` + `async with ... as session:` makes
`__aenter__`/`__aexit__` run in the frame that owns the context manager,
satisfying anyio's structured concurrency requirements.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Chris Coutinho
2026-04-15 12:58:47 +02:00
co-authored by Claude Opus 4.6
parent 9b6b554155
commit 3935f45be8
5 changed files with 50 additions and 48 deletions
+2 -2
View File
@@ -39,10 +39,10 @@ def create_sampling_callback(provider: Provider):
if provider.supports_generation:
callback = create_sampling_callback(provider)
async for session in create_mcp_client_session(
async with create_mcp_client_session(
url="http://localhost:8000/mcp",
sampling_callback=callback,
):
) as session:
# Session now supports sampling
pass
```