Handle CLI request errors and backend whisper timeout
Some checks failed
CI / Backend (pull_request) Failing after 15s
CI / CLI (pull_request) Successful in 28s

This commit is contained in:
2026-05-27 13:08:56 +00:00
parent 44af756bd3
commit 1c6415d306
4 changed files with 67 additions and 10 deletions

View File

@@ -17,6 +17,7 @@ CONTENT_TYPES = {
}
app = FastAPI(title="whisper-remote-backend")
WHISPER_PROCESS_TIMEOUT_SECONDS = 300
def validate_output_format(output_format: str) -> str:
@@ -112,12 +113,21 @@ async def transcribe(
check=False,
capture_output=True,
text=True,
timeout=WHISPER_PROCESS_TIMEOUT_SECONDS,
)
except FileNotFoundError as exc:
raise HTTPException(
status_code=500,
detail="The 'whisper' CLI was not found on PATH on the backend host.",
) from exc
except subprocess.TimeoutExpired as exc:
raise HTTPException(
status_code=504,
detail=(
"Whisper CLI timed out after "
f"{WHISPER_PROCESS_TIMEOUT_SECONDS}s and was terminated."
),
) from exc
if completed.returncode != 0:
detail = completed.stderr.strip() or completed.stdout.strip() or "Whisper CLI failed."

View File

@@ -64,3 +64,22 @@ def test_transcriptions_maps_subprocess_failure(monkeypatch) -> None:
assert response.status_code == 502
assert response.json()["detail"] == "bad whisper day"
def test_transcriptions_maps_subprocess_timeout(monkeypatch) -> None:
def fake_run(command: list[str], check: bool, capture_output: bool, text: bool, timeout: int):
raise server.subprocess.TimeoutExpired(cmd=command, timeout=timeout)
monkeypatch.setattr(server.subprocess, "run", fake_run)
response = client.post(
"/transcriptions",
data={"model": "base", "output_format": "txt"},
files={"file": ("clip.wav", b"audio", "audio/wav")},
)
assert response.status_code == 504
assert (
response.json()["detail"]
== f"Whisper CLI timed out after {server.WHISPER_PROCESS_TIMEOUT_SECONDS}s and was terminated."
)