Skip to content

fix: #3334 await realtime background tasks during cleanup#3335

Draft
Aphroq wants to merge 1 commit intoopenai:mainfrom
Aphroq:fix/realtime-await-cancelled-tasks
Draft

fix: #3334 await realtime background tasks during cleanup#3335
Aphroq wants to merge 1 commit intoopenai:mainfrom
Aphroq:fix/realtime-await-cancelled-tasks

Conversation

@Aphroq
Copy link
Copy Markdown
Contributor

@Aphroq Aphroq commented May 10, 2026

Summary

  • Await cancelled Realtime guardrail and tool-call background tasks during session cleanup before clearing their tracking sets.
  • Keep cleanup exception handling exercised with real asyncio tasks, and add a regression test that verifies cancelled tasks finish their finally blocks before _cleanup() returns.

Related: #1976 previously identified the missing await for Realtime guardrail task cleanup. This PR keeps that direction, also covers Realtime tool-call task cleanup, and adds regression coverage.

Test plan

  • uv run pytest tests/realtime/test_session.py::test_cleanup_awaits_cancelled_background_tasks tests/realtime/test_session_exceptions.py::TestSessionExceptions::test_exception_during_guardrail_processing
  • bash .agents/skills/code-change-verification/scripts/run.sh

Issue number

Closes #3334

Checks

  • I've added new tests (if relevant)
  • I've added/updated the relevant documentation
  • I've run make lint and make format
  • I've made sure tests pass

@Aphroq Aphroq changed the title Await realtime background tasks during cleanup fix: #3334 Await realtime background tasks during cleanup May 10, 2026
@Aphroq Aphroq changed the title fix: #3334 Await realtime background tasks during cleanup fix: #3334 await realtime background tasks during cleanup May 10, 2026
@Aphroq
Copy link
Copy Markdown
Contributor Author

Aphroq commented May 10, 2026

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4f50cdfb6b

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/agents/realtime/session.py Outdated
task.cancel()
self._tool_call_tasks.clear()

await asyncio.gather(*tasks, return_exceptions=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 Bound cleanup waits for cancelled user tasks

When a realtime tool call is running user code that suppresses or delays CancelledError, close()/__aexit__ now waits indefinitely here before closing the model connection or waking event iterators. Tool functions are arbitrary async callables, so a cancellation-resistant tool can make an async with RealtimeSession(...) hang during shutdown; the guardrail cleanup uses the same unbounded gather pattern. Consider bounding the wait or otherwise ensuring cleanup can finish even when a background task does not promptly exit after cancellation.

Useful? React with 👍 / 👎.

@seratch seratch marked this pull request as draft May 10, 2026 22:07
@Aphroq Aphroq force-pushed the fix/realtime-await-cancelled-tasks branch from 4f50cdf to f04563e Compare May 11, 2026 04:53
@github-actions github-actions Bot added the bug Something isn't working label May 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working feature:realtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Realtime cleanup should await cancelled background tasks

1 participant