Skip to content

Commit de160a4

Browse files
authored
Add a default timeout for filelock (#2391)
File lock operations did not have an associated timeout, which could present problems when trying to debug an issue since nothing would be logged if we could not acquire the lock and would wait forever.
1 parent c8a3e3a commit de160a4

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/huggingface_hub/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ def _as_int(value: Optional[str]) -> Optional[int]:
4949
SAFETENSORS_INDEX_FILE = "model.safetensors.index.json"
5050
SAFETENSORS_MAX_HEADER_LENGTH = 25_000_000
5151

52+
# Timeout of aquiring file lock and logging the attempt
53+
FILELOCK_LOG_EVERY_SECONDS = 10
54+
5255
# Git-related constants
5356

5457
DEFAULT_REVISION = "main"

src/huggingface_hub/utils/_fixes.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@
1818
from typing import Callable, Generator, Optional, Union
1919

2020
import yaml
21-
from filelock import BaseFileLock, FileLock
21+
from filelock import BaseFileLock, FileLock, Timeout
2222

23+
from .. import constants
24+
from . import logging
25+
26+
27+
logger = logging.get_logger(__name__)
2328

2429
# Wrap `yaml.dump` to set `allow_unicode=True` by default.
2530
#
@@ -80,8 +85,14 @@ def _set_write_permission_and_retry(func, path, excinfo):
8085
@contextlib.contextmanager
8186
def WeakFileLock(lock_file: Union[str, Path]) -> Generator[BaseFileLock, None, None]:
8287
"""A filelock that won't raise an exception if release fails."""
83-
lock = FileLock(lock_file)
84-
lock.acquire()
88+
lock = FileLock(lock_file, timeout=constants.FILELOCK_LOG_EVERY_SECONDS)
89+
while True:
90+
try:
91+
lock.acquire()
92+
except Timeout:
93+
logger.info("still waiting to acquire lock on %s", lock_file)
94+
else:
95+
break
8596

8697
yield lock
8798

0 commit comments

Comments
 (0)