Skip to content

Commit 2d460b6

Browse files
committed
fix: prevent race condition in listener iteration
Fixed RuntimeError when listeners list is modified during event emission. The _emit_event() method now copies the listeners list before iteration to prevent modification during async iteration. Scenario: 1. _emit_event() iterates listeners and awaits on_event() 2. During await, cleanup or other code calls remove_listener() 3. List modified during iteration causes RuntimeError Fix: Create a shallow copy of listeners list before iterating, allowing safe concurrent modifications without affecting ongoing iteration. Test results: All 23 tests in tests/realtime/test_openai_realtime.py passed
1 parent d1abf43 commit 2d460b6

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

src/agents/realtime/openai_realtime.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ def remove_listener(self, listener: RealtimeModelListener) -> None:
266266

267267
async def _emit_event(self, event: RealtimeModelEvent) -> None:
268268
"""Emit an event to the listeners."""
269-
for listener in self._listeners:
269+
# Copy list to avoid modification during iteration
270+
for listener in list(self._listeners):
270271
await listener.on_event(event)
271272

272273
async def _listen_for_messages(self):

0 commit comments

Comments
 (0)