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

`RuntimeError: Attempted to exit cancel scope in a different task than it was entered in` with AsyncExitStack

Open rectalogic opened this issue 2 years ago • 7 comments

Describe the bug I am pushing the stdio_client and ClientSession onto an AsyncExitStack to avoid needing to wrap everyting in with statements. My client will have multiple simultaneous instances of different ClientSessions, so I wanted a manager class to handle cleaning up the session when it is GC'd. My plan was to run a task in the manager classes __del__ method to clean up, e.g. asyncio.create_task(self.exit_stack.aclose()).

However this raises RuntimeError: Attempted to exit cancel scope in a different task than it was entered in

To Reproduce uv run this script:

# /// script
# requires-python = ">=3.10"
# dependencies = [
#     "mcp",
# ]
# ///

import asyncio
from contextlib import AsyncExitStack
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client


async def main() -> None:
    exit_stack = AsyncExitStack()
    server_params = StdioServerParameters(
        command="npx",
        args=["-y", "@modelcontextprotocol/server-filesystem", "/tmp"],
    )
    read, write = await exit_stack.enter_async_context(stdio_client(server_params))
    session = await exit_stack.enter_async_context(ClientSession(read, write))
    await session.initialize()

    asyncio.create_task(exit_stack.aclose())


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

Expected behavior Clean shutdown.

Logs

(node:63152) ExperimentalWarning: CommonJS module /opt/homebrew/lib/node_modules/npm/node_modules/debug/src/node.js is loading ES Module /opt/homebrew/lib/node_modules/npm/node_modules/supports-color/index.js using require().
Support for loading ES Module in require() is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
Secure MCP Filesystem Server running on stdio
Allowed directories: [ '/tmp' ]
unhandled exception during asyncio.run() shutdown
task: <Task finished name='Task-6' coro=<AsyncExitStack.aclose() done, defined at /Users/aw/.local/share/uv/python/cpython-3.10.15-macos-aarch64-none/lib/python3.10/contextlib.py:654> exception=RuntimeError('Attempted to exit cancel scope in a different task than it was entered in')>
Traceback (most recent call last):
  File "/Users/aw/Library/Caches/uv/archive-v0/bkm4-HGL-Ma8SIqOuzaOM/lib/python3.10/site-packages/mcp/client/stdio.py", line 128, in stdio_client
    yield read_stream, write_stream
  File "/Users/aw/.local/share/uv/python/cpython-3.10.15-macos-aarch64-none/lib/python3.10/contextlib.py", line 697, in __aexit__
    cb_suppress = await cb(*exc_details)
  File "/Users/aw/Library/Caches/uv/archive-v0/bkm4-HGL-Ma8SIqOuzaOM/lib/python3.10/site-packages/mcp/shared/session.py", line 122, in __aexit__
    return await self._task_group.__aexit__(exc_type, exc_val, exc_tb)
  File "/Users/aw/Library/Caches/uv/archive-v0/bkm4-HGL-Ma8SIqOuzaOM/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 769, in __aexit__
    if self.cancel_scope.__exit__(type(exc), exc, exc.__traceback__):
  File "/Users/aw/Library/Caches/uv/archive-v0/bkm4-HGL-Ma8SIqOuzaOM/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 433, in __exit__
    raise RuntimeError(
RuntimeError: Attempted to exit cancel scope in a different task than it was entered in

During handling of the above exception, another exception occurred:

  + Exception Group Traceback (most recent call last):
  |   File "/Users/aw/Library/Caches/uv/archive-v0/bkm4-HGL-Ma8SIqOuzaOM/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 763, in __aexit__
  |     raise BaseExceptionGroup(
  | exceptiongroup.ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "/Users/aw/Library/Caches/uv/archive-v0/bkm4-HGL-Ma8SIqOuzaOM/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 767, in __aexit__
    |     raise exc_val
    |   File "/Users/aw/Library/Caches/uv/archive-v0/bkm4-HGL-Ma8SIqOuzaOM/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 741, in __aexit__
    |     await _wait(self._tasks)
    |   File "/Users/aw/Library/Caches/uv/archive-v0/bkm4-HGL-Ma8SIqOuzaOM/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 706, in _wait
    |     await waiter
    | asyncio.exceptions.CancelledError
    |
    | During handling of the above exception, another exception occurred:
    |
    | Traceback (most recent call last):
    |   File "/Users/aw/Library/Caches/uv/archive-v0/bkm4-HGL-Ma8SIqOuzaOM/lib/python3.10/site-packages/mcp/client/stdio.py", line 128, in stdio_client
    |     yield read_stream, write_stream
    |   File "/Users/aw/.local/share/uv/python/cpython-3.10.15-macos-aarch64-none/lib/python3.10/contextlib.py", line 697, in __aexit__
    |     cb_suppress = await cb(*exc_details)
    |   File "/Users/aw/Library/Caches/uv/archive-v0/bkm4-HGL-Ma8SIqOuzaOM/lib/python3.10/site-packages/mcp/shared/session.py", line 122, in __aexit__
    |     return await self._task_group.__aexit__(exc_type, exc_val, exc_tb)
    |   File "/Users/aw/Library/Caches/uv/archive-v0/bkm4-HGL-Ma8SIqOuzaOM/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 769, in __aexit__
    |     if self.cancel_scope.__exit__(type(exc), exc, exc.__traceback__):
    |   File "/Users/aw/Library/Caches/uv/archive-v0/bkm4-HGL-Ma8SIqOuzaOM/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 433, in __exit__
    |     raise RuntimeError(
    | RuntimeError: Attempted to exit cancel scope in a different task than it was entered in
    +------------------------------------

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/aw/.local/share/uv/python/cpython-3.10.15-macos-aarch64-none/lib/python3.10/contextlib.py", line 656, in aclose
    await self.__aexit__(None, None, None)
  File "/Users/aw/.local/share/uv/python/cpython-3.10.15-macos-aarch64-none/lib/python3.10/contextlib.py", line 714, in __aexit__
    raise exc_details[1]
  File "/Users/aw/.local/share/uv/python/cpython-3.10.15-macos-aarch64-none/lib/python3.10/contextlib.py", line 697, in __aexit__
    cb_suppress = await cb(*exc_details)
  File "/Users/aw/.local/share/uv/python/cpython-3.10.15-macos-aarch64-none/lib/python3.10/contextlib.py", line 217, in __aexit__
    await self.gen.athrow(typ, value, traceback)
  File "/Users/aw/Library/Caches/uv/archive-v0/bkm4-HGL-Ma8SIqOuzaOM/lib/python3.10/site-packages/mcp/client/stdio.py", line 122, in stdio_client
    async with (
  File "/Users/aw/Library/Caches/uv/archive-v0/bkm4-HGL-Ma8SIqOuzaOM/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 769, in __aexit__
    if self.cancel_scope.__exit__(type(exc), exc, exc.__traceback__):
  File "/Users/aw/Library/Caches/uv/archive-v0/bkm4-HGL-Ma8SIqOuzaOM/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 433, in __exit__
    raise RuntimeError(
RuntimeError: Attempted to exit cancel scope in a different task than it was entered in

rectalogic avatar Nov 28 '24 01:11 rectalogic

Looks like an anyio issue and this is probably not the correct approach, closing.

rectalogic avatar Nov 28 '24 15:11 rectalogic

I met the same problem, how did you solve it

docshun avatar Mar 21 '25 14:03 docshun

I met the same problem, how did you solve it?

WikiHkz avatar Apr 01 '25 08:04 WikiHkz

I met the same problem, how did you solve it?

fuhuiking avatar Apr 04 '25 02:04 fuhuiking

Solve it by not doing this asyncio.create_task(exit_stack.aclose())

rectalogic avatar Apr 04 '25 14:04 rectalogic

I have the similar problems when trying to extract the session creation into a separate module.

Run it with python openai_function_calling_with_mcp.py.

# openai_function_calling_with_mcp.py
from mcp_client import McpClient
from openai import OpenAI
import os
import json
from dotenv import load_dotenv
import asyncio

load_dotenv()

# Initialize OpenAI client with environment variables
client = OpenAI(
    api_key=os.getenv("OPENAI_API_KEY"), base_url=os.getenv("OPENAI_BASE_URL")
)

async def run_conversation():
    messages = [
        {
            "role": "user",
            "content": "What's the weather in Shanghai and what's the current date and time?",
        }
    ]

    async with McpClient.get_mcp_client() as mcp_client:
        # Get available tools
        mcp_tools = await mcp_client.list_tools()
        tools = [
            {
                "type": "function",
                "function": {
                    "name": tool.name,
                    "description": tool.description,
                    "parameters": tool.inputSchema,
                },
            }
            for tool in mcp_tools.tools
        ]
        print(f"Available tools: {[t.name for t in mcp_tools.tools]}")

        response = client.chat.completions.create(
            model=os.getenv("OPENAI_MODEL_NAME"),
            messages=messages,
            tools=tools,
            tool_choice="auto",
        )

        # Handle function calling
        while response.choices[0].finish_reason == "tool_calls":
            tool_call = response.choices[0].message.tool_calls[0]
            function_name = tool_call.function.name
            arguments = json.loads(tool_call.function.arguments)

            # Call MCP tool
            function_response = await mcp_client.call_tool(
                name=function_name, arguments=arguments
            )

            messages.append(response.choices[0].message)
            messages.append(
                {
                    "role": "tool",
                    "content": function_response.content[0].text,
                    "tool_call_id": tool_call.id,
                }
            )

            response = client.chat.completions.create(
                model=os.getenv("OPENAI_MODEL_NAME"),
                messages=messages,
                tools=tools,
                tool_choice="auto",
            )

        print(response.choices[0].message.content)


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

# mcp_client.py
from contextlib import asynccontextmanager, AsyncExitStack
from types import TracebackType
from mcp import ClientSession, StdioServerParameters, stdio_client

class McpClient:
    def __init__(self, server_command="python", server_args=["mcp_server.py"]):
        self.server = StdioServerParameters(
            command=server_command,
            args=server_args
        )
        self._client_session = None
        self._exit_stack = None

    @asynccontextmanager
    async def initialize(self):
        """Initialize the client connection using AsyncExitStack"""
        # Not works, RuntimeError: Attempted to exit cancel scope in a different task than it was entered in
        self._exit_stack = AsyncExitStack()
        try:
            read, write = await self._exit_stack.enter_async_context(stdio_client(self.server))
            mcp_client = await self._exit_stack.enter_async_context(ClientSession(read, write))
            await mcp_client.initialize()
            self._client_session = mcp_client
            yield mcp_client
            # self._exit_stack.pop_all() # This line does not fix the issue too.
        except Exception as e:
            await self._exit_stack.aclose()
            
        # works without errors
        # async with AsyncExitStack() as stack:
        #     read, write = await stack.enter_async_context(stdio_client(self.server))
        #     mcp_client = await stack.enter_async_context(ClientSession(read, write))
        #     await mcp_client.initialize()
        #     self._client_session = mcp_client
        #     yield mcp_client

    async def call_tool(self, name, arguments):
        """Call a tool by name with given arguments"""
        if not self._client_session:
            raise RuntimeError("Client not initialized")
        return await self._client_session.call_tool(name=name, arguments=arguments)

    async def list_tools(self):
        """List available tools"""
        if not self._client_session:
            raise RuntimeError("Client not initialized")
        return await self._client_session.list_tools()

    @classmethod
    @asynccontextmanager
    async def get_mcp_client(cls):
        """Backward compatible context manager"""
        client = cls()
        async with client.initialize() as session:
          yield session

    
    async def __aexit__(
        self,
        exc_type: type[BaseException] | None,
        exc_val: BaseException | None,
        exc_tb: TracebackType | None,
    ) -> None:
        print("Exiting MCP Client")
        await self._exit_stack.aclose()

And If I changed initialize of mcp_client to use async with AsyncExitStack() as stack, then no problems.

liudonghua123 avatar Apr 07 '25 01:04 liudonghua123

Just checking if there are any plans to address the RuntimeError when exiting AsyncExitStack across tasks.

npkanaka avatar May 15 '25 01:05 npkanaka