Skip to content

fix: prevent partial file reads during concurrent downloads#1548

Open
nico-martin wants to merge 4 commits intomainfrom
fix/concurrent-download-file-lock
Open

fix: prevent partial file reads during concurrent downloads#1548
nico-martin wants to merge 4 commits intomainfrom
fix/concurrent-download-file-lock

Conversation

@nico-martin
Copy link
Collaborator

Fixes #1544

What

Writes model files to a unique .tmp.<pid>.<random> path first, then atomically renames to the final path once the download is complete. This ensures concurrent readers (whether in other processes or in the same process) never see a partially-written file.

Changes

  • hub/files.js → split into hub/FileResponse.js and cache/FileCache.js
  • FileCache.put() now writes to a unique temp file and renames atomically on success, or deletes on error

The main fix is this commit and this small improvement. The rest ist just some clean-up.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@xenova
Copy link
Collaborator

xenova commented Mar 1, 2026

@sroussey would be great to get your take on this PR! :)

@sroussey
Copy link
Contributor

sroussey commented Mar 3, 2026

Looks good to me!

@sroussey
Copy link
Contributor

sroussey commented Mar 3, 2026

btw... does unlink() work for directories?

@sroussey
Copy link
Contributor

sroussey commented Mar 3, 2026

from the node docs:

To get a behavior similar to the rm -rf Unix command, use fsPromises.rm() with options { recursive: true, force: true }.

https://nodejs.org/api/fs.html#fspromisesrmpath-options

This is in regards to the catch()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Loading the same model simultaneously in multiple processes fails

4 participants