Skip to content

Commit

Permalink
Retry downloads when <75% of the file is downloaded (#496)
Browse files Browse the repository at this point in the history
Closes #489
(hopefully)
  • Loading branch information
zanieb authored Jan 14, 2025
1 parent 2d8972d commit 6a1b95d
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pythonbuild/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ def write_target_settings(targets, dest_path: pathlib.Path):
class IntegrityError(Exception):
"""Represents an integrity error when downloading a URL."""

def __init__(self, *args, length: int):
self.length = length
super().__init__(*args)


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

break
except IntegrityError:
except IntegrityError as e:
tmp.unlink()
raise
# If we didn't get most of the expected file, retry
if e.length > size * 0.75:
raise
print(f"Integrity error on {url}; retrying: {e}")
time.sleep(2**attempt)
except http.client.HTTPException as e:
print(f"HTTP exception on {url}; retrying: {e}")
time.sleep(2**attempt)
Expand Down

0 comments on commit 6a1b95d

Please sign in to comment.