heard icon indicating copy to clipboard operation
heard copied to clipboard

Kokoro model download truncates silently — no Content-Length / checksum verification

Open arthursolwayne opened this issue 7 months ago • 1 comments

Repro

On a fresh install with a flaky / interrupted network during first-run Kokoro model download:

$ ls -la ~/Library/Application\ Support/heard/models/
-rw-r--r--  1 me  staff  24936448 Apr 30 14:58 kokoro-v1.0.onnx     ← truncated
-rw-r--r--  1 me  staff  28214398 Apr 30 14:57 voices-v1.0.bin

$ /Applications/Heard.app/Contents/MacOS/python -m heard doctor
...
Synth
  ✗ synth                 InvalidProtobuf: [ONNXRuntimeError] : 7 : INVALID_PROTOBUF :
                          Load model from .../kokoro-v1.0.onnx failed: Protobuf parsing failed.

Upstream kokoro-v1.0.onnx is 325,532,387 bytes; on-disk file was 24.9 MB after the download "completed" without raising. ONNX runtime then explodes on next synth, and the daemon never recovers — the broken file sits there forever because is_downloaded() only checks .exists().

Root cause

heard/tts/kokoro.py:_download_with_progress:

urllib.request.urlretrieve(url, tmp)
tmp.rename(dest)

urlretrieve doesn't raise on a connection that closes cleanly mid-stream; the partial file gets renamed atop the destination. There is no:

  1. Content-Length verification
  2. Checksum verification (no SHA256 pinned anywhere in the repo)
  3. Retry on truncation

is_downloaded() is a pure existence check, so the daemon will never re-pull a corrupt file on its own.

Suggested fix

  1. Pull the resource through urllib.request.urlopen, read Content-Length from headers, stream chunks to .part, abort if the file size doesn't match.
  2. Pin a known-good SHA256 for both kokoro-v1.0.onnx and voices-v1.0.bin in a constants file; verify after download, retry up to N times.
  3. Tighten is_downloaded() — at minimum check stat().st_size > 0 and against the pinned size; ideally verify the checksum, but that's expensive on a 325 MB file at every daemon start. A .sha256 sidecar written after first verification and skipped on subsequent loads would be the cheap fix.

Happy to PR if there's a preferred shape — left a comment on the SSL PR linking here.

Env

  • macOS 26
  • Heard v0.4

arthursolwayne avatar Apr 30 '26 22:04 arthursolwayne

Fixed in 85782e9. Streamed download + Content-Length verification + final byte-count check + 3-attempt retry. Pinned upstream sizes (325,532,387 / 28,214,398) catch truncation deterministically. is_downloaded() now verifies size against the pinned values, so truncated leftovers from pre-fix installs auto-re-download on next launch — no manual cleanup needed. Will ship in v0.4.3.

Thanks @arthursolwayne — clear repro and the SHA256-vs-size tradeoff call you flagged was exactly right.

sodiumsun avatar May 02 '26 06:05 sodiumsun