Fix fire-and-forget event tasks in RealtimeSession#3553
Open
kratos0718 wants to merge 1 commit into
Open
Conversation
Three error events were emitted with a bare asyncio.create_task(self._put_event(...)) whose result was discarded. asyncio only keeps a weak reference to a task, so these tasks could be garbage-collected before running, silently dropping the error events they were meant to deliver. Route them through a new _emit_event_soon helper that retains a strong reference in a tracking set until the task completes (mirroring the existing _guardrail_tasks and _tool_call_tasks pattern), and cancel any pending event tasks during cleanup. Adds regression tests.
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.
Summary
RealtimeSessionemitted three error events via a bareasyncio.create_task(self._put_event(...))whose result was discarded (in_on_guardrail_task_doneand_on_tool_call_task_done).asyncioonly keeps a weak reference to a task, so a fire-and-forget task can be garbage-collected before it runs (documented behavior). When that happens here, the error event it was meant to deliver is silently dropped — and these are exactly the paths that report guardrail/tool-call failures to the caller.The session already uses a keep-a-reference pattern for
_guardrail_tasksand_tool_call_tasks(a trackingset+add_done_callback(discard)). This change applies the same pattern to the event-emitting tasks via a small_emit_event_soonhelper, and cancels any still-pending event tasks during_cleanup.Test plan
tests/realtime/test_session_event_tasks.py:_emit_event_soonholds a strong reference while the task is pending, delivers the event to the queue, and releases the reference once done._cleanup_event_taskscancels pending event tasks.make format,make lintpass (ruff).Issue number
N/A — found via static analysis of fire-and-forget
create_taskusage.Checks
make lintandmake format