{"record":{"id":"5fd9424f6eab1879","repo":"affaan-m/ECC","slug":"download-length-does-not-match-declared-size-falapi","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-application/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-application/scripts/taste/falapi.py#L697-L733","documentation":"After streaming, download() compares the actual bytes read with the declared Content-Length and raises FalError on a mismatch. This detects truncated or corrupted transfers so a partial file is never promoted to the destination.","triggerScenarios":"Connection drops mid-transfer so the server sent fewer bytes than Content-Length declared (truncation); a proxy or CDN alters the body; server sends a wrong Content-Length header (e.g. compressed vs uncompressed mismatch with an interfering intermediary).","commonSituations":"Flaky Wi-Fi or CI network interruptions during long video downloads; corporate proxy truncating responses; server misreporting length for gzip-encoded content.","solutions":["Retry the download — transient truncation usually succeeds on a second attempt","Check whether a proxy/intermediary is altering the response and bypass it (NO_PROXY / direct connection)","Verify the server's Content-Length with curl -sI and compare with the real byte count","If a specific endpoint consistently misreports length, adjust the client to skip the equality check (not recommended) or fetch without compression"],"exampleFix":"# before\n# truncated by flaky network\npath = download(url, dest)\n# after\nfor attempt in range(3):\n    try:\n        path = download(url, dest); break\n    except FalError as e:\n        if 'does not match declared size' not in str(e): raise\n        time.sleep(2 ** attempt)","handlingStrategy":"retry","validationCode":"# compare declared vs delivered size for your own client\nassert int(resp.headers['Content-Length']) == bytes_received","typeGuard":null,"tryCatchPattern":"for attempt in range(3):\n    try:\n        dest = download(url, dest_path)\n        break\n    except FalError:\n        time.sleep(2 ** attempt)\nelse:\n    raise RuntimeError('download kept failing size check')","preventionTips":["Retry truncated downloads automatically — most mismatches are transient","Exclude compression intermediaries/proxies that change body length","Use a stable network path for large downloads (wired CI runners)","Verify server Content-Length behavior once per endpoint"],"tags":["download","http","integrity","network"],"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"}