Uh oh!
There was an error while loading. Please reload this page.
fix(live): stop background tool tasks when a live agent run ends - #6769
Merged
Conversation
Streaming tools and non-blocking tools run as background asyncio tasks that nothing owns: only an explicit `stop_streaming` call ever cancelled one. When a live agent run ended for any other reason -- a handoff to another agent, `task_completed`, the model closing the connection, the caller walking away -- its tools kept running, feeding function responses into a live request queue that by then belonged to another agent, which never made those calls, or to nobody at all. The tasks were never awaited either, so a failure in one surfaced only as a stray asyncio warning. `BaseLlmFlow.run_live` now cancels the background tools its run started before it returns, and at a handoff it cancels them before the sub agent takes over the live request queue rather than when the enclosing run eventually finishes. Cancellation is best effort: a task that ignores it is logged instead of stalling the handoff or the caller's teardown. Stopped tools also give up their registry entry. `_send_to_model` copies every live request into each registered stream, so a stream left behind by a tool that is no longer reading kept growing for the rest of the session, one entry per audio chunk the user spoke. Co-authored-by: Liang Wu <wuliang@google.com> PiperOrigin-RevId: 964915958
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 freeto 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.
Summary
Backport of commit 0088abb (
fix(live): stop background tool tasks when a live agent run ends, PiperOrigin-RevId: 964915958) frommaintov1.Streaming tools and non-blocking tools run as background asyncio tasks that nothing owns: only an explicit
stop_streamingcall ever cancelled one. When a live agent run ended for any other reason -- a handoff to another agent,task_completed, the model closing the connection, the caller walking away -- its tools kept running, feeding function responses into a live request queue that by then belonged to another agent, which never made those calls, or to nobody at all. The tasks were never awaited either, so a failure in one surfaced only as a stray asyncio warning.BaseLlmFlow.run_livenow cancels the background tools its run started before it returns, and at a handoff it cancels them before the sub agent takes over the live request queue rather than when the enclosing run eventually finishes. Cancellation is best effort: a task that ignores it is logged instead of stalling the handoff or the caller's teardown.Stopped tools also give up their registry entry.
_send_to_modelcopies every live request into each registered stream, so a stream left behind by a tool that is no longer reading kept growing for the rest of the session, one entry per audio chunk the user speaks.Testing Plan
pytest tests/unittests/streaming/test_live_tool_shutdown.py tests/unittests/streaming/test_streaming.py(24 passed)pytest tests/unittests/flows/(426 passed)pyinkandisortclean.