Skip to content
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

Keep lock files in a /locks folder to prevent rare concurrency issue #1659

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 4 additions & 6 deletions src/huggingface_hub/file_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,7 @@ def hf_hub_download(
cache_dir = str(cache_dir)
if isinstance(local_dir, Path):
local_dir = str(local_dir)
locks_dir = os.path.join(cache_dir, ".locks")

if subfolder == "":
subfolder = None
Expand Down Expand Up @@ -1410,7 +1411,8 @@ def hf_hub_download(
return pointer_path

# Prevent parallel downloads of the same file with a lock.
lock_path = blob_path + ".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")

# Some Windows versions do not allow for paths longer than 255 characters.
# In this case, we must specify it is an extended path by using the "\\?\" prefix.
Expand All @@ -1420,6 +1422,7 @@ def hf_hub_download(
if os.name == "nt" and len(os.path.abspath(blob_path)) > 255:
blob_path = "\\\\?\\" + os.path.abspath(blob_path)

Path(lock_path).parent.mkdir(parents=True, exist_ok=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

with FileLock(lock_path):
# If the download just completed while the lock was activated.
if os.path.exists(pointer_path) and not force_download:
Expand Down Expand Up @@ -1494,11 +1497,6 @@ def _resumable_file_manager() -> Generator[io.BufferedWriter, None, None]:
_chmod_and_replace(temp_file.name, local_dir_filepath)
pointer_path = local_dir_filepath # for return value

try:
os.remove(lock_path)
except OSError:
pass

return pointer_path


Expand Down
14 changes: 14 additions & 0 deletions tests/test_file_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,20 @@ def test_download_from_a_gated_repo_with_hf_hub_download(self):
cache_dir=tmpdir,
)

@unittest.skipIf(os.name == "nt", "Lock files are always deleted on Windows.")
def test_keep_lock_file(self):
Wauplin marked this conversation as resolved.
Show resolved Hide resolved
beeender marked this conversation as resolved.
Show resolved Hide resolved
"""Lock files should not be deleted on Linux."""
with SoftTemporaryDirectory() as tmpdir:
hf_hub_download(DUMMY_MODEL_ID, filename=CONFIG_NAME, cache_dir=tmpdir)
lock_file_exist = False
locks_dir = os.path.join(tmpdir, ".locks")
for subdir, dirs, files in os.walk(locks_dir):
for file in files:
if file.endswith(".lock"):
lock_file_exist = True
break
self.assertTrue(lock_file_exist, "no lock file can be found")


@with_production_testing
@pytest.mark.usefixtures("fx_cache_dir")
Expand Down