{"record":{"id":"50b68f22a403a754","repo":"Comfy-Org/ComfyUI","slug":"hash-of-path-url-url-failed-to-validate","errorCode":null,"errorMessage":"hash of {path} (url: {url}) failed to validate","messagePattern":"hash of (.+?) \\(url: (.+?)\\) failed to validate","errorType":"exception","errorClass":"OSError","httpStatus":null,"severity":"error","filePath":"comfy/k_diffusion/utils.py","lineNumber":47,"sourceCode":"    return expanded.detach().clone() if expanded.device.type == 'mps' else expanded\n\n\ndef n_params(module):\n    \"\"\"Returns the number of trainable parameters in a module.\"\"\"\n    return sum(p.numel() for p in module.parameters())\n\n\ndef download_file(path, url, digest=None):\n    \"\"\"Downloads a file if it does not exist, optionally checking its SHA-256 hash.\"\"\"\n    path = Path(path)\n    path.parent.mkdir(parents=True, exist_ok=True)\n    if not path.exists():\n        with urllib.request.urlopen(url) as response, open(path, 'wb') as f:\n            shutil.copyfileobj(response, f)\n    if digest is not None:\n        file_digest = hashlib.sha256(open(path, 'rb').read()).hexdigest()\n        if digest != file_digest:\n            raise OSError(f'hash of {path} (url: {url}) failed to validate')\n    return path\n\n\n@contextmanager\ndef train_mode(model, mode=True):\n    \"\"\"A context manager that places a model into training mode and restores\n    the previous mode on exit.\"\"\"\n    modes = [module.training for module in model.modules()]\n    try:\n        yield model.train(mode)\n    finally:\n        for i, module in enumerate(model.modules()):\n            module.training = modes[i]\n\n\ndef eval_mode(model):\n    \"\"\"A context manager that places a model into evaluation mode and restores\n    the previous mode on exit.\"\"\"","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/Comfy-Org/ComfyUI/blob/1c6d8d45b3693bfbb32385b410d813a7fd6be216/comfy/k_diffusion/utils.py#L29-L65","documentation":"Raised by comfy/k_diffusion/utils.py's download_file() helper after a file (already on disk or just downloaded) fails SHA-256 validation against the caller-supplied digest. It is an OSError, signalling the local bytes do not match the expected checksum for the given URL. Typical root cause is a truncated/corrupted prior download, a proxy or CDN serving different content, or an incorrect digest string.","triggerScenarios":"Calling download_file(path, url, digest=...) where the file already exists (or downloads) and hashlib.sha256 of its contents != digest. Happens when a previous run was interrupted mid-write leaving a partial file, or when the URL points to a re-published artifact whose hash changed.","commonSituations":"Corrupted cache from a killed process or disk-full event; the upstream file at the URL was replaced; a typo'd or copy-paste-mangled digest; a mirror/proxy injecting an HTML error page instead of the binary.","solutions":["Delete the partial/corrupt file at {path} (and any siblings) so download_file re-downloads it fresh","Recompute sha256 of the file (shasum -a 256 / certutil -hashfile) and compare with the expected digest to confirm corruption vs. wrong digest","Verify the URL still serves the intended artifact and that the digest matches the publisher's stated hash; update digest if the artifact legitimately changed","If behind a proxy/cache, bypass it or clear it and retry the download manually before re-running"],"exampleFix":"# before: corrupt cached file keeps failing\n# rm the cached file so it re-downloads\nimport os; os.remove('/path/to/model/file')\n\n# after: validate before passing digest\nd = hashlib.sha256(open(path,'rb').read()).hexdigest()\nif d != expected: os.remove(path)\npath = download_file(path, url, digest=expected)","handlingStrategy":"retry","validationCode":"import hashlib, os\ndef ensure_file(path, url, digest):\n    if os.path.exists(path):\n        d = hashlib.sha256(open(path,'rb').read()).hexdigest()\n        if d != digest:\n            os.remove(path)  # force clean re-download\n    return download_file(path, url, digest=digest)","typeGuard":null,"tryCatchPattern":"try:\n    path = download_file(p, url, digest=d)\nexcept OSError as e:\n    if 'failed to validate' in str(e):\n        os.remove(p)\n        path = download_file(p, url, digest=d)  # one clean retry\n    else:\n        raise","preventionTips":["Pre-download models with a checksum-verifying tool before the run","Delete partial files after any interrupted download instead of reusing them","Pin artifact URLs and digests from the publisher together so they never drift independently"],"tags":["download","checksum","corruption","network"],"backgroundTag":null,"analyzedSha":"1c6d8d45b3693bfbb32385b410d813a7fd6be216","analyzedAt":"2026-08-14T19:37:18.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}