From a1de5c62e92bd1aaa0d85f50752e3bac08b29a24 Mon Sep 17 00:00:00 2001 From: AAliKKhan Date: Thu, 23 Jul 2026 16:03:00 +0500 Subject: [PATCH] fix: mark realtime session as closed before CancelledError on cycle detection --- src/agents/realtime/session.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/agents/realtime/session.py b/src/agents/realtime/session.py index 66dc72be5b..def69cee86 100644 --- a/src/agents/realtime/session.py +++ b/src/agents/realtime/session.py @@ -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 raise asyncio.CancelledError if cleanup_task is None: