Skip to content

Conversation

gn00295120
Copy link
Contributor

Summary

Fixed a race condition in OpenAIRealtimeWebSocketModel._emit_event() where the listeners list could be modified during async iteration, causing RuntimeError: list changed size during iteration.

Problem

The _emit_event() method iterates over self._listeners while calling await listener.on_event(event):

for listener in self._listeners:  # Iteration
    await listener.on_event(event)  # Await point - yields control

During the await, other code (e.g., cleanup) can call remove_listener(), modifying the list mid-iteration.

Crash scenario:

  1. Session emits event → _emit_event() starts iterating listeners
  2. At await listener.on_event(), control returns to event loop
  3. Cleanup runs → calls remove_listener() → modifies list
  4. Control returns to iteration → RuntimeError

This is realistic because remove_listener() is called during session cleanup (session.py:761).

Solution

Create a shallow copy of the listeners list before iteration:

for listener in list(self._listeners):  # Copy prevents modification issues
    await listener.on_event(event)

This allows safe concurrent modifications without affecting the ongoing iteration.

Testing

  • All 23 existing tests in tests/realtime/test_openai_realtime.py pass
  • No behavioral changes, only defensive coding

Impact

  • Prevents crashes during concurrent listener management
  • Common in cleanup scenarios where session removes itself as listener
  • Minimal performance impact (shallow copy of small list)

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
@Copilot Copilot AI review requested due to automatic review settings October 21, 2025 05:37
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a race condition in the event emission mechanism where concurrent listener list modifications during async iteration could cause runtime errors. The fix creates a snapshot of listeners before iteration to prevent RuntimeError: list changed size during iteration when listeners are removed during event processing.

Key changes:

  • Added defensive copying in _emit_event() to prevent race condition when listeners list is modified during async iteration
  • Added explanatory comment documenting the purpose of the list copy

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@seratch
Copy link
Member

seratch commented Oct 21, 2025

@codex review

Copy link
Member

@seratch seratch left a comment

Choose a reason for hiding this comment

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

This is a kind of edge case improvement but looks good to me

@chatgpt-codex-connector
Copy link

Codex Review: Didn't find any major issues. Swish!

ℹ️ 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".

@chatgpt-codex-connector
Copy link

Codex Review: Didn't find any major issues. Keep them coming!

ℹ️ 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".

@seratch seratch merged commit 9db9c01 into openai:main Oct 21, 2025
5 checks passed
@gn00295120 gn00295120 deleted the fix/realtime-listeners-race-condition branch October 23, 2025 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants