{"record":{"id":"3990115a306c765b","repo":"stablyai/orca","slug":"destination-file-appeared-before-download-complete","errorCode":null,"errorMessage":"Destination file appeared before download completed","messagePattern":"Destination file appeared before download completed","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/main/ipc/filesystem.ts","lineNumber":264,"sourceCode":"    return { existed: true }\n  } catch (error) {\n    if (isENOENT(error)) {\n      return { existed: false }\n    }\n    throw error\n  }\n}\n\nasync function assertDestinationStillUnclaimed(destinationPath: string): Promise<void> {\n  try {\n    await stat(destinationPath)\n  } catch (error) {\n    if (isENOENT(error)) {\n      return\n    }\n    throw error\n  }\n  throw new Error('Destination file appeared before download completed')\n}\n\nasync function promoteDownloadedFile(\n  tempPath: string,\n  destinationPath: string,\n  destinationExisted: boolean\n): Promise<void> {\n  if (!destinationExisted) {\n    await assertDestinationStillUnclaimed(destinationPath)\n    await rename(tempPath, destinationPath)\n    return\n  }\n\n  const backupPath = createSiblingTransferPath(destinationPath, 'backup')\n  let backupCreated = false\n  try {\n    await rename(destinationPath, backupPath)\n    backupCreated = true","sourceCodeStart":246,"sourceCodeEnd":282,"githubUrl":"https://github.com/stablyai/orca/blob/1136503c6a231a16dce8f921f6fadb63d181e8db/src/main/ipc/filesystem.ts#L246-L282","documentation":"Thrown by assertDestinationStillUnclaimed during download promotion. If the destination did not exist when the download started, but now exists when the temp file is about to be renamed, another process created it in the window. The code refuses to overwrite, treating the race as a potential conflict or security concern.","triggerScenarios":"Between inspectDownloadDestination (which recorded existed=false) and promoteDownloadedFile (which calls assertDestinationStillUnclaimed), another process or concurrent download creates a file at the exact destination path. The final stat succeeds, indicating the destination is no longer free.","commonSituations":"Two concurrent downloads targeting the same path. A background sync (Dropbox, iCloud, rsync) creates the file during the download. A user or script creates the file manually while the download is in progress.","solutions":["Retry the download to a different filename.","Ensure no concurrent downloads or sync agents target the same directory.","If the conflict was from a failed prior download, remove the stale file and retry."],"exampleFix":"// before: download to /downloads/file.bin while a sync agent also writes there\n// after: pause sync or use a unique filename\n//   download to /downloads/file (1).bin","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await ipcRenderer.invoke('fs:downloadFile', { filePath, connectionId })\n} catch (error) {\n  if (error instanceof Error && error.message === 'Destination file appeared before download completed') {\n    showUserWarning('Another process created the destination file. Retry with a different name.')\n    return\n  }\n  throw error\n}","preventionTips":["Avoid concurrent downloads to the same destination path.","Pause cloud sync agents (Dropbox, iCloud) that might create files in the download directory.","Use unique filenames (e.g., with timestamps) for download destinations."],"tags":["download","race-condition","toctou","filesystem","ipc"],"backgroundTag":null,"analyzedSha":"1136503c6a231a16dce8f921f6fadb63d181e8db","analyzedAt":"2026-08-12T23:15:58.167Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}