Skip to content

The agent that grows with you

fix(voice): retry a timed-out audio input stream start once

teknium150e5ffa34643
diff --git a/tests/tools/test_voice_mode.py b/tests/tools/test_voice_mode.py
index be9f24f6de3..e270973e1d5 100644
--- a/tests/tools/test_voice_mode.py
+++ b/tests/tools/test_voice_mode.py
@@ -1068,6 +1068,38 @@ class TestStreamLeakOnStartFailure:
mock_stream.close.assert_called_once()
+class TestStreamStartTimeoutRetry:
+ """PortAudio paTimedOut (-9987) on a cold bridge: retry the open once (#109303)."""
+
+ def test_timed_out_start_retries_once_and_succeeds(self, mock_sd):
+ cold = MagicMock()
+ cold.start.side_effect = OSError("Error starting stream: Wait timed out [PaErrorCode -9987]")
+ warm = MagicMock()
+ mock_sd.InputStream.side_effect = [cold, warm]
+
+ from tools.voice_mode import AudioRecorder
+ recorder = AudioRecorder()
+ recorder._ensure_stream()
+
+ assert recorder._stream is warm
+ cold.close.assert_called_once()
+ warm.close.assert_not_called()
+
+ def test_persistent_timeout_raises_after_second_attempt(self, mock_sd):
+ mock_stream = MagicMock()
+ mock_stream.start.side_effect = OSError("Wait timed out [PaErrorCode -9987]")
+ mock_sd.InputStream.return_value = mock_stream
+
+ from tools.voice_mode import AudioRecorder
+ recorder = AudioRecorder()
+ with pytest.raises(RuntimeError, match="Wait timed out"):
+ recorder._ensure_stream()
+
+ assert mock_sd.InputStream.call_count == 2
+ assert mock_stream.close.call_count == 2
+ assert recorder._stream is None
+
+
# ============================================================================
# listen_for_speech — VAD barge-in monitor
# ============================================================================
diff --git a/tools/voice_mode.py b/tools/voice_mode.py
index 89c7d5118c9..b420132774f 100644
--- a/tools/voice_mode.py
+++ b/tools/voice_mode.py
@@ -729,16 +729,25 @@ class AudioRecorder(_RecorderBase):
self._on_audio_block(np, indata)
stream = None
- try: # may block on CoreAudio (first call only)
- stream = sd.InputStream(samplerate=self._sample_rate, channels=CHANNELS, dtype=DTYPE,
- callback=_callback)
- stream.start()
- except Exception as e:
- with suppress(Exception):
- stream.close()
- raise RuntimeError(
- f"Failed to open audio input stream: {e}. "
- "Check that a microphone is connected and accessible.") from e
+ for attempt in range(2):
+ try: # may block on CoreAudio (first call only)
+ stream = sd.InputStream(samplerate=self._sample_rate, channels=CHANNELS, dtype=DTYPE,
+ callback=_callback)
+ stream.start()
+ break
+ except Exception as e:
+ with suppress(Exception):
+ stream.close()
+ stream = None
+ # PortAudio paTimedOut (-9987): a cold host-API bridge (WSLg ALSA->Pulse
+ # with a SUSPENDED RDP source) missed the 1 s thread-start window. The
+ # failed open itself wakes the bridge, so one immediate retry succeeds
+ # where the user's second key press would have (#109303).
+ if attempt or "timed out" not in str(e).lower():
+ raise RuntimeError(
+ f"Failed to open audio input stream: {e}. "
+ "Check that a microphone is connected and accessible.") from e
+ logger.info("Audio input stream start timed out; retrying once")
self._stream = stream
def start(self, on_silence_stop=None) -> None: