python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

`RuntimeError: Attempted to exit cancel scope in a different task` when cleaning up multiple MCPClient instances out-of-order

Open HMJiangGatech opened this issue 1 year ago • 11 comments

Describe the bug If two MCPClient objects are instantiated and cleaned up in non-FILO order (i.e., the first-created client is cleaned up before the second), teardown fails with a cascade of RuntimeError/CancelledError exceptions coming from anyio and mcp.client.stdio.

To Reproduce Minimal repro:

import os, asyncio, json
from typing import Optional
from contextlib import AsyncExitStack
from mcp import ClientSession, StdioServerParameters
from mcp.types import TextContent
from mcp.client.stdio import stdio_client

class MCPClient:
    def __init__(self, command: str, args: list[str], env: Optional[dict] = None):
        self.session: Optional[ClientSession] = None
        self.command, self.args, self.env = command, args, env
        self._cleanup_lock = asyncio.Lock()
        self.exit_stack: Optional[AsyncExitStack] = None

    async def connect_to_server(self):
        await self.cleanup()
        self.exit_stack = AsyncExitStack()

        server_params = StdioServerParameters(
            command=self.command, args=self.args, env=self.env
        )
        stdio_transport = await self.exit_stack.enter_async_context(stdio_client(server_params))
        self.stdio, self.write = stdio_transport
        self.session = await self.exit_stack.enter_async_context(
            ClientSession(self.stdio, self.write)
        )
        await self.session.initialize()

    async def cleanup(self):
        if self.exit_stack:
            async with self._cleanup_lock:
                await self.exit_stack.aclose()
                self.session = None
            self.exit_stack = None

async def main():
    cfg = {
        "command": "npx",
        "args": ["-y", "@adenot/mcp-google-search"],
        "env": {
            "GOOGLE_API_KEY": os.environ["GOOGLE_API_KEY"],
            "GOOGLE_SEARCH_ENGINE_ID": os.environ["GOOGLE_SEARCH_ENGINE_ID"],
        },
    }

    c1, c2 = MCPClient(**cfg), MCPClient(**cfg)
    await c1.connect_to_server()
    await c2.connect_to_server()

    # Works (FILO)
    # await c2.cleanup()
    # await c1.cleanup()

    # Fails (FIFO)
    await c1.cleanup()      # <-- boom
    await c2.cleanup()

if __name__ == "__main__":
    asyncio.run(main())

Expected behavior

cleanup() should succeed regardless of the order in which multiple MCPClient instances are closed, as long as each instance’s own exit_stack is intact. A single client ought to manage its own lifetime without depending on external FILO discipline.

Actual Traceback

RuntimeError: Attempted to exit cancel scope in a different task than it was entered in
...
asyncio.exceptions.CancelledError: Cancelled by cancel scope ...
...
RuntimeError: Attempted to exit a cancel scope that isn't the current task's current cancel scope

Environment

Item Version
mcp 1.6.0
Python 3.12.10
anyio 4.9.0
OS macOS 14.4 (Apple Silicon)

HMJiangGatech avatar Apr 24 '25 01:04 HMJiangGatech

Facing the same issue when connectng trying to use FastAPI and async methods with mcp to work with frontend The error simply like this and it starts at mcp libraries sse.py and flows down to below ``

~~~~~~~~~~~~~~~~~~~~~~~^^
File "/home/mukundan/projects/mcp-sse/.venv/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 773, in aexit
if self.cancel_scope.exit(type(exc), exc, exc.traceback):
~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/mukundan/projects/mcp-sse/.venv/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 456, in exit
raise RuntimeError(
...<2 lines>...
)
RuntimeError: Attempted to exit cancel scope in a different task than it was entered in```

mashriram avatar May 02 '25 15:05 mashriram

Faced the same issue. My custom context manager class was doing simple iteration of the clients list to attempt cleanup (FIFO). In line with the workaround detailed above, fixed with FILO:

e.g.

finally:
    # Clean up in the same task context
    # MUST BE FILO, FIFO fails
    # https://github.com/modelcontextprotocol/python-sdk/issues/577
    for client in reversed(self.clients):
        try:
            await client.cleanup()

pitta-bread avatar May 12 '25 10:05 pitta-bread

How to achieve free management of multiple MCPClient instances under such conditions, enabling instant exit and connection establishment

CuriousTank avatar May 21 '25 06:05 CuriousTank

Trying OpenAI Codex to fix the issue: https://chatgpt.com/s/cd_682dec27cf788191b2892b8097d45248

jhaoming-oai avatar May 21 '25 15:05 jhaoming-oai

any progress? why must follow the FIFO

vincent-pli avatar Jun 20 '25 01:06 vincent-pli

Hi @HMJiangGatech thanks for reporting this and providing a detailed repro.

felixweinberger avatar Sep 26 '25 10:09 felixweinberger

I think mcp.client.stdio.stdio_client should explicitly cancel its TaskGroup inside the context manager’s aexit (i.e., in the finally block) before shutting down the subprocess. That way, the background reader/writer tasks are cancelled by the same task that created the scope, avoiding cross-task scope exit.

Proposed diff:

 @asynccontextmanager
 async def stdio_client(server: StdioServerParameters, ...):
     ...
-    async with (anyio.create_task_group() as tg, process):
+    async with anyio.create_task_group() as tg, process:
         tg.start_soon(stdout_reader)
         tg.start_soon(stdin_writer)
         try:
             yield read_stream, write_stream
         finally:
-            # [Existing cleanup: close stdin, wait for process, kill if needed]
+            # Cancel background tasks before closing process to avoid cross-task cancellation
+            tg.cancel_scope.cancel()
+            # Proceed with process shutdown
             if process.stdin:
                 try:
                     await process.stdin.aclose()
                 except Exception:
                     pass
             try:
                 with anyio.fail_after(PROCESS_TERMINATION_TIMEOUT):
                     await process.wait()
             except TimeoutError:
                 await _terminate_process_tree(process)
             except ProcessLookupError:
                 pass

Since this is my first time leaving a comment on GitHub, I apologize in advance if I said anything awkward or inappropriate. Thanks

Ti1228 avatar Oct 11 '25 15:10 Ti1228

After running into the same problem, I did a deep investigation into this issue and wanted to share what I found.

TL;DR

This is a fundamental limitation of anyio's structured concurrency model when combined with AsyncExitStack. The error occurs because anyio enforces strict LIFO (Last-In-First-Out) cleanup order for cancel scopes, but AsyncExitStack allows arbitrary cleanup order.

Root Cause

When you create multiple MCP clients using AsyncExitStack:

  1. Each client creates a task group with an associated cancel scope
  2. These cancel scopes form a stack that anyio expects to be exited in LIFO order
  3. When you clean up clients in FIFO order (first-created first), you're trying to exit an "inner" cancel scope while an "outer" one is still active
  4. AnyIO detects this as "exiting a cancel scope in a different task than it was entered in" because the scope stack is out of order

Why the Suggested Fix Doesn't Work

@Ti1228's suggestion to add tg.cancel_scope.cancel() at the start of the finally block is good practice, but unfortunately doesn't solve this specific problem because:

  • The error occurs during the task group's __aexit__ method before our finally block completes
  • The issue is with the order of exiting the context managers themselves, not just the background tasks

I tested this approach extensively and the error persists.

This is By Design

After researching anyio's issue tracker, I found that this behavior is intentional. From the anyio maintainer (issue agronholm/anyio#345):

"Yield in general cannot be used in a task group context, which is a fundamental limitation of structured concurrency frameworks like AnyIO and Trio."

The combination of async generators (which stdio_client uses) and arbitrary cleanup order violates anyio's structured concurrency guarantees.

Workarounds

Since this is a limitation of the anyio framework rather than a bug in the MCP SDK, here are the recommended approaches:

Option 1: Ensure LIFO Cleanup (Recommended)

class MCPClient:
    # ... your existing code ...

async def main():
    cfg = {"command": "npx", "args": ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"]}

    clients = []
    c1 = MCPClient(**cfg)
    await c1.connect_to_server()
    clients.append(c1)

    c2 = MCPClient(**cfg)
    await c2.connect_to_server()
    clients.append(c2)

    # Do work...

    # Clean up in LIFO order (last-created first)
    for client in reversed(clients):
        await client.cleanup()

Option 2: Use Context Managers Directly

Instead of AsyncExitStack, use nested context managers which guarantee LIFO cleanup:

async def main():
    server_params1 = StdioServerParameters(...)
    server_params2 = StdioServerParameters(...)

    async with stdio_client(server_params1) as (read1, write1):
        async with ClientSession(read1, write1) as session1:
            await session1.initialize()

            async with stdio_client(server_params2) as (read2, write2):
                async with ClientSession(read2, write2) as session2:
                    await session2.initialize()

                    # Both clients available here
                    # They will automatically clean up in LIFO order

Option 3: Use Separate Event Loops

If you truly need independent cleanup order, run each client in its own async context:

async def run_client(cfg):
    async with AsyncExitStack() as stack:
        # Each client in its own context
        ...

await asyncio.gather(
    run_client(cfg1),
    run_client(cfg2)
)

Should This Be Fixed in the SDK?

The MCP SDK is working correctly within anyio's constraints. This is a documentation issue rather than a code bug. We should:

  1. Document this limitation in the SDK documentation
  2. Provide examples of proper multi-client management
  3. Consider adding a ClientManager helper class that enforces LIFO cleanup

Related Issues

  • MCP SDK #79: Similar issue with AsyncExitStack
  • anyio agronholm/anyio#345: Discussion of structured concurrency limitations
  • anyio agronholm/anyio#805: pytest fixtures with task groups

Hope this helps clarify what's happening!

cbcoutinho avatar Oct 18 '25 17:10 cbcoutinho

@cbcoutinho Thanks for looking into this! However, doesn't AsyncExitStack already clean up in LIFO order? ExitStack is explicitly documented at doing so, so I'd expect AsyncExitStack do be the same:

https://docs.python.org/3/library/contextlib.html#contextlib.ExitStack

Since registered callbacks are invoked in the reverse order of registration, this ends up behaving as if multiple nested with statements had been used with the registered set of callbacks.

In other words, it's (meant to be) equivalent to your "Option 2: Use Context Managers Directly".

DouweM avatar Oct 24 '25 13:10 DouweM

Howdy @DouweM,

I'm seeing this error specifically when generating multiple streamable_http_clients within pytest fixtures. These fixtures are not related and do not follow strictly LIFO - that's my best guess as to why this error crops up intermittently.

Indeed, you could nest all the clients manually one by one to enforce LIFO, but this was undesirable in my case. I've since switched to anyio and don't see the same problem when creating multiple clients concurrently in pytest fixtures.

cbcoutinho avatar Oct 24 '25 14:10 cbcoutinho

Hit this in a FastAPI app managing a pool of stdio MCP clients that get created/destroyed based on user sessions — the FIFO cleanup order was non-deterministic because different request handlers could release clients in any order.

The root cause is that anyio cancel scopes are task-local: a cancel scope entered in task A can only be exited in task A. AsyncExitStack preserves its own LIFO order internally, but when two independent AsyncExitStack instances are owned by two different objects, there's no cross-stack LIFO enforcement.

Two patterns that work reliably in production:

Option 1: Single shared exit stack for all clients (enforces LIFO automatically)

from contextlib import AsyncExitStack

async with AsyncExitStack() as stack:
    c1 = await stack.enter_async_context(MCPClient(...))
    c2 = await stack.enter_async_context(MCPClient(...))
    # stack.aclose() will clean up c2 then c1 — LIFO guaranteed

This works if the clients are owned by the same scope. For pools where lifetime varies, it's too rigid.

Option 2: anyio TaskGroup with one task per client

import anyio

async with anyio.create_task_group() as tg:
    tg.start_soon(run_client, c1)
    tg.start_soon(run_client, c2)
    # each client lives in its own task; cancel scopes don't cross

This is what @cbcoutinho found worked with pytest fixtures. The anyio task group manages cancellation per-task, so there's no cross-task cancel scope conflict.

For a FastAPI lifespan with a dynamic pool, I ended up using a asyncio.Queue to serialize client teardown through a single background task, which sidesteps the ordering issue entirely:

# clients always cleaned up from the same background task context
teardown_queue: asyncio.Queue = asyncio.Queue()

async def teardown_worker():
    while True:
        client = await teardown_queue.get()
        await client.cleanup()
        teardown_queue.task_done()

# When a session ends:
await teardown_queue.put(client)

Not elegant, but zero ordering assumptions.

Looking forward to the fix in #2484 landing so this is handled at the transport layer.

IgnazioDS avatar Jun 08 '26 11:06 IgnazioDS