Skip to content

Commit cf0b669

Browse files
committed
Do not retry on 429 (only on 5xx) (#3377)
* Do not retry on 429 * harmonize retries in globally * make style
1 parent 0f12365 commit cf0b669

File tree

4 files changed

+8
-20
lines changed

4 files changed

+8
-20
lines changed

src/huggingface_hub/file_download.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def _request_wrapper(
267267
"""Wrapper around requests methods to follow relative redirects if `follow_relative_redirects=True` even when
268268
`allow_redirection=False`.
269269
270-
A backoff mechanism retries the HTTP call on 429, 503 and 504 errors.
270+
A backoff mechanism retries the HTTP call on 5xx errors and network errors.
271271
272272
Args:
273273
method (`str`):
@@ -306,7 +306,7 @@ def _request_wrapper(
306306
return response
307307

308308
# Perform request and return if status_code is not in the retry list.
309-
response = http_backoff(method=method, url=url, **params, retry_on_exceptions=(), retry_on_status_codes=(429,))
309+
response = http_backoff(method=method, url=url, **params)
310310
hf_raise_for_status(response)
311311
return response
312312

src/huggingface_hub/hf_file_system.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -958,13 +958,7 @@ def _fetch_range(self, start: int, end: int) -> bytes:
958958
repo_type=self.resolved_path.repo_type,
959959
endpoint=self.fs.endpoint,
960960
)
961-
r = http_backoff(
962-
"GET",
963-
url,
964-
headers=headers,
965-
retry_on_status_codes=(500, 502, 503, 504),
966-
timeout=constants.HF_HUB_DOWNLOAD_TIMEOUT,
967-
)
961+
r = http_backoff("GET", url, headers=headers, timeout=constants.HF_HUB_DOWNLOAD_TIMEOUT)
968962
hf_raise_for_status(r)
969963
return r.content
970964

@@ -1063,7 +1057,6 @@ def read(self, length: int = -1):
10631057
"GET",
10641058
url,
10651059
headers=self.fs._api._build_hf_headers(),
1066-
retry_on_status_codes=(500, 502, 503, 504),
10671060
stream=True,
10681061
timeout=constants.HF_HUB_DOWNLOAD_TIMEOUT,
10691062
)
@@ -1086,7 +1079,6 @@ def read(self, length: int = -1):
10861079
"GET",
10871080
url,
10881081
headers={"Range": "bytes=%d-" % self.loc, **self.fs._api._build_hf_headers()},
1089-
retry_on_status_codes=(500, 502, 503, 504),
10901082
stream=True,
10911083
timeout=constants.HF_HUB_DOWNLOAD_TIMEOUT,
10921084
)

src/huggingface_hub/lfs.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ def _upload_single_part(operation: "CommitOperationAdd", upload_url: str) -> Non
316316
"""
317317
with operation.as_file(with_tqdm=True) as fileobj:
318318
# S3 might raise a transient 500 error -> let's retry if that happens
319-
response = http_backoff("PUT", upload_url, data=fileobj, retry_on_status_codes=(500, 502, 503, 504))
319+
response = http_backoff("PUT", upload_url, data=fileobj)
320320
hf_raise_for_status(response)
321321

322322

@@ -400,9 +400,7 @@ def _upload_parts_iteratively(
400400
read_limit=chunk_size,
401401
) as fileobj_slice:
402402
# S3 might raise a transient 500 error -> let's retry if that happens
403-
part_upload_res = http_backoff(
404-
"PUT", part_upload_url, data=fileobj_slice, retry_on_status_codes=(500, 502, 503, 504)
405-
)
403+
part_upload_res = http_backoff("PUT", part_upload_url, data=fileobj_slice)
406404
hf_raise_for_status(part_upload_res)
407405
headers.append(part_upload_res.headers)
408406
return headers # type: ignore

src/huggingface_hub/utils/_http.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import time
2222
import uuid
2323
from functools import lru_cache
24-
from http import HTTPStatus
2524
from shlex import quote
2625
from typing import Any, Callable, List, Optional, Tuple, Type, Union
2726

@@ -221,7 +220,7 @@ def http_backoff(
221220
requests.Timeout,
222221
requests.ConnectionError,
223222
),
224-
retry_on_status_codes: Union[int, Tuple[int, ...]] = HTTPStatus.SERVICE_UNAVAILABLE,
223+
retry_on_status_codes: Union[int, Tuple[int, ...]] = (500, 502, 503, 504),
225224
**kwargs,
226225
) -> Response:
227226
"""Wrapper around requests to retry calls on an endpoint, with exponential backoff.
@@ -250,9 +249,8 @@ def http_backoff(
250249
retry_on_exceptions (`Type[Exception]` or `Tuple[Type[Exception]]`, *optional*):
251250
Define which exceptions must be caught to retry the request. Can be a single type or a tuple of types.
252251
By default, retry on `requests.Timeout` and `requests.ConnectionError`.
253-
retry_on_status_codes (`int` or `Tuple[int]`, *optional*, defaults to `503`):
254-
Define on which status codes the request must be retried. By default, only
255-
HTTP 503 Service Unavailable is retried.
252+
retry_on_status_codes (`int` or `Tuple[int]`, *optional*, defaults to `(500, 502, 503, 504)`):
253+
Define on which status codes the request must be retried. By default, 5xx errors are retried.
256254
**kwargs (`dict`, *optional*):
257255
kwargs to pass to `requests.request`.
258256

0 commit comments

Comments
 (0)