Skip to content

Expand file lock scope to resolve concurrency issues during downloads #3063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/huggingface_hub/file_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -1130,19 +1130,21 @@ def _hf_hub_download_to_cache_dir(
# In that case store a ref.
_cache_commit_hash_for_specific_revision(storage_folder, revision, commit_hash)

# If file already exists, return it (except if force_download=True)
if not force_download:
if os.path.exists(pointer_path):
return pointer_path

if os.path.exists(blob_path):
# we have the blob already, but not the pointer
_create_symlink(blob_path, pointer_path, new_blob=False)
return pointer_path

# Prevent parallel downloads of the same file with a lock.
# etag could be duplicated across repos,
lock_path = os.path.join(locks_dir, repo_folder_name(repo_id=repo_id, repo_type=repo_type), f"{etag}.lock")
Path(lock_path).parent.mkdir(parents=True, exist_ok=True)

# If file already exists, return it (except if force_download=True)
with WeakFileLock(lock_path):
if not force_download:
if os.path.exists(pointer_path):
return pointer_path

if os.path.exists(blob_path):
# we have the blob already, but not the pointer
_create_symlink(blob_path, pointer_path, new_blob=False)
return pointer_path

# Some Windows versions do not allow for paths longer than 255 characters.
# In this case, we must specify it as an extended path by using the "\\?\" prefix.
Expand All @@ -1154,7 +1156,6 @@ def _hf_hub_download_to_cache_dir(

# Local file doesn't exist or etag isn't a match => retrieve file from remote (or cache)

Path(lock_path).parent.mkdir(parents=True, exist_ok=True)
with WeakFileLock(lock_path):
_download_to_tmp_and_move(
incomplete_path=Path(blob_path + ".incomplete"),
Expand Down