{"record":{"id":"ca5b4221ff149c85","repo":"different-ai/openwork","slug":"zero-byte-file","errorCode":"zero-byte-file","errorMessage":"Download response was empty.","messagePattern":"Download response was empty\\.","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"apps/desktop/electron/binary-transfer.mjs","lineNumber":312,"sourceCode":"    await mkdir(stagingDir, { recursive: true });\n    stagingPath = path.join(stagingDir, `download-${randomUUID()}.part`);\n    stagingFile = await open(stagingPath, \"wx+\");\n    const reader = response.body?.getReader();\n    let bytes = 0;\n    if (reader) {\n      while (true) {\n        signal?.throwIfAborted();\n        const { done, value } = await reader.read();\n        if (done) break;\n        bytes += value.byteLength;\n        if (bytes > maxBytes) {\n          await reader.cancel();\n          throw transferError(`Download exceeds the ${maxBytes}-byte limit.`, \"file-too-large\");\n        }\n        await writeAll(stagingFile, value);\n      }\n    }\n    if (bytes === 0) throw transferError(\"Download response was empty.\", \"zero-byte-file\");\n    // Only a complete download reaches the workspace. Revalidate the\n    // destination, create it exclusively (\"wx\" never follows a final\n    // symlink), and prove by device and inode that the created file resides\n    // inside the authorized root before a single byte is written to it.\n    await resolveAuthorizedPath(input?.destinationPath, options?.authorizedRoots);\n    try {\n      destinationFile = await open(destinationPath, \"wx\");\n    } catch (error) {\n      if (error?.code === \"EEXIST\") {\n        throw transferError(\"Download destination already exists.\", \"destination-exists\");\n      }\n      throw error;\n    }\n    await verifyOpenFileWithinRoot(destinationFile, destinationPath, destination.rootRealPath, \"Download destination\");\n    const buffer = Buffer.allocUnsafe(1024 * 1024);\n    let position = 0;\n    while (position < bytes) {\n      signal?.throwIfAborted();","sourceCodeStart":294,"sourceCodeEnd":330,"githubUrl":"https://github.com/different-ai/openwork/blob/2b7df46e8ae1517d64c896c7793d2d52ec845669/apps/desktop/electron/binary-transfer.mjs#L294-L330","documentation":"After the response body stream completes, downloadBinaryToPath throws code 'zero-byte-file' if no bytes at all were received. An empty body means there is no binary to save, so the operation fails rather than creating an empty destination file. This indicates an upstream server/CDN problem, not a local one.","triggerScenarios":"Calling downloadBinaryToPath against a URL that returns response.ok with a zero-length body (empty 200 response), after the read loop ends with bytes === 0.","commonSituations":"CDN or proxy returning empty bodies on transient faults; URLs that redirect to an empty success page; server-side job not finished so the artifact endpoint returns an empty 200; expired signed URLs yielding empty success responses.","solutions":["Retry the download after a short delay — empty 200s are often transient upstream faults.","Verify the URL actually serves the binary (curl -I / curl the URL) and that it is not a placeholder or redirect landing page.","Check server-side artifact availability (build finished, upload completed) before downloading.","If the resource can legitimately be empty, gate the call: only download when size/content-length > 0."],"exampleFix":"// before\nawait downloadBinaryToPath({ url, destinationPath });\n// after\nconst head = await fetch(url, { method: \"HEAD\" });\nconst size = Number(head.headers.get(\"content-length\") ?? 0);\nif (size > 0) await downloadBinaryToPath({ url, destinationPath });\nelse throw new Error(`Artifact at ${url} is empty upstream; check the server.`);","handlingStrategy":"retry","validationCode":"const head = await fetch(url, { method: \"HEAD\" });\nconst len = Number(head.headers.get(\"content-length\"));\nif (head.ok && len === 0) throw new Error(`Upstream artifact at ${url} is empty; do not download yet`);","typeGuard":"function isDownloadableHead(head) {\n  return head.ok && Number(head.headers.get(\"content-length\") ?? \"0\") > 0;\n}","tryCatchPattern":"try {\n  await downloadBinaryToPath({ url, destinationPath });\n} catch (e) {\n  if (e?.code === \"zero-byte-file\") {\n    await sleep(2000);\n    return downloadBinaryToPath({ url, destinationPath }); // bounded retry\n  }\n  throw e;\n}","preventionTips":["Only download artifacts after the producing job/upload is confirmed complete.","Verify signed URLs are fresh and point at the object, not a redirect landing page.","Add a bounded retry with backoff for transient upstream empty responses."],"tags":["download","empty-response","network"],"backgroundTag":"empty-response-body","analyzedSha":"2b7df46e8ae1517d64c896c7793d2d52ec845669","analyzedAt":"2026-09-01T07:59:23.713Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}