fix: realtime agent handoff race conditions #4746
Open
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
update_agent()triggers activity transition, the old code continued usingself._rt_sessionfor chat context updates and reply generation. Sinceupdate_agent()closes the old realtime session, this caused errors and broken agent transfers. Added earlyreturnto skip post-handoff processing.manual_function_calls=Trueso the framework controls tool execution flow rather than Gemini auto-executing, preventing race conditions during agent handoff where tool responses could be sent to the wrong session.Context
These bugs were discovered while building a multi-agent example with Gemini Realtime API. Transferring from main agent to a specialist worked, but transferring back consistently failed because:
AgentHandoff, triggeringupdate_agent()which starts closing the old realtime sessionself._rt_session.update_chat_ctx()on the closing sessionTest plan
manual_function_calls=True)