Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

fix(voice): avoid replaying compatible speech after lost responses

Rudy Celekli515a0b3d3cbc
diff --git a/app/services/voice.py b/app/services/voice.py
index d8433f82..f0a3ea0f 100644
--- a/app/services/voice.py
+++ b/app/services/voice.py
@@ -2576,6 +2576,18 @@ def _openai_compatible_tts(
text=text,
audio_duration_seconds=audio_duration,
)
+ except requests.exceptions.ConnectTimeout:
+ # No connection was established; retrying cannot replay synthesis.
+ logger.warning(f"{provider} tts could not connect, retrying")
+ except requests.exceptions.RequestException as exc:
+ # A lost response can follow successful remote synthesis. Keep the
+ # same acceptance boundary as hosted speech providers: do not POST
+ # again when the first request's outcome is unknown.
+ logger.error(
+ f"{provider} tts result is unconfirmed after a transport error; "
+ f"stop speech retries: {type(exc).__name__}"
+ )
+ return None
except Exception as e:
logger.error(f"{provider} tts failed: {str(e)}")
if response_accepted:
diff --git a/test/services/test_compatible_speech_transport.py b/test/services/test_compatible_speech_transport.py
new file mode 100644
index 00000000..2443bcfc
--- /dev/null
+++ b/test/services/test_compatible_speech_transport.py
@@ -0,0 +1,26 @@
+from unittest.mock import Mock
+
+import pytest
+import requests
+from app.services import voice
+
+
[email protected]("failure", [requests.ReadTimeout, requests.ConnectionError, requests.exceptions.ChunkedEncodingError])
+def test_ambiguous_compatible_speech_outcome_is_not_replayed(monkeypatch, tmp_path, failure):
+ post = Mock(side_effect=failure("response lost after synthesis"))
+ monkeypatch.setattr(voice.requests, "post", post)
+ target = tmp_path / "previous.mp3"
+ target.write_bytes(b"last good narration")
+ assert voice._openai_compatible_tts("test", "https://server.test/v1", "test-key", "model", "voice", "hello", 1, str(target)) is None
+ post.assert_called_once()
+ assert target.read_bytes() == b"last good narration"
+ assert list(tmp_path.iterdir()) == [target]
+
+
+def test_connect_timeout_remains_retryable(monkeypatch, tmp_path):
+ post = Mock(side_effect=requests.ConnectTimeout("connection not established"))
+ monkeypatch.setattr(voice.requests, "post", post)
+ target = tmp_path / "not-created.mp3"
+ assert voice._openai_compatible_tts("test", "https://server.test/v1", "test-key", "model", "voice", "hello", 1, str(target)) is None
+ assert post.call_count == 3
+ assert not target.exists()
diff --git a/test/services/test_kokoro.py b/test/services/test_kokoro.py
index a0298ed2..fc870e97 100644
--- a/test/services/test_kokoro.py
+++ b/test/services/test_kokoro.py
@@ -95,7 +95,7 @@ def test_transport_closes_audio_and_preserves_contract(monkeypatch, tmp_path, pr
@pytest.mark.parametrize("provider", ["kokoro", "chatterbox"])
[email protected]("failure", ["empty", "decode", "zero", "nan", "replace", "http", "timeout"])
[email protected]("failure", ["empty", "decode", "zero", "nan", "replace", "http", "timeout", "connect_timeout"])
def test_failed_audio_never_overwrites_output(monkeypatch, tmp_path, provider, failure):
"""失败保留原文件,解码资源与临时文件均释放,包括 Windows 替换失败。"""
output = tmp_path / "existing.mp3"
@@ -105,6 +105,8 @@ def test_failed_audio_never_overwrites_output(monkeypatch, tmp_path, provider, f
content=b"" if failure == "empty" else b"invalid audio", text="Unavailable"))
if failure == "timeout":
post.side_effect = requests.Timeout()
+ elif failure == "connect_timeout":
+ post.side_effect = requests.ConnectTimeout()
monkeypatch.setattr(voice.requests, "post", post)
clip = Mock(duration=0 if failure == "zero" else float("nan") if failure == "nan" else 1)
reader = Mock(return_value=clip)
@@ -118,14 +120,16 @@ def test_failed_audio_never_overwrites_output(monkeypatch, tmp_path, provider, f
) is None
assert output.read_bytes() == b"previous audio"
assert list(tmp_path.iterdir()) == [output]
- expected_attempts = 3 if failure in {"http", "timeout"} else 1
+ # An unspecified request timeout may occur after synthesis was accepted;
+ # only a proven pre-connection timeout is safe to submit again.
+ expected_attempts = 3 if failure in {"http", "connect_timeout"} else 1
assert post.call_count == expected_attempts
if failure in {"zero", "nan", "replace"}:
clip.close.assert_called_once()
-def test_transient_error_retries_successfully(monkeypatch, tmp_path):
- responses = [requests.Timeout(), SimpleNamespace(status_code=200, content=b"audio", text="")]
+def test_preconnection_error_retries_successfully(monkeypatch, tmp_path):
+ responses = [requests.ConnectTimeout(), SimpleNamespace(status_code=200, content=b"audio", text="")]
post = Mock(side_effect=responses)
monkeypatch.setattr(voice.requests, "post", post)
monkeypatch.setattr(voice, "AudioFileClip", Mock(return_value=Mock(duration=1)))