Skip to content

Commit 337c18b

Browse files
Retry metadata fetch with longer timeout when no local file exists (#3569)
* implement retry mechanism for HEAD call * don't define a en variable * nit
1 parent 9b0c214 commit 337c18b

File tree

1 file changed

+44
-4
lines changed

1 file changed

+44
-4
lines changed

src/huggingface_hub/file_download.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363

6464
_are_symlinks_supported_in_dir: dict[str, bool] = {}
6565

66+
# Internal retry timeout for metadata fetch when no local file exists
67+
_ETAG_RETRY_TIMEOUT = 60
68+
6669

6770
def are_symlinks_supported(cache_dir: Union[str, Path, None] = None) -> bool:
6871
"""Return whether the symlinks are supported on the machine.
@@ -1131,8 +1134,28 @@ def _hf_hub_download_to_cache_dir(
11311134
if not force_download:
11321135
return pointer_path
11331136

1134-
# Otherwise, raise appropriate error
1135-
_raise_on_head_call_error(head_call_error, force_download, local_files_only)
1137+
# No local file found, retry with longer timeout if it was a timeout error
1138+
if isinstance(head_call_error, httpx.TimeoutException):
1139+
logger.info("Metadata fetch timed out and no local file found. Retrying with longer timeout..")
1140+
(url_to_download, etag, commit_hash, expected_size, xet_file_data, head_call_error) = (
1141+
_get_metadata_or_catch_error(
1142+
repo_id=repo_id,
1143+
filename=filename,
1144+
repo_type=repo_type,
1145+
revision=revision,
1146+
endpoint=endpoint,
1147+
etag_timeout=_ETAG_RETRY_TIMEOUT,
1148+
headers=headers,
1149+
token=token,
1150+
local_files_only=local_files_only,
1151+
storage_folder=storage_folder,
1152+
relative_filename=relative_filename,
1153+
)
1154+
)
1155+
1156+
# If still error, raise
1157+
if head_call_error is not None:
1158+
_raise_on_head_call_error(head_call_error, force_download, local_files_only)
11361159

11371160
# From now on, etag, commit_hash, url and size are not None.
11381161
assert etag is not None, "etag must have been retrieved from server"
@@ -1300,9 +1323,26 @@ def _hf_hub_download_to_local_dir(
13001323
)
13011324
if not force_download:
13021325
return local_path
1326+
elif not force_download and isinstance(head_call_error, httpx.TimeoutException):
1327+
# No local file found, retry with longer timeout if it was a timeout error
1328+
logger.info("Metadata fetch timed out and no local file found. Retrying with longer timeout...")
1329+
(url_to_download, etag, commit_hash, expected_size, xet_file_data, head_call_error) = (
1330+
_get_metadata_or_catch_error(
1331+
repo_id=repo_id,
1332+
filename=filename,
1333+
repo_type=repo_type,
1334+
revision=revision,
1335+
endpoint=endpoint,
1336+
etag_timeout=_ETAG_RETRY_TIMEOUT,
1337+
headers=headers,
1338+
token=token,
1339+
local_files_only=local_files_only,
1340+
)
1341+
)
13031342

1304-
# Otherwise => raise
1305-
_raise_on_head_call_error(head_call_error, force_download, local_files_only)
1343+
# If still error, raise
1344+
if head_call_error is not None:
1345+
_raise_on_head_call_error(head_call_error, force_download, local_files_only)
13061346

13071347
# From now on, etag, commit_hash, url and size are not None.
13081348
assert etag is not None, "etag must have been retrieved from server"

0 commit comments

Comments
 (0)