Repository navigation
fix: stop a closed task-mode RemoteA2aAgent from yielding release events - #7919
Open
hyeonsang010716 wants to merge 1 commit into
Open
hyeonsang010716 wants to merge 1 commit into
hyeonsang010716 wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Link to Issue or Description of Change
Problem:
In
taskmode,RemoteA2aAgent._run_async_implyields thefinish_taskandagent state events from its
finallyblock. Most failure paths setshould_release_task_controlbefore they yield the error event, so if thegenerator is closed at that event, the
finallyblock yields again and Pythonraises
RuntimeError: async generator ignored GeneratorExit.The generator is closed there when the caller stops reading
Runner.run_async()at that event, withbreakor by cancelling the task. TheRuntimeErrorreplaces the cancellation:NodeRunnerlogsNode execution failed with exception, andon_agent_error_callbackplugins receive theRuntimeError.NodeRunnerthen enqueues an error event that nothing reads anymore, so theremote agent's node task waits forever and closing
Runner.run_async()hangs. With
abort_signal, the run closes but the log and callback stillhappen.
Solution:
Catch
GeneratorExitin_run_async_impl, clearshould_release_task_control, and re-raise, so thefinallyblock does notyield 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:
Added to
tests/unittests/a2a/agent/test_remote_a2a_agent.py:_run_async_implintaskmode can be closed at its error event, and doesnot set the agent state.
InMemoryRunner, breaking out ofrun_async()at ataskmoderemote agent's error event closes the run, and
on_agent_error_callbackisnot 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.
The full
tests/unittestsrun has 14 failures, all inintegrations/vmaas/test_agent_engine_environment.py(#7778, macOS) andtest_release_dependencies.py. They fail the same way onmain.pre-commit runpasses on the changed files. mypy reports the same 2 errorsin
_remote_a2a_agent.pyas onmain, none from this change.Manual End-to-End (E2E) Tests:
Ran the reproduction from the issue: a coordinator
LlmAgentwith a stub modeldelegating to a
taskmodeRemoteA2aAgentwhose agent card URL refusesconnections. Without the fix,
breakat the remote agent's error event hangsafter logging the
RuntimeError. With the fix, the run closes right away andnothing is logged. Cancelling the consuming task instead of breaking hangs
without the fix and raises
CancelledErrorwith it.Checklist