{"record":{"id":"935fa91a18f02899","repo":"lllyasviel/Fooocus","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":"ldm_patched/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/lllyasviel/Fooocus/blob/ae05379cc97bc4361ec8b4ec90193dab21be763f/ldm_patched/k_diffusion/utils.py#L29-L65","documentation":"download_file() fetches a file over HTTP and, when a SHA-256 digest is supplied, recomputes the hash of the on-disk file and compares it to the expected value. On mismatch it raises OSError naming the path and URL. This catches both corrupted/interrupted downloads and upstream files that changed after the digest was pinned.","triggerScenarios":"A partial download caused by a dropped connection (the file exists so it is not re-fetched, then fails hashing); a previously downloaded file corrupted on disk; the remote asset at `url` was replaced so the pinned digest no longer matches.","commonSituations":"Pretrained-weight bootstrap in k-diffusion training scripts; shared caches where another process wrote to the same path; mirrors/CDNs serving different bytes; disk-full truncation during first download.","solutions":["Delete the cached file at `path` and retry so it is downloaded fresh.","Verify the remote file actually changed (recompute sha256 of a fresh copy); if it legitimately changed, update the pinned `digest`.","Check free disk space / network stability if the file is truncated (size much smaller than expected).","If the URL is dead or redirected, update the url argument along with the digest."],"exampleFix":"# before\ndownload_file('ckpt/vgg16.pth', URL, digest='oldhash...')  # OSError: hash failed\n\n# after (re-download then re-pin)\n!rm ckpt/vgg16.pth\ndownload_file('ckpt/vgg16.pth', URL)  # inspect, then:\ndigest = hashlib.sha256(open('ckpt/vgg16.pth','rb').read()).hexdigest()\ndownload_file('ckpt/vgg16.pth', URL, digest=digest)","handlingStrategy":"retry","validationCode":"def sha256_of(path):\n    h = hashlib.sha256()\n    with open(path, 'rb') as f:\n        for chunk in iter(lambda: f.read(1 << 20), b''):\n            h.update(chunk)\n    return h.hexdigest()\n\nif path.exists() and digest is not None and sha256_of(path) != digest:\n    path.unlink()  # force clean re-download","typeGuard":null,"tryCatchPattern":"for attempt in range(3):\n    try:\n        p = download_file(path, url, digest=digest)\n        break\n    except OSError as e:\n        if 'failed to validate' not in str(e):\n            raise\n        Path(path).unlink(missing_ok=True)  # corrupt cache; retry fresh\nelse:\n    raise RuntimeError(f'hash validation failed after retries: {path}')","preventionTips":["On hash failure, delete the cached file and retry the download once.","Pin digests from a trusted manifest and update them deliberately when upstreams re-release.","Download to a temp file and atomically rename after hashing to avoid partial-file caches."],"tags":["k-diffusion","download","sha256","integrity"],"backgroundTag":null,"analyzedSha":"ae05379cc97bc4361ec8b4ec90193dab21be763f","analyzedAt":"2026-08-15T04:23:59.533Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}