fix(cli): improve TTS and model download reliability#1187
Open
miguel-heygen wants to merge 1 commit into
Open
Conversation
Three changes to make TTS (and all model downloads) more reliable: 1. download.ts: add retry with exponential backoff (3 retries), socket and response timeouts, redirect loop protection, and specific HTTP 403/429 (rate limit) error messages. This fixes the primary failure mode: GitHub Releases rate-limiting model downloads at scale. 2. tts/manager.ts: add file-level locking (.lock files) for model and voice downloads to prevent concurrent processes from racing on the same .tmp file. The lock is stale-checked after 5 minutes. 3. tts/synthesize.ts: increase hasPythonPackage timeout from 10s to 30s (ONNX runtime cold import exceeds 10s on constrained machines), and set maxBuffer to 10 MB (ONNX prints verbose warnings that can exceed the 1 MB default, killing the subprocess). The download retry also benefits whisper (transcribe) and background-removal commands which share the same download utility. PostHog data: TTS failure rate hit 61% on June 2 when usage spiked 3x.
| const stat = statSync(lockPath); | ||
| if (Date.now() - stat.mtimeMs > LOCK_STALE_MS) { | ||
| unlinkSync(lockPath); | ||
| writeFileSync(lockPath, String(process.pid), { flag: "wx" }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
.lockfiles) to TTS model and voice downloads to prevent concurrent processes from racing on the same temp filehasPythonPackagetimeout from 10s to 30s to accommodate ONNX runtime cold-startmaxBufferto 10 MB on the synthesis subprocess to prevent truncation from verbose ONNX warningsProblem
The TTS command fails frequently at scale due to four compounding issues:
.tmpfile, causing corruption.hasPythonPackage("kokoro_onnx")report false even when it's installed.Reproduction
Scope
The download utility (
packages/cli/src/utils/download.ts) is shared by TTS, Whisper, and background-removal commands — all three benefit from the retry and timeout improvements.Test plan
bun run build)hyperframes tts "Hello" -o test.wavworks end-to-end