Skip to content

Conversation

@mozillazg
Copy link
Contributor

before

$ uv run cli.py test --task 'who are you' --filepath config.json 

INFO:root:Starting KAgent
INFO:kagent.adk._a2a:
>>> User Query: who are you
xxx
INFO:kagent.adk._a2a:  [Event] {xxxx}
ERROR:asyncio:unhandled exception during asyncio.run() shutdown
task: <Task finished name='Task-20' 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 "/xxx/kagent/python/.venv/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 776, in __aexit__
    raise exc_val
  File "/xxx/kagent/python/.venv/lib/python3.13/site-packages/mcp/client/streamable_http.py", line 498, in streamablehttp_client
    yield (
    ...<3 lines>...
    )
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/xxx/kagent/python/.venv/lib/python3.13/site-packages/mcp/client/streamable_http.py", line 474, in streamablehttp_client
    async with anyio.create_task_group() as tg:
               ~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/xxx/kagent/python/.venv/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 778, in __aexit__
    if self.cancel_scope.__exit__(type(exc), exc, exc.__traceback__):
       ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/xxx/kagent/python/.venv/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 457, in __exit__
    raise RuntimeError(
    ...<2 lines>...
    )
RuntimeError: Attempted to exit cancel scope in a different task than it was entered in

After

$ uv run cli.py test --task 'who are you' --filepath config.json 

INFO:root:Starting KAgent
INFO:kagent.adk._a2a:
>>> User Query: who are you
xxx
INFO:kagent.adk._a2a:  [Event] {xxx}
INFO:google_adk.google.adk.runners:Closing toolset: MCPToolset
INFO:google_adk.google.adk.runners:Successfully closed toolset: MCPToolset

## before

```
$ uv run cli.py test --task 'who are you' --filepath config.json 

INFO:root:Starting KAgent
INFO:kagent.adk._a2a:
>>> User Query: who are you
xxx
INFO:kagent.adk._a2a:  [Event] {xxxx}
ERROR:asyncio:unhandled exception during asyncio.run() shutdown
task: <Task finished name='Task-20' 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 "/xxx/kagent/python/.venv/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 776, in __aexit__
    raise exc_val
  File "/xxx/kagent/python/.venv/lib/python3.13/site-packages/mcp/client/streamable_http.py", line 498, in streamablehttp_client
    yield (
    ...<3 lines>...
    )
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/xxx/kagent/python/.venv/lib/python3.13/site-packages/mcp/client/streamable_http.py", line 474, in streamablehttp_client
    async with anyio.create_task_group() as tg:
               ~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/xxx/kagent/python/.venv/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 778, in __aexit__
    if self.cancel_scope.__exit__(type(exc), exc, exc.__traceback__):
       ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/xxx/kagent/python/.venv/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 457, in __exit__
    raise RuntimeError(
    ...<2 lines>...
    )
RuntimeError: Attempted to exit cancel scope in a different task than it was entered in
```

## After

```
$ uv run cli.py test --task 'who are you' --filepath config.json 

INFO:root:Starting KAgent
INFO:kagent.adk._a2a:
>>> User Query: who are you
xxx
INFO:kagent.adk._a2a:  [Event] {xxx}
INFO:google_adk.google.adk.runners:Closing toolset: MCPToolset
INFO:google_adk.google.adk.runners:Successfully closed toolset: MCPToolset
```



Signed-off-by: Huang Huang <[email protected]>
Copilot AI review requested due to automatic review settings October 20, 2025 05:54
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes an asyncio.exceptions.CancelledError that occurred when running the CLI test command due to improper cleanup of async resources. The fix ensures proper resource cleanup by adding a try/finally block to close runner connections when the test completes.

  • Wraps the async event iteration in a try/finally block
  • Adds proper cleanup logic to close runner resources if available
  • Includes error handling for cleanup operations to prevent secondary exceptions

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +142 to +143
if hasattr(runner, 'close'):
await runner.close()
Copy link

Copilot AI Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using hasattr for dynamic method checking is less reliable than using a proper protocol or interface. Consider defining a proper cleanup interface or checking for a specific type that guarantees the close method exists.

Suggested change
if hasattr(runner, 'close'):
await runner.close()
await runner.close()

Copilot uses AI. Check for mistakes.
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

Successfully merging this pull request may close these issues.

1 participant