Skip to content

Commit e9d845b

Browse files
authored
gh-134262: Add retries to downloads in PCbuild\get_external.py (GH-134820)
1 parent e496444 commit e9d845b

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

PCbuild/get_external.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,36 @@
99
from urllib.request import urlretrieve
1010

1111

12+
def retrieve_with_retries(download_location, output_path, reporthook,
13+
max_retries=7):
14+
"""Download a file with exponential backoff retry and save to disk."""
15+
for attempt in range(max_retries):
16+
try:
17+
resp = urlretrieve(
18+
download_location,
19+
output_path,
20+
reporthook=reporthook,
21+
)
22+
except ConnectionError as ex:
23+
if attempt == max_retries:
24+
msg = f"Download from {download_location} failed."
25+
raise OSError(msg) from ex
26+
time.sleep(2.25**attempt)
27+
else:
28+
return resp
29+
30+
1231
def fetch_zip(commit_hash, zip_dir, *, org='python', binary=False, verbose):
1332
repo = f'cpython-{"bin" if binary else "source"}-deps'
1433
url = f'https://github.com/{org}/{repo}/archive/{commit_hash}.zip'
1534
reporthook = None
1635
if verbose:
1736
reporthook = print
1837
zip_dir.mkdir(parents=True, exist_ok=True)
19-
filename, headers = urlretrieve(
38+
filename, _headers = retrieve_with_retries(
2039
url,
2140
zip_dir / f'{commit_hash}.zip',
22-
reporthook=reporthook,
41+
reporthook
2342
)
2443
return filename
2544

0 commit comments

Comments
 (0)