Skip to content

Potential MCP issue (asyncio.exceptions.CancelledError)Β #2700

@Eddydpyl

Description

@Eddydpyl

Question

I've been having a recurring error, where running an Agent with MCP toolsets causes it to error, but it does not happen all the time. It happens very frequently when the Agent is run while another is already running, and they both use the same MCP toolset. That said, it also happens without that being the case, just less frequently.

Here's a stacktrace. Another thing that's suspicious, is that the Agent run is being called when another Agent's run is ending (in this particular case). Maybe one is closing the MCP connection, while the other is opening it, and that causes this issue?

Exception Group Traceback (most recent call last):
  |   File "/app/.venv/lib/python3.12/site-packages/opentelemetry/trace/__init__.py", line 589, in use_span
  |     yield span
  |   File "/app/.venv/lib/python3.12/site-packages/pydantic_graph/graph.py", line 259, in iter
  |     yield GraphRun[StateT, DepsT, RunEndT](
  |   File "/app/.venv/lib/python3.12/site-packages/pydantic_ai/agent/__init__.py", line 659, in iter
  |     yield agent_run
  |   File "/app/ocean/hooked/agent.py", line 258, in iter
  |     yield self._last_run
  |   File "/app/.venv/lib/python3.12/site-packages/pydantic_ai/agent/abstract.py", line 222, in run
  |     async for node in agent_run:
  |   File "/app/ocean/hooked/agent.py", line 366, in __anext__
  |     node = await self._apply_end_hooks(node)
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/app/ocean/hooked/agent.py", line 429, in _apply_end_hooks
  |     node = await hook.end_hook(ctx, node)
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/app/ocean/hooked/hooks/final_check.py", line 79, in end_hook
  |     return await self._validate_attachments(ctx, node)
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/app/ocean/hooked/hooks/final_check.py", line 120, in _validate_attachments
  |     validate_result = await validate_attachments(abridge_result.output, last_call.tool_args.get("attachments"), self.base_dir)
  |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/app/ocean/agents/validator.py", line 73, in validate_attachments
  |     return await agent.run(prompt, toolsets=[FunctionToolset(tools)] + excel.get_toolsets())
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/app/.venv/lib/python3.12/site-packages/pydantic_ai/agent/abstract.py", line 211, in run
  |     async with self.iter(
  |                ^^^^^^^^^^
  |   File "/usr/local/lib/python3.12/contextlib.py", line 210, in __aenter__
  |     return await anext(self.gen)
  |            ^^^^^^^^^^^^^^^^^^^^^
  |   File "/app/.venv/lib/python3.12/site-packages/pydantic_ai/agent/__init__.py", line 650, in iter
  |     async with toolset:
  |                ^^^^^^^
  |   File "/app/.venv/lib/python3.12/site-packages/pydantic_ai/toolsets/combined.py", line 56, in __aenter__
  |     await exit_stack.enter_async_context(toolset)
  |   File "/usr/local/lib/python3.12/contextlib.py", line 659, in enter_async_context
  |     result = await _enter(cm)
  |              ^^^^^^^^^^^^^^^^
  |   File "/app/.venv/lib/python3.12/site-packages/pydantic_ai/toolsets/combined.py", line 56, in __aenter__
  |     await exit_stack.enter_async_context(toolset)
  |   File "/usr/local/lib/python3.12/contextlib.py", line 659, in enter_async_context
  |     result = await _enter(cm)
  |              ^^^^^^^^^^^^^^^^
  |   File "/app/.venv/lib/python3.12/site-packages/pydantic_ai/mcp.py", line 244, in __aenter__
  |     async with AsyncExitStack() as exit_stack:
  |                ^^^^^^^^^^^^^^^^
  |   File "/usr/local/lib/python3.12/contextlib.py", line 754, in __aexit__
  |     raise exc_details[1]
  |   File "/usr/local/lib/python3.12/contextlib.py", line 737, in __aexit__
  |     cb_suppress = await cb(*exc_details)
  |                   ^^^^^^^^^^^^^^^^^^^^^^
  |   File "/usr/local/lib/python3.12/contextlib.py", line 231, in __aexit__
  |     await self.gen.athrow(value)
  |   File "/app/.venv/lib/python3.12/site-packages/pydantic_ai/mcp.py", line 512, in client_streams
  |     async with stdio_client(server=server) as (read_stream, write_stream):
  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/usr/local/lib/python3.12/contextlib.py", line 231, in __aexit__
  |     await self.gen.athrow(value)
  |   File "/app/.venv/lib/python3.12/site-packages/mcp/client/stdio/__init__.py", line 181, in stdio_client
  |     anyio.create_task_group() as tg,
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/app/.venv/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 772, in __aexit__
  |     raise BaseExceptionGroup(
  | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
  +-+---------------- 1 ----------------
    | Exception Group Traceback (most recent call last):
    |   File "/app/.venv/lib/python3.12/site-packages/mcp/client/stdio/__init__.py", line 187, in stdio_client
    |     yield read_stream, write_stream
    |   File "/app/.venv/lib/python3.12/site-packages/pydantic_ai/mcp.py", line 513, in client_streams
    |     yield read_stream, write_stream
    |   File "/usr/local/lib/python3.12/contextlib.py", line 737, in __aexit__
    |     cb_suppress = await cb(*exc_details)
    |                   ^^^^^^^^^^^^^^^^^^^^^^
    |   File "/app/.venv/lib/python3.12/site-packages/mcp/shared/session.py", line 218, in __aexit__
    |     return await self._task_group.__aexit__(exc_type, exc_val, exc_tb)
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "/app/.venv/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 772, in __aexit__
    |     raise BaseExceptionGroup(
    | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
    +-+---------------- 1 ----------------
      | Traceback (most recent call last):
      |   File "/app/.venv/lib/python3.12/site-packages/anyio/streams/memory.py", line 111, in receive
      |     return self.receive_nowait()
      |            ^^^^^^^^^^^^^^^^^^^^^
      |   File "/app/.venv/lib/python3.12/site-packages/anyio/streams/memory.py", line 106, in receive_nowait
      |     raise WouldBlock
      | anyio.WouldBlock
      | 
      | During handling of the above exception, another exception occurred:
      | 
      | Traceback (most recent call last):
      |   File "/app/.venv/lib/python3.12/site-packages/anyio/_core/_tasks.py", line 115, in fail_after
      |     yield cancel_scope
      |   File "/app/.venv/lib/python3.12/site-packages/pydantic_ai/mcp.py", line 256, in __aenter__
      |     await self._client.initialize()
      |   File "/app/.venv/lib/python3.12/site-packages/mcp/client/session.py", line 151, in initialize
      |     result = await self.send_request(
      |              ^^^^^^^^^^^^^^^^^^^^^^^^
      |   File "/app/.venv/lib/python3.12/site-packages/mcp/shared/session.py", line 272, in send_request
      |     response_or_error = await response_stream_reader.receive()
      |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      |   File "/app/.venv/lib/python3.12/site-packages/anyio/streams/memory.py", line 119, in receive
      |     await receive_event.wait()
      |   File "/app/.venv/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 1774, in wait
      |     await self._event.wait()
      |   File "/usr/local/lib/python3.12/asyncio/locks.py", line 212, in wait
      |     await fut
      | asyncio.exceptions.CancelledError: Cancelled by cancel scope 723888656570
      | 
      | During handling of the above exception, another exception occurred:
      | 
      | Traceback (most recent call last):
      |   File "/app/.venv/lib/python3.12/site-packages/pydantic_ai/mcp.py", line 255, in __aenter__
      |     with anyio.fail_after(self.timeout):
      |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      |   File "/usr/local/lib/python3.12/contextlib.py", line 158, in __exit__
      |     self.gen.throw(value)
      |   File "/app/.venv/lib/python3.12/site-packages/anyio/_core/_tasks.py", line 118, in fail_after
      |     raise TimeoutError
      | TimeoutError
      +------------------------------------

Additional Context

Might be related #2655

Metadata

Metadata

Assignees

Labels

StalequestionFurther information is requested

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions