{"record":{"id":"63b6a0a4d12bac0d","repo":"affaan-m/ECC","slug":"download-length-does-not-match-declared-size-63b6a0","errorCode":null,"errorMessage":"download length does not match declared size","messagePattern":"download length does not match declared size","errorType":"exception","errorClass":"FalError","httpStatus":null,"severity":"error","filePath":"skills/taste-distillation/scripts/taste/falapi.py","lineNumber":715,"sourceCode":"        with opener.open(req, timeout=300) as resp:\n            declared = getattr(resp, \"headers\", {}).get(\"Content-Length\")\n            expected = int(declared) if declared is not None else None\n            if expected is not None and not 0 <= expected <= MAX_DOWNLOAD_BYTES:\n                raise FalError(\"download declares an invalid or excessive size\")\n            with tempfile.NamedTemporaryFile(dir=dest.parent, prefix=\".taste-download-\",\n                                             delete=False) as fh:\n                temporary = Path(fh.name)\n                total = 0\n                while True:\n                    chunk = resp.read(min(1024 * 1024, MAX_DOWNLOAD_BYTES - total + 1))\n                    if not chunk:\n                        break\n                    total += len(chunk)\n                    if total > MAX_DOWNLOAD_BYTES:\n                        raise FalError(\"download exceeds maximum allowed size\")\n                    fh.write(chunk)\n                if expected is not None and total != expected:\n                    raise FalError(\"download length does not match declared size\")\n        os.replace(temporary, dest)\n        temporary = None\n    except FalError:\n        raise\n    except Exception:\n        raise FalError(\"download failed; existing destination preserved\") from None\n    finally:\n        if temporary is not None:\n            temporary.unlink(missing_ok=True)\n    return dest\n\n\n# ---------------------------------------------------------------------------\n# dry-run stubs\n# ---------------------------------------------------------------------------\n\n\ndef _digest(*parts: Any) -> str:","sourceCodeStart":697,"sourceCodeEnd":733,"githubUrl":"https://github.com/affaan-m/ECC/blob/8321021c54d670126ce3b2969d5deb880b4b0c2a/skills/taste-distillation/scripts/taste/falapi.py#L697-L733","documentation":"After the body is fully read, download() compares the number of bytes received against the Content-Length the server declared and raises if they differ. This detects truncated or padded transfers so a corrupt file is never renamed into place.","triggerScenarios":"Connection drops mid-transfer (urllib may surface EOF without error but fewer bytes), server declares a larger Content-Length than it sends, or an intermediary rewrites the body while keeping the original header.","commonSituations":"Flaky mobile/VPN connections truncating downloads; reverse proxies (e.g. misconfigured gzip) altering body length; CDN edge timeouts on slow links.","solutions":["Retry the download; transient truncation is the most common cause","Check network stability / disable interfering proxies or TLS-inspecting middleboxes","Compare Content-Length with a HEAD request to see if the server is inconsistent","Rely on the destination file being preserved (temp file is discarded) and surface a user-facing retry"],"exampleFix":"// before\ndownload(url, dest)  # raises on truncated transfer\n// after\nfor attempt in range(3):\n    try:\n        download(url, dest); break\n    except FalError as e:\n        if \"does not match declared size\" not in str(e): raise","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"for attempt in range(3):\n    try:\n        download(url, dest); break\n    except FalError as e:\n        if \"does not match declared size\" in str(e) and attempt < 2:\n            time.sleep(2 ** attempt); continue\n        raise","preventionTips":["Retry truncated transfers automatically","Avoid flaky networks/proxies for large downloads","Verify the file after download before consuming it"],"tags":["network","integrity","http"],"backgroundTag":"checksum-mismatch","analyzedSha":"8321021c54d670126ce3b2969d5deb880b4b0c2a","analyzedAt":"2026-09-16T10:08:13.343Z","contentChangedAt":"2026-09-16T10:08:13.343Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}