Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

RuntimeError but the actual root cause is not logged correctly #743

Copy link
Copy link
Open
@Shades-en

Description

@Shades-en
Issue body actions

Describe the bug
Suppose we have an AsyncContext manager inside an mcp server and some logic is written to manually close the context. However if we encounter an exception before it reaches this context closure part of the code.
We get RuntimeError: Attempted to exit cancel scope in a different task than it was entered in

The issue is that I will never know what is the exception that caused this. There is no traceback to the original exception. This is because the AsyncExitStack was not closed, the error resulting from it overshadows the actual Exception that caused it.

The MCP server times out because of this and never returns any response.

To Reproduce
Steps to reproduce the behavior:

  1. Use the google-adk to create 2 agents, one of them using MCP tools.
  2. Let the agent using MCP tools (here, figma agent using figma-developer-mcp) be created dynamically and the other agent predefined.
  3. close the AsyncExitStack for the MCP tools at the end.
  4. Create an stdio mcp server and enclose the logic in step 1 to 3 in an mcp tool
  5. Use mcp inspector to connect to the server. Use the tool the first time it works properly. Use it the second time you will get the RuntimeError: Attempted to exit cancel scope in a different task than it was entered in in logs and server will time out.
  6. The actual error is due to 1 validation error for SequentialAgent Value error, Agent 'CodeAgent' already has a parent agent, current parent: 'FigmaToCodePipelineAgent', trying to add: 'FigmaToCodePipelineAgent'[type=value_error, input_value={'name': 'FigmaToCodePipe...ng shadcn components'}, input_type=dict] For further information visit https://errors.pydantic.dev/2.11/v/va lue_error

Expected behavior
If I did not use any MCP tools (figma-developer-mcp) inside my MCP server then in the mcp inspector it outputs the correct error mentioned in point 6 above.
I would expect the same traceback even in this case or atleast in the log generated to have some reference to this trace back

Screenshots

Image

Additional context
The complete error log as copied from inspector and formatted -

ERROR:Task exception was never retrieved
Future: <Task finished name='Task-35' coro=<<async_generator_athrow without name>()> exception=RuntimeError('Attempted to exit cancel scope in a different task than it was entered in')>

Traceback (most recent call last):
File "/..../figma-backyard/.venv/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 772, in aexit
...
raise BaseExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)

┌────────────── Sub-exception #1 ───────────────┐
│ Traceback (most recent call last): │
│ File "/..../figma-backyard │
│ /.venv/lib/python3.11/site-packages/ │
│ mcp/client/stdio/init.py", line 177, in stdio_client │
│ yield │
│ GeneratorExit │
└──────────────────────────────────────────────┘

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/..../figma-backyard/.venv/lib/python3.11/site-packages/mcp/client/stdio/init.py", line 170, in stdio_client
async with (
anyio.create_task_group() as tg,
):
File "/..../figma-backyard/.venv/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 778, in aexit
if self._cancel_scope._entered_by != current_task():
raise RuntimeError(
"Attempted to exit cancel scope in a different task than it was entered in"
)
File "/..../.venv/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 457, in exit
raise RuntimeError(
"Attempted to exit cancel scope in a different task than it was entered in"
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.