Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/agents/voice/models/openai_stt.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ async def _wait_for_event(
"""
Wait for an event from event_queue whose type is in expected_types within the specified timeout.
"""
start_time = time.time()
# Wall-clock adjustments can move a deadline forwards or backwards. Timeout
# accounting must use a monotonic clock instead.
start_time = time.monotonic()
while True:
remaining = timeout - (time.time() - start_time)
remaining = timeout - (time.monotonic() - start_time)
if remaining <= 0:
raise TimeoutError(f"Timeout waiting for event(s): {expected_types}")
evt = await asyncio.wait_for(event_queue.get(), timeout=remaining)
Expand Down
19 changes: 15 additions & 4 deletions tests/voice/test_openai_stt.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
ErrorSentinel,
WebsocketDoneSentinel,
_audio_buffer_to_base64,
_wait_for_event,
)

from .pipeline_test_models import StreamedAudioInputFactory
Expand All @@ -55,6 +56,16 @@ def create_mock_websocket(messages: list[str]) -> AsyncMock:
return mock_ws


@pytest.mark.asyncio
async def test_wait_for_event_returns_matching_event() -> None:
queue: asyncio.Queue[dict[str, str]] = asyncio.Queue()
await queue.put({"type": "session.created"})

event = await _wait_for_event(queue, ["session.created"], timeout=1)

assert event == {"type": "session.created"}


def fake_time(increment: int):
current = 1000
while True:
Expand Down Expand Up @@ -579,8 +590,8 @@ async def test_timeout_waiting_for_created_event(monkeypatch):
def fake_time_func():
return next(time_gen)

# Monkey-patch time.time with our fake_time_func
monkeypatch.setattr(time, "time", fake_time_func)
# Monkey-patch the monotonic clock used for event deadlines.
monkeypatch.setattr(time, "monotonic", fake_time_func)

mock_ws = create_mock_websocket(
[
Expand Down Expand Up @@ -758,12 +769,12 @@ async def test_inactivity_timeout():
)

# We'll artificially manipulate the "time" to simulate inactivity quickly.
# The code checks time.time() for inactivity over EVENT_INACTIVITY_TIMEOUT.
# The code checks time.monotonic() for inactivity over EVENT_INACTIVITY_TIMEOUT.
# We'll increment the return_value manually.
with (
patch("websockets.connect", return_value=mock_ws),
patch(
"time.time",
"time.monotonic",

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Keep the monotonic mock away from the event-loop clock

Patching process-global time.monotonic also replaces the clock used by asyncio.BaseEventLoop.time(). During this async test, event-loop scheduling consumes the five-value side_effect and can raise StopIteration, strand pending tasks, or expire asyncio.wait_for independently of the STT deadline, so the test is failing or passing for the wrong reason. Mock only an STT-local clock dependency, or otherwise preserve the loop's real clock.

AGENTS.md reference: AGENTS.md:L200-L200

Useful? React with 👍 / 👎.

side_effect=[
1000.0,
1000.0 + EVENT_INACTIVITY_TIMEOUT + 1,
Expand Down