---
type: email
from: Lucas Wang <notifications@github.com>
subject: [openai/openai-agents-python] fix: await cancelled websocket task to

 prevent resource leak (PR #1955)
received: 2026-03-06 08:10:53
date_header: Mon, 20 Oct 2025 22:36:02 -0700
priority: normal
status: awaiting_approval
uid: 33209
---

## Email Content

**From:** Lucas Wang <notifications@github.com>
**Subject:** [openai/openai-agents-python] fix: await cancelled websocket task to

 prevent resource leak (PR #1955)
**Date:** Mon, 20 Oct 2025 22:36:02 -0700

### Body (preview)

## Summary

Fixed a resource leak in the `OpenAIRealtimeWebSocketModel.close()` method where the websocket task was cancelled but not awaited, potentially leaving dangling resources.



## Problem

When `close()` was called, the code would:

1. Cancel `self._websocket_task`

2. Immediately set it to `None`



This could lead to:

- Resource leaks (unclosed connections, file descriptors)

- Python warnings: &quot;Task was destroyed but it is pending!&quot;

- Memory leaks from unreleased task resources



## Solution

Now properly awaits the cancelled task with exception handling:

```python

self._websocket_task.cancel()

try:

    await self._websocket_task

except asyncio.CancelledError:

    pass

self._websocket_task = None

```



This ensures the task is fully cleaned up before being discarded.



## Testing

- All 23 existing tests in `tests/realtime/test_openai_realtime.py` pass

- No behavioral changes, only proper cleanup



## Impact

- Prevents resource leaks in realtime sessions

- Eliminates potential &quot;pending task&quot; warnings

- Follows asyncio best practices for task cancellation

You can view, comment on, or merge this pull request online at:



  https://github.com/openai/openai-agents-python/pull/1955



-- Commit Summary --



  * fix: await cancelled websocket task to prevent resource leak



-- File Changes --



    M src/agents/realtime/openai_realtime.py (4)



-- Patch Links --



https://github.com/openai/openai-agents-python/pull/1955.patch

https://github.com/openai/openai-agents-python/pull/1955.diff



-- 

Reply to this email directly or view it on GitHub:

https://github.com/openai/openai-agents-python/pull/1955

You are receiving this because you are subscribed to this thread.



Message ID: &lt;openai/openai-agents-python/pull/1955@github.com&gt;

---

## Suggested Actions
- [ ] Reply
- [ ] Archive
- [ ] Escalate
