Skip to content

Commit 6a1b95d

Browse files
authored
Retry downloads when <75% of the file is downloaded (#496)
Closes #489 (hopefully)
1 parent 2d8972d commit 6a1b95d

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

pythonbuild/utils.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ def write_target_settings(targets, dest_path: pathlib.Path):
214214
class IntegrityError(Exception):
215215
"""Represents an integrity error when downloading a URL."""
216216

217+
def __init__(self, *args, length: int):
218+
self.length = length
219+
super().__init__(*args)
220+
217221

218222
def secure_download_stream(url, size, sha256):
219223
"""Securely download a URL to a stream of chunks.
@@ -291,9 +295,13 @@ def download_to_path(url: str, path: pathlib.Path, size: int, sha256: str):
291295
fh.write(chunk)
292296

293297
break
294-
except IntegrityError:
298+
except IntegrityError as e:
295299
tmp.unlink()
296-
raise
300+
# If we didn't get most of the expected file, retry
301+
if e.length > size * 0.75:
302+
raise
303+
print(f"Integrity error on {url}; retrying: {e}")
304+
time.sleep(2**attempt)
297305
except http.client.HTTPException as e:
298306
print(f"HTTP exception on {url}; retrying: {e}")
299307
time.sleep(2**attempt)

0 commit comments

Comments
 (0)