Skip to content

fix: stop a closed task-mode RemoteA2aAgent from yielding release events - #7919

Open
hyeonsang010716 wants to merge 1 commit into
google:mainfrom
hyeonsang010716:fix/remote-a2a-agent-close-release
Open

hyeonsang010716 wants to merge 1 commit into
google:mainfrom
hyeonsang010716:fix/remote-a2a-agent-close-release

Conversation

@hyeonsang010716

Copy link
Copy Markdown
Contributor

Link to Issue or Description of Change

Problem:

In task mode, RemoteA2aAgent._run_async_impl yields the finish_task and
agent state events from its finally block. Most failure paths set
should_release_task_control before they yield the error event, so if the
generator is closed at that event, the finally block yields again and Python
raises RuntimeError: async generator ignored GeneratorExit.

The generator is closed there when the caller stops reading
Runner.run_async() at that event, with break or by cancelling the task. The
RuntimeError replaces the cancellation:

  • NodeRunner logs Node execution failed with exception, and
    on_agent_error_callback plugins receive the RuntimeError.
  • NodeRunner then enqueues an error event that nothing reads anymore, so the
    remote agent's node task waits forever and closing Runner.run_async()
    hangs. With abort_signal, the run closes but the log and callback still
    happen.

Solution:

Catch GeneratorExit in _run_async_impl, clear
should_release_task_control, and re-raise, so the finally block does not
yield while the generator is closing. The release events cannot reach anyone
once the caller has closed the stream, and nothing about them was persisted
before either. Events on every path that is not closed early are unchanged.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Added to tests/unittests/a2a/agent/test_remote_a2a_agent.py:

  • _run_async_impl in task mode can be closed at its error event, and does
    not set the agent state.
  • Through InMemoryRunner, breaking out of run_async() at a task mode
    remote agent's error event closes the run, and on_agent_error_callback is
    not called. It has a timeout, so it fails instead of hanging without the fix.

Both tests fail without the source change and pass with it.

$ pytest tests/unittests/a2a/agent/test_remote_a2a_agent.py -k "closes_at_error_event or stopping_run_at_task_error"
2 passed

$ pytest tests/unittests/a2a tests/unittests/agents tests/unittests/workflow tests/unittests/runners tests/unittests/test_runners.py tests/unittests/flows tests/unittests/plugins
6282 passed, 58 skipped, 5 xfailed

The full tests/unittests run has 14 failures, all in
integrations/vmaas/test_agent_engine_environment.py (#7778, macOS) and
test_release_dependencies.py. They fail the same way on main.

pre-commit run passes on the changed files. mypy reports the same 2 errors
in _remote_a2a_agent.py as on main, none from this change.

Manual End-to-End (E2E) Tests:

Ran the reproduction from the issue: a coordinator LlmAgent with a stub model
delegating to a task mode RemoteA2aAgent whose agent card URL refuses
connections. Without the fix, break at the remote agent's error event hangs
after logging the RuntimeError. With the fix, the run closes right away and
nothing is logged. Cancelling the consuming task instead of breaking hangs
without the fix and raises CancelledError with it.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules (N/A; no dependencies).

In task mode, _run_async_impl yields the finish_task and agent state events
from its finally block. Most failure paths set should_release_task_control
before they yield the error event, so closing the generator at that event made
the finally block yield again and raise "RuntimeError: async generator ignored
GeneratorExit".

That happens when a caller stops reading Runner.run_async() there. The
RuntimeError replaced the cancellation: NodeRunner logged it as a node failure,
on_agent_error_callback plugins received it, and the error event NodeRunner
then enqueued was never read, so closing the run hung.

Clear should_release_task_control on GeneratorExit so the finally block does
not yield while the generator is closing.

Fixes google#7918
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.

RemoteA2aAgent in task mode hangs Runner.run_async() when the caller stops at its error event

1 participant