Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The error "Attempted to exit a cancel scope that isn't the current task's current cancel scope" #35

Open
doncat99 opened this issue Feb 28, 2025 · 1 comment

Comments

@doncat99
Copy link

Error

Traceback (most recent call last):
  File "/Users/huangdon/Documents/universe-platform/warehouse/plan_agent/main.py", line 77, in <module>
    asyncio.run(main())
  File "/Users/huangdon/miniconda3/envs/universe/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/Users/huangdon/miniconda3/envs/universe/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/huangdon/miniconda3/envs/universe/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Users/huangdon/Documents/universe-platform/warehouse/plan_agent/main.py", line 31, in main
    async with MCPConnectionManager(context.server_registry):
  File "/Users/huangdon/Documents/universe-platform/libs/mcp-agent/src/mcp_agent/mcp/mcp_connection_manager.py", line 186, in __aexit__
    await self._tg.__aexit__(exc_type, exc_val, exc_tb)
  File "/Users/huangdon/miniconda3/envs/universe/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 778, in __aexit__
    return self.cancel_scope.__exit__(exc_type, exc_val, exc_tb)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/huangdon/miniconda3/envs/universe/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 464, in __exit__
    raise RuntimeError(
RuntimeError: Attempted to exit a cancel scope that isn't the current tasks's current cancel scope

Why is This Happening?

This error typically occurs when an async task group (_tg) is being exited (aexit) from a different task than the one that created it.

In your traceback:

The error originates from MCPConnectionManager.aexit():

async with MCPConnectionManager(context.server_registry):

This means something is trying to exit the async with block improperly.
Inside MCPConnectionManager.aexit(), it calls:

await self._tg.__aexit__(exc_type, exc_val, exc_tb)

Here, self._tg is an AnyIO task group (possibly anyio.create_task_group()).
aexit() is called from a different task than the one that started the group.
AnyIO detects that the cancel_scope is being exited from the wrong task, leading to:

raise RuntimeError("Attempted to exit a cancel scope that isn't the current task's current cancel scope")

How to Fix It

There are a few possible causes and fixes:

✅ 1️⃣ Ensure the Task Group is Created & Exited in the Same Task
If MCPConnectionManager creates a task group like this:

async with anyio.create_task_group() as tg:
    self._tg = tg  # Storing task group

Then make sure _tg.aexit() is called within the same task that created it.

@saqadri
Copy link
Collaborator

saqadri commented Mar 5, 2025

@doncat99 can you please provide repro steps on when this error occurs for you? Would love to debug it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants