Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/agents/realtime/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ async def close(self) -> None:
if cleanup_task is not None and (
current_task in self._guardrail_tasks or current_task in self._tool_call_tasks
):
# Cleanup is already waiting for this tracked task, so waiting here would form a cycle.
self._closing = True
self._closed = True

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid marking the session closed before cleanup finishes

When a cancelled guardrail/tool task re-enters close() while _cleanup_task is active, this marks _closed before _cleanup has finished cancelling background work and closing the model. Any other close() call in that window returns immediately without waiting, and if model.close() later fails the retry path is disabled because _closed remains true, leaving the transport unclosed. Keep this path as in-progress and let _cleanup mark the session closed after resources are coherent. .agents/references/realtime-session-lifecycle.mdL25-L26

Useful? React with 👍 / 👎.

raise asyncio.CancelledError

if cleanup_task is None:
Expand Down