Skip to content

[Medium] Whisper model cache readiness is existence-only (partial/corrupt model passes) #16

Open

Description

@adilei

Severity

Medium — a partially-downloaded/corrupt model passes the readiness check.

Location

electron/narration/whisper.ts — model-cache readiness is determined by directory/file existence only, with no completeness verification.

Mechanism

If a model download is interrupted (network drop, crash, disk full), the cache directory exists but is incomplete. An existence-only check reports "ready", so transcription proceeds against a truncated/corrupt model.

Impact

Confusing runtime failures or silently degraded transcription that looks like a model/quality problem rather than a bad cache.

Suggested fix

Verify completeness before marking ready — e.g. check the expected file set and sizes, and ideally a manifest hash (see the related model-pinning low finding) — and re-download if incomplete.

Regression test to add (npm test)

Extend electron/narration/whisper.test.ts: point at a partial model dir (missing/short files) and assert readiness returns false / triggers re-fetch. Deterministic; not an evals/ concern.

Activity

  1. lizheng220 commented on Sep 7, 2026

    @lizheng220

    I picked this up and want to flag something before writing a patch, because I couldn't reproduce the stated mechanism against the version in the lockfile. Very happy to be told I'm missing context.

    The download path already writes atomically

    @huggingface/transformers 4.2.0 caches through FileCache.put(), which streams into a temp sibling and only renames on success (from dist/transformers.node.cjs after a clean npm ci):

    const tmpPath = filePath + `.tmp.${id}.${randomSuffix}`;
    // ... stream the response body into tmpPath ...
    await fs.promises.rename(tmpPath, filePath);
    } catch (error) {
      await fs.promises.unlink(tmpPath);   // any failure removes the temp
      throw error;
    }

    So an interrupted download — network drop, crash, disk full — doesn't leave a truncated file at the final path. It leaves the final path absent, plus an orphan .tmp.<pid>.<suffix>. An absent file is already caught by the existing NARRATION_MODEL_FILES.every(existsSync) in isNarrationModelCachedAt().

    And the orphans are already swept

    removePartialDownloads() (electron/narration/manager.ts:78) recursively deletes entries matching /\.tmp\.\d+\./, and loadModel() calls it before every re-download (manager.ts:346). That regex matches transformers.js's temp naming exactly, so it reads as deliberate rather than incidental.

    For what it's worth, that sweep landed in 96e7058 ("Add consent-gated Whisper download", 2026-07-27), two days before this issue was filed — so it may simply have been missed when the audit was written.

    What I think is still true, but narrower

    1. Existence-only can't detect corruption that happens after a successful download — AV quarantine or truncation, bit rot, a partial backup restore, manual edits. Real, but a different and rarer path than an interrupted download.
    2. NARRATION_MODEL_FILES is a hand-maintained list of 7 paths. If the upstream repo's required file set changes, all 7 existing doesn't imply loadable — that's staleness rather than corruption.

    One thing worth knowing before anyone changes this: whisper.test.ts currently seeds the cache with zero-byte files and asserts readiness is true. So "an empty file counts as ready" is presently encoded as intended behaviour, and any completeness check changes that contract — both existing tests would need updating alongside it.

    So: do you still want hardening here?

    If yes I'm glad to do it, and I'd want your steer on which shape:

    • Non-empty + conservative per-file size floors. Simple, no new on-disk state, catches truncation-to-zero and gross corruption. Doesn't catch subtle corruption.
    • A manifest written after a verified download and checked on load. Much stronger, but existing installs have no manifest, so it needs a migration path or it forces every current user into a ~252 MB re-download.

    If you'd rather close this as already covered by the atomic write plus the sweep, that's an equally good outcome from my side — I mainly didn't want to submit a patch built on a mechanism I couldn't demonstrate.

    I haven't checked the sibling findings filed in the same batch; happy to verify those the same way if that would be useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions