-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Fix: race condition in update_chat_ctx deletes server-created function calls #4960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
StianHanssen
wants to merge
4
commits into
livekit:main
Choose a base branch
from
StianHanssen:fix-tool-sync-can-cause-item-loss
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+55
−5
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1c7c834
feat(realtime.py): add a protocol for realtime sessions with dual cha…
StianHanssen 0a9747d
feat(openai/realtime): add diff guard for inflight function calls
StianHanssen 1a3dadc
feat(agent_actiity.py): notify realtime sessions with dual chat conte…
StianHanssen d8b2f99
fix(agent_activity.py): ensure function call processed is always sent
StianHanssen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Incomplete cleanup of
_inflight_fc_idson interruption leaks stale entries that permanently block deletionWhen a speech is interrupted,
_notify_fc_processed(function_calls)is called at line 2683 to release all in-flight function call IDs. However,function_callsis populated by the_read_fnc_streamtask, which was already cancelled at line 2632 (await utils.aio.cancel_and_wait(*tasks)). This meansfunction_callsmay be incomplete — it might not include function calls whose IDs were already added to_inflight_fc_idsby the server'sconversation.item.addedevent.Root Cause: timing gap between server event and stream reader
The
_inflight_fc_idsset is populated in_handle_conversion_item_added(realtime_model.py:1510-1511) when the server sendsconversation.item.added(step 3 in the event sequence). However, a function call only entersfunction_callsafterresponse.output_item.done(step 8) pushes it tofunction_ch, and then_read_fnc_streamreads it from the tee.Two scenarios cause a leak:
Response cancelled before
response.output_item.done: The server creates the function_call item (step 3) but cancels the response before step 8. The function call never reachesfunction_ch, so it's never infunction_calls._read_fnc_streamcancelled before reading buffered items: Even if step 8 fires and the item is in the tee buffer,_read_fnc_streamis cancelled atagent_activity.py:2632before reading it.In both cases, the stale ID remains in
_inflight_fc_idsindefinitely. Every futureupdate_chat_ctxcall sees the function_call in_remote_chat_ctxbut not in local context, yet the_inflight_fc_idsguard (realtime_model.py:1189-1190) prevents deletion. The item permanently occupies space in the server's conversation context window.Impact: After an interruption during function call generation, orphaned function_call items can accumulate in the remote context and can never be cleaned up by
update_chat_ctx(e.g., during summarization), causing gradual context window waste.Prompt for agents
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes,
function_callscan be incomplete if_read_fnc_streamwas cancelled before consuming all items from the stream. However, this is expected and covered by a secondary clean-up path:_handle_conversion_item_deletedinrealtime_model.pycallsself._inflight_fc_ids.discard(event.item_id)whenever the server deletes an item. During interruption, the server typically deletes/truncates these items, which triggers the clean-up.In the worst case (server retains the item), the ID lingers in
_inflight_fc_ids, which preventsupdate_chat_ctxfrom deleting it, but I believe that is the correct behaviour, since the item legitimately exists on the server and deleting it would cause the cascade corruption this fix prevents.