From 0e1b95c5c4f14a37e3e7bb9670afadf69f39fd4d Mon Sep 17 00:00:00 2001 From: Kayvan Zahiri <123409429+Kayvan-Zahiri@users.noreply.github.com> Date: Mon, 19 Oct 2026 12:07:20 -0700 Subject: [PATCH] fix(waves): close streaming socket when initial send fails --- src/smallestai/waves/stream_tts.py | 6 +++--- tests/custom/test_waves_stream_tts_lifecycle.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/smallestai/waves/stream_tts.py b/src/smallestai/waves/stream_tts.py index 8f7a697b..b8023b22 100644 --- a/src/smallestai/waves/stream_tts.py +++ b/src/smallestai/waves/stream_tts.py @@ -190,10 +190,10 @@ def synthesize(self, text: str) -> Generator[bytes, None, None]: ws = self.ws assert ws is not None # _connect() raises unless the socket opened - payload = self._create_payload(text) - ws.send(json.dumps(payload)) - try: + payload = self._create_payload(text) + ws.send(json.dumps(payload)) + while True: if not self.error_queue.empty(): raise self.error_queue.get() diff --git a/tests/custom/test_waves_stream_tts_lifecycle.py b/tests/custom/test_waves_stream_tts_lifecycle.py index 1ecf3dd9..db049408 100644 --- a/tests/custom/test_waves_stream_tts_lifecycle.py +++ b/tests/custom/test_waves_stream_tts_lifecycle.py @@ -174,3 +174,18 @@ def sleep(self, _seconds): tts._connect() assert _only_socket().close_calls == 1 + + +@pytest.mark.parametrize("failure", [ConnectionResetError("send reset"), RuntimeError("send failed")]) +def test_synthesize_closes_the_socket_when_initial_send_raises(monkeypatch, failure): + def fail_send(self, payload): + raise failure + + monkeypatch.setattr(_FakeWebSocketApp, "send", fail_send) + tts = _tts() + with pytest.raises(type(failure)) as raised: + next(tts.synthesize("hello")) + assert raised.value is failure + assert _only_socket().close_calls == 1 + assert tts.ws is None + assert not tts.is_connected