Skip to content

Commit 01217ac

Browse files
bootstrap.py: fix incomplete reads when fetching from Hackage (#10999)
* bootstrap.py: fix incomplete reads when fetching from Hackage fix #10998 The fix was suggested by ChatGPT. * fixup! * fixup! * fixup! * fixup! --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent b2fa5ec commit 01217ac

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

bootstrap/bootstrap.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import sys
2525
import tempfile
2626
import urllib.request
27+
import http.client
28+
import time
2729
from textwrap import dedent
2830
from typing import Optional, Dict, List, Tuple, \
2931
NewType, BinaryIO, NamedTuple
@@ -136,6 +138,20 @@ def __str__(self):
136138
f' found: {self.found_sha256}',
137139
])
138140

141+
def download_with_retry(url, output_path, retries=3, delay=2):
142+
for attempt in range(retries):
143+
try:
144+
with urllib.request.urlopen(url, timeout=10) as resp:
145+
with output_path.open('wb') as out_file:
146+
shutil.copyfileobj(resp, out_file)
147+
return # success
148+
except http.client.IncompleteRead as e:
149+
print(f"IncompleteRead error (attempt {attempt + 1}): {e}")
150+
except Exception as e:
151+
print(f"Other error (attempt {attempt + 1}): {e}")
152+
time.sleep(delay)
153+
raise Exception(f"Failed to download after {retries} attempts.")
154+
139155
def package_url(package: PackageName, version: Version) -> str:
140156
return f'http://hackage.haskell.org/package/{package}-{version}/{package}-{version}.tar.gz'
141157

@@ -351,8 +367,7 @@ def fetch_from_plan(plan : FetchPlan, output_dir : Path):
351367
sha = plan[path].sha256
352368
if not output_path.exists():
353369
print(f'Fetching {url}...')
354-
with urllib.request.urlopen(url, timeout = 10) as resp:
355-
shutil.copyfileobj(resp, output_path.open('wb'))
370+
download_with_retry(url, output_path)
356371
verify_sha256(sha, output_path)
357372

358373
def gen_fetch_plan(info : BootstrapInfo) -> FetchPlan :

0 commit comments

Comments
 (0)