Skip to content

Commit bb504ef

Browse files
committed
Retry the PyPI lookup in the version check
check_version read the latest release from PyPI with one request and no timeout, so a single dropped connection failed a required check and blocked the merge until someone reran the job. The request now has a 15 s timeout and is retried up to four times, with 2, 4 and 8 s pauses, on connection errors, timeouts and 5xx responses. If PyPI still can't be read, the job fails with a message that names PyPI and says the PR's version isn't the problem. The version comparison is unchanged. Also quotes $GITHUB_ENV, which actionlint's shellcheck pass flagged.
1 parent 4e28391 commit bb504ef

1 file changed

Lines changed: 46 additions & 4 deletions

File tree

‎.github/workflows/version-check.yml‎

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,24 @@ jobs:
3131
3232
# Get version from current PR
3333
PR_VERSION=$(grep -o "__version__.*" socketsecurity/__init__.py | awk '{print $3}' | tr -d "'")
34-
echo "PR_VERSION=$PR_VERSION" >> $GITHUB_ENV
34+
echo "PR_VERSION=$PR_VERSION" >> "$GITHUB_ENV"
3535
3636
# Get version from main branch
3737
MAIN_VERSION=$(git show origin/main:socketsecurity/__init__.py | grep -o "__version__.*" | awk '{print $3}' | tr -d "'")
38-
echo "MAIN_VERSION=$MAIN_VERSION" >> $GITHUB_ENV
38+
echo "MAIN_VERSION=$MAIN_VERSION" >> "$GITHUB_ENV"
3939
4040
export PR_VERSION
4141
export MAIN_VERSION
4242
4343
# Compare against the latest published PyPI release.
4444
python3 <<'PY'
45+
import http.client
4546
import json
4647
import os
4748
import subprocess
49+
import time
4850
import tomllib
51+
import urllib.error
4952
import urllib.request
5053
from packaging import version
5154
@@ -72,8 +75,47 @@ jobs:
7275
)
7376
raise SystemExit(1)
7477
75-
with urllib.request.urlopen("https://pypi.org/pypi/socketsecurity/json") as response:
76-
pypi_data = json.load(response)
78+
# This is a required check, so a dropped connection to PyPI would
79+
# otherwise block the merge until someone reruns the job. Retry
80+
# network errors, timeouts and 5xx responses, and if PyPI still can't
81+
# be read, say so instead of failing with a bare traceback that looks
82+
# like a version problem. OSError covers URLError, timeouts and SSL
83+
# errors; HTTPException covers a response cut off mid-read, which
84+
# urllib doesn't wrap in URLError.
85+
pypi_url = "https://pypi.org/pypi/socketsecurity/json"
86+
attempts = 4
87+
pypi_data = None
88+
for attempt in range(1, attempts + 1):
89+
try:
90+
with urllib.request.urlopen(pypi_url, timeout=15) as response:
91+
pypi_data = json.load(response)
92+
break
93+
except urllib.error.HTTPError as exc:
94+
if exc.code < 500:
95+
print(
96+
f"❌ PyPI returned HTTP {exc.code} for {pypi_url}, so the "
97+
f"latest published version couldn't be read. This is "
98+
f"not a problem with the version in this PR."
99+
)
100+
raise SystemExit(1)
101+
error = exc
102+
except (OSError, http.client.HTTPException) as exc:
103+
error = exc
104+
if attempt < attempts:
105+
delay = 2 ** attempt
106+
print(
107+
f"⚠️ PyPI request failed (attempt {attempt}/{attempts}): "
108+
f"{error!r}. Retrying in {delay}s."
109+
)
110+
time.sleep(delay)
111+
112+
if pypi_data is None:
113+
print(
114+
f"❌ Couldn't reach PyPI ({pypi_url}) after {attempts} attempts; "
115+
f"last error: {error!r}. This is a network problem, not a "
116+
f"problem with the version in this PR. Rerun the job."
117+
)
118+
raise SystemExit(1)
77119
78120
published_versions = []
79121
for raw in pypi_data.get("releases", {}).keys():

0 commit comments

Comments
 (0)