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

RuntimeError but the actual root cause is not logged correctly

Open Shades-en opened this issue 1 year ago • 1 comments

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" )

Shades-en avatar May 17 '25 11:05 Shades-en

Could you provide a minimal runnable example to reproduce your issue?

felixweinberger avatar Oct 03 '25 15:10 felixweinberger