Skip to content

The agent that grows with you

fix(pm): any 5xx from an upstream host falls back to the pinned mirror

Tekniumf14a599576de
diff --git a/pm/downloader.py b/pm/downloader.py
index 3c7dfed6275..bad802cc5fb 100644
--- a/pm/downloader.py
+++ b/pm/downloader.py
@@ -134,8 +134,11 @@ class DownloadTransportError(DownloadError):
# A host that does not resolve is not worth a retry, but the pinned
# mirror is a different host and the hash still guards the bytes.
unresolved = isinstance(getattr(cause, "reason", cause), socket.gaierror)
+ # Any 5xx is the upstream host failing, not its bytes changing: a CDN's
+ # origin-down codes (520-527) are what a dead third-party host returns.
self.fallback_allowed = (
- self.status in (401, 403, 404, 410) or unresolved or is_transient(cause)
+ self.status in (401, 403, 404, 410) or (self.status or 0) >= 500
+ or unresolved or is_transient(cause)
)
refused = edge_block(cause) or ("the host refused access" if self.status in (401, 403) else None)
reason = f"{cause}; {refused}" if refused else str(cause)
diff --git a/tests/pm/test_download_mirrors.py b/tests/pm/test_download_mirrors.py
index 509b67736b1..255f7b3b72a 100644
--- a/tests/pm/test_download_mirrors.py
+++ b/tests/pm/test_download_mirrors.py
@@ -70,7 +70,7 @@ def test_exhausted_mirrors_name_each_attempted_url(tmp_path, dl_server, monkeypa
assert not (tmp_path / "absent").exists()
[email protected]("failure", ["503", "dns", "tls", "disk", "pause"])
[email protected]("failure", ["503", "521", "dns", "tls", "disk", "pause"])
def test_only_availability_failures_can_use_a_mirror(tmp_path, dl_server, monkeypatch, failure):
import socket
import ssl
@@ -98,13 +98,16 @@ def test_only_availability_failures_can_use_a_mirror(tmp_path, dl_server, monkey
dl.pause()
if failure == "503":
raise urllib.error.HTTPError(request.full_url, 503, "Unavailable", {}, None)
+ if failure == "521":
+ # A CDN's origin-down answer (macOS ffmpeg's only upstream, 2026-10-21).
+ raise urllib.error.HTTPError(request.full_url, 521, "Web Server Is Down", {}, None)
if failure == "dns":
# The macOS runner's error for an upstream host it cannot resolve.
raise urllib.error.URLError(socket.gaierror(socket.EAI_NONAME, "nodename nor servname provided"))
return original(request, **kwargs)
monkeypatch.setattr(downloader._OPENER, "open", open_request)
- if failure == "dns":
+ if failure in ("dns", "521"):
dl.run()
assert (tmp_path / "tool").read_bytes() == payload
assert requests.count(url(dl_server, "/primary")) == 1