Skip to content

fix(realtime): await cancelled guardrail and tool-call tasks during cleanup#3764

Closed
Diwak4r wants to merge 1 commit into
openai:mainfrom
Diwak4r:fix/realtime-await-cancelled-tasks
Closed

fix(realtime): await cancelled guardrail and tool-call tasks during cleanup#3764
Diwak4r wants to merge 1 commit into
openai:mainfrom
Diwak4r:fix/realtime-await-cancelled-tasks

Conversation

@Diwak4r

@Diwak4r Diwak4r commented Jul 9, 2026

Copy link
Copy Markdown

Summary

RealtimeSession._cleanup() cancels pending guardrail and tool-call background tasks but never awaited them, so cleanup could return before the tasks' finally blocks executed. The tracking sets (_guardrail_tasks, _tool_call_tasks) were also cleared before cancellation took effect, which could drop references to in-flight tasks.

This makes _cleanup_guardrail_tasks and _cleanup_tool_call_tasks async: they now collect the not-yet-done tasks, clear the tracking set first, cancel the tasks, and await asyncio.gather(..., return_exceptions=True) so each task's finally block runs and the cancellation is observed before _cleanup returns.

Fixes #3334

Test plan

  • Added tests/realtime/test_session.py::test_cleanup_awaits_cancelled_guardrail_and_tool_call_tasks: seeds two never-returning background tasks into the tracking sets, calls _cleanup(), and asserts both tasks are done, the finished event is set (proving the finally blocks ran), and the tracking sets are empty.
  • Verified the test fails against the old (sync) implementation and passes with the fix.
  • uv run python -m pytest tests/realtime/test_session.py — full suite passes (111 passed), no regressions.

Note: this repo does not require DCO sign-off, so the commit is not -s.

…leanup

RealtimeSession._cleanup() cancelled pending guardrail and tool-call
background tasks but neither awaited them nor deferred clearing the
tracking sets, so cleanup could return before the tasks' finally blocks
ran. Clear the tracking sets before cancelling and await the cancelled
tasks with return_exceptions=True so their finally blocks run before
cleanup returns.

Fixes openai#3334
Copilot AI review requested due to automatic review settings July 9, 2026 07:41

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@seratch seratch added duplicate This issue or pull request already exists feature:realtime labels Jul 9, 2026
@seratch

seratch commented Jul 9, 2026

Copy link
Copy Markdown
Member

Thanks again for your interest here. The issue was resolved by #3767

@seratch seratch closed this Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

duplicate This issue or pull request already exists feature:realtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Realtime cleanup should await cancelled background tasks

3 participants