fix: mark realtime session as closed before CancelledError on cycle detection#3926
fix: mark realtime session as closed before CancelledError on cycle detection#3926AAliKKhan wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a1de5c62e9
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| ): | ||
| # Cleanup is already waiting for this tracked task, so waiting here would form a cycle. | ||
| self._closing = True | ||
| self._closed = True |
There was a problem hiding this comment.
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 👍 / 👎.
When RealtimeSession.close() detects that the current task is a tracked guardrail or tool call task, it raises CancelledError to avoid a cycle (the cleanup task is already waiting for this tracked task). However, it did not set _closed = True before raising, leaving the session in an inconsistent state where subsequent calls to close() would attempt cleanup again. The fix sets both _closing and _closed before raising.