Skip to content

Commit 6a0f84f

Browse files
committed
Add retries to generate_sbom.py
generate_sbom.py downloads artifacts, which can flake. This commit adds exponential backoff to the download to avoid failures due to network flakes.
1 parent 8d490b3 commit 6a0f84f

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

Tools/build/generate_sbom.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sys
1010
import typing
1111
import urllib.request
12+
import urllib.error
1213
from pathlib import Path, PurePosixPath, PureWindowsPath
1314

1415
CPYTHON_ROOT_DIR = Path(__file__).parent.parent.parent
@@ -161,6 +162,21 @@ def get_externals() -> list[str]:
161162
return externals
162163

163164

165+
def download_with_retries(download_location, retries=5):
166+
"""Download a file with exponential backoff retry."""
167+
attempt = 0
168+
while attempt < retries:
169+
attempt += 1
170+
try:
171+
resp = urllib.request.urlopen(download_location)
172+
except urllib.error.URLError as ex:
173+
if attempt == retries:
174+
raise ex
175+
time.sleep(2**attempt)
176+
else:
177+
return resp
178+
179+
164180
def check_sbom_packages(sbom_data: dict[str, typing.Any]) -> None:
165181
"""Make a bunch of assertions about the SBOM package data to ensure it's consistent."""
166182

@@ -175,7 +191,7 @@ def check_sbom_packages(sbom_data: dict[str, typing.Any]) -> None:
175191
# and that the download URL is valid.
176192
if "checksums" not in package or "CI" in os.environ:
177193
download_location = package["downloadLocation"]
178-
resp = urllib.request.urlopen(download_location)
194+
resp = download_with_retries(download_location)
179195
error_if(resp.status != 200, f"Couldn't access URL: {download_location}'")
180196

181197
package["checksums"] = [{

0 commit comments

Comments
 (0)