Skip to content
Draft
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: 3 additions & 3 deletions src/smallestai/waves/stream_tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
15 changes: 15 additions & 0 deletions tests/custom/test_waves_stream_tts_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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