diff --git a/roles/update/files/shell/pacman_xfer.sh b/roles/update/files/shell/pacman_xfer.sh new file mode 100755 index 0000000000..6ea895cece --- /dev/null +++ b/roles/update/files/shell/pacman_xfer.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +# Param: $1 output file (pacman's %o), $2 URL (pacman's %u). Exits 0 only on a complete download, else with curl's exit code. A failed attempt is retried 3 times after its partial bytes are dropped, 2 s apart or as long as the Retry-After seconds of a 429/503 ask (at most 30 s); HTTP 404 and 410 fail at once, because every Arch mirror answers the DatabaseOptional .db.sig probe with 404 and pacman ignores that failure. +set -euo pipefail + +output="$1" +url="$2" +offset=0 +if [[ -f "${output}" ]]; then + offset="$(stat -c %s -- "${output}")" +fi + +for retries_left in 3 2 1 0; do + # nocheck: curl-timeout pacman package downloads are size-unbounded; a --max-time would truncate large transfers + reply="$(curl -L -C - -f --connect-timeout 5 -w '%{http_code} %header{retry-after}' -o "${output}" "${url}")" && exit 0 + status=$? + code="${reply%% *}" + retry_after="${reply#* }" + if [[ "${code}" == 404 || "${code}" == 410 ]] || ((retries_left == 0)); then + exit "${status}" + fi + if [[ -f "${output}" ]]; then + truncate -s "${offset}" -- "${output}" + fi + delay=2 + if [[ "${code}" == 429 || "${code}" == 503 ]] && [[ "${retry_after}" =~ ^[0-9]{1,9}$ ]]; then + delay=$((10#${retry_after} < 2 ? 2 : (10#${retry_after} > 30 ? 30 : 10#${retry_after}))) + fi + sleep "${delay}" +done diff --git a/roles/update/tasks/arch.yml b/roles/update/tasks/arch.yml index 84f4018e17..db32c95da5 100644 --- a/roles/update/tasks/arch.yml +++ b/roles/update/tasks/arch.yml @@ -1,11 +1,19 @@ --- +- name: "📦 update (pacman) | install the downloader that retries transient failures" + become: true + ansible.builtin.copy: + src: shell/pacman_xfer.sh + dest: "{{ UPDATE_PACMAN_XFER }}" + owner: root + group: root + mode: "0755" + - name: "🔁 update (pacman) | make pacman retry transient mirror/egress failures natively" become: true ansible.builtin.lineinfile: path: /etc/pacman.conf regexp: "^XferCommand\\s*=" - # nocheck: curl-timeout pacman package downloads are size-unbounded; a --max-time would truncate large transfers - line: "XferCommand = /usr/bin/curl -L -C - -f --connect-timeout 5 --retry 3 --retry-all-errors --retry-delay 2 -o %o %u" + line: "XferCommand = {{ UPDATE_PACMAN_XFER }} %o %u" insertafter: "^\\[options\\]" - name: "🔄 update (pacman) | refresh pacman cache and upgrade all installed packages" diff --git a/roles/update/vars/main.yml b/roles/update/vars/main.yml index d47ea10e89..509226ce0d 100644 --- a/roles/update/vars/main.yml +++ b/roles/update/vars/main.yml @@ -1,2 +1,3 @@ --- application_id: update +UPDATE_PACMAN_XFER: "{{ [ DIR_BIN, 'pacman-xfer' ] | path_join }}" diff --git a/tests/lint/repository/test_shell_download_retry.py b/tests/lint/repository/test_shell_download_retry.py index 7c8acab892..9ea8e43a6d 100644 --- a/tests/lint/repository/test_shell_download_retry.py +++ b/tests/lint/repository/test_shell_download_retry.py @@ -12,6 +12,14 @@ local-API calls that legitimately fast-fail: * wget offender: ``-O `` writing to a real path (not ``-``), or a plain ``wget `` download, without ``--tries=``. +A download the script retries itself counts as retry-hardened only in a +bounded retry loop: a literal counter header (``for n in 3 2 1 0; do`` or +``for ((...)); do``), the download directly in its body leaving the loop on +success (``&& exit 0``, ``&& break`` or ``&& return``), and a ``sleep`` +directly in the body after it. Scripts that decide per failure whether to +retry, e.g. failing fast on a 404 that ``--retry-all-errors`` together with +``-f`` would retry, need that shape. + Exempt: ``-o /dev/null`` / ``-O-`` stdout probes, ``command -v`` checks, and lines suppressed via ``# nocheck: shell-download-retry``. """ @@ -34,42 +42,166 @@ _CURL_OUT_RE = re.compile( ) _WGET_RE = re.compile(r"(? set[int]: + """Return the lines a bounded retry loop repeats until they succeed. + + Args: + lines: the script's lines. + + Returns: + 1-based numbers of every line that sits directly in the body of a + literal counter loop, leaves the loop on success, and is followed + by a ``sleep`` directly in the same body. + """ + retried: set[int] = set() + for header, line in enumerate(lines): + if not _COUNTER_LOOP_RE.search(line): + continue + depth = 0 + leaving: list[int] = [] + for no in range(header + 1, len(lines)): + body = lines[no] + if depth == 0 and _LOOP_END_RE.search(body): + break + if depth == 0 and _LEAVES_ON_SUCCESS_RE.search(body): + leaving.append(no + 1) + elif depth == 0 and _SLEEP_RE.search(body): + retried.update(leaving) + depth += len(_BLOCK_OPEN_RE.findall(body)) + depth -= len(_BLOCK_CLOSE_RE.findall(body)) + return retried + + +def _offenders(rel: str, lines: list[str]) -> list[str]: + """Return one finding per download line that carries no retry. + + Args: + rel: the script's repository-relative path, used in each finding. + lines: the script's lines. + + Returns: + ``:: `` for every unretried download. + """ + retried = _retried_lines(lines) + found: list[str] = [] + for no, line in enumerate(lines, start=1): + is_curl_dl = _CURL_RE.search(line) and _CURL_OUT_RE.search(line) + is_wget_dl = _WGET_RE.search(line) and not _WGET_STDOUT_RE.search(line) + bad = (is_curl_dl and "--retry-all-errors" not in line) or ( + is_wget_dl and "--tries=" not in line and "-t " not in line + ) + if not bad or no in retried or is_suppressed_at(lines, no, _RULE): + continue + found.append(f"{rel}:{no}: {line.strip()[:100]}") + return found + + class TestShellDownloadRetry(unittest.TestCase): def test_shell_downloads_carry_native_retries(self) -> None: offenders: list[str] = [] for glob in _GLOBS: for path in sorted(PROJECT_ROOT.glob(glob)): rel = path.relative_to(PROJECT_ROOT).as_posix() - lines = read_text(str(path)).splitlines() - for no, line in enumerate(lines, start=1): - bad = None - is_curl_dl = _CURL_RE.search(line) and _CURL_OUT_RE.search(line) - is_wget_dl = _WGET_RE.search(line) and not _WGET_STDOUT_RE.search( - line - ) - if is_curl_dl and "--retry-all-errors" not in line: - bad = "curl download without --retry-all-errors" - elif is_wget_dl and "--tries=" not in line and "-t " not in line: - bad = "wget download without --tries=" - if not bad: - continue - if is_suppressed_at(lines, no, _RULE): - continue - offenders.append(f"{rel}:{no}: {line.strip()[:100]}") + offenders += _offenders(rel, read_text(str(path)).splitlines()) if offenders: self.fail( f"{len(offenders)} shell download(s) without native retries. Add " "`--retry-all-errors --retry-delay 2` to curl or `--tries=3 " "--waitretry=2` to wget (files/*.sh cannot use the curl filter), " + "retry it in a bounded retry loop (see this lint's docstring), " f"or mark a legitimate exception with `# nocheck: {_RULE}`:\n" + "\n".join(f" - {o}" for o in offenders) ) + def test_a_bounded_retry_loop_counts_as_retried(self) -> None: + lines = [ + "for retries_left in 3 2 1 0; do", + '\treply="$(curl -L -f -w "%{http_code}" -o "${out}" "${url}")" && exit 0', + '\tif [[ "${reply}" == 404 ]]; then', + "\t\texit 22", + "\tfi", + '\tsleep "${delay}"', + "done", + ] + self.assertEqual(_offenders("x.sh", lines), []) + + def test_an_arithmetic_counter_loop_counts_as_retried(self) -> None: + lines = [ + "for ((attempt = 1; attempt <= 4; attempt++)); do", + '\tcurl -fsSL -o "${out}" "${url}" && break', + "\tsleep 2", + "done", + ] + self.assertEqual(_offenders("x.sh", lines), []) + + def test_a_rate_limited_url_loop_is_flagged(self) -> None: + lines = [ + 'for url in "${urls[@]}"; do', + '\tcurl -fsSL -o "${dir}/${url##*/}" "${url}"', + "\tsleep 1", + "done", + ] + self.assertEqual(_offenders("x.sh", lines), [f"x.sh:2: {lines[1].strip()}"]) + + def test_a_while_read_mirror_loop_is_flagged(self) -> None: + lines = [ + "while read -r mirror; do", + '\tcurl -fsSL -o "${out}" "${mirror}/core.db" && break', + "\tsleep 1", + 'done < "${mirrorlist}"', + ] + self.assertEqual(_offenders("x.sh", lines), [f"x.sh:2: {lines[1].strip()}"]) + + def test_an_hourly_service_loop_is_flagged(self) -> None: + lines = [ + "while true; do", + '\tcurl -fsSL -o "${out}" "${url}"', + "\tsleep 3600", + "done", + ] + self.assertEqual(_offenders("x.sh", lines), [f"x.sh:2: {lines[1].strip()}"]) + + def test_a_sleep_only_in_a_skip_branch_is_flagged(self) -> None: + lines = [ + "for attempt in 1 2 3; do", + '\tif [[ -e "${skip}" ]]; then', + "\t\tsleep 1", + "\t\tcontinue", + "\tfi", + '\tcurl -fsSL -o "${out}" "${url}" && break', + '\tif [[ -e "${slow}" ]]; then', + "\t\tsleep 5", + "\tfi", + "done", + ] + self.assertEqual(_offenders("x.sh", lines), [f"x.sh:6: {lines[5].strip()}"]) + + def test_a_download_that_never_leaves_the_loop_is_flagged(self) -> None: + lines = [ + "for attempt in 1 2 3; do", + '\tcurl -fsSL -o "${out}" "${url}"', + "\tsleep 2", + "done", + ] + self.assertEqual(_offenders("x.sh", lines), [f"x.sh:2: {lines[1].strip()}"]) + + def test_a_download_outside_any_loop_is_flagged(self) -> None: + lines = ['wget -qO "${out}" "${url}"'] + self.assertEqual(_offenders("x.sh", lines), [f"x.sh:1: {lines[0]}"]) + if __name__ == "__main__": # pragma: no cover unittest.main() diff --git a/tests/unit/python/roles/update/__init__.py b/tests/unit/python/roles/update/__init__.py new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/tests/unit/python/roles/update/__init__.py @@ -0,0 +1 @@ + diff --git a/tests/unit/python/roles/update/files/__init__.py b/tests/unit/python/roles/update/files/__init__.py new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/tests/unit/python/roles/update/files/__init__.py @@ -0,0 +1 @@ + diff --git a/tests/unit/python/roles/update/files/test_pacman_xfer.py b/tests/unit/python/roles/update/files/test_pacman_xfer.py new file mode 100644 index 0000000000..cbd3083144 --- /dev/null +++ b/tests/unit/python/roles/update/files/test_pacman_xfer.py @@ -0,0 +1,315 @@ +import http.server +import os +import socket +import subprocess +import tempfile +import threading +import time +import unittest +from pathlib import Path + +from utils import PROJECT_ROOT +from utils.cache.files import read_text + +SCRIPT = PROJECT_ROOT / "roles/update/files/shell/pacman_xfer.sh" +OLD_XFERCOMMAND = ( + "curl -L -C - -f --connect-timeout 5 --retry 3 --retry-all-errors " + "--retry-delay 2 -o %o %u" +) +BODY = "0123456789abcdef" * 65536 +DATA = BODY.encode() +PATH = "/core/core.db" +SLEEP_STUB = '#!/bin/sh\necho "$@" >> "${SLEEP_LOG}"\n' +CURL_STUB = "#!/bin/sh\nprintf '%s\\n' \"$@\" > \"${CURL_ARGV}\"\nprintf '200 '\n" + + +class _Mirror(http.server.ThreadingHTTPServer): + daemon_threads = True + + def __init__(self, plan): + super().__init__(("127.0.0.1", 0), _Handler) + self.plan = plan + self.hits = 0 + self.ranges = [] + self.lock = threading.Lock() + self.retried = threading.Event() + + def handle_error(self, request, client_address): + pass + + +class _Handler(http.server.BaseHTTPRequestHandler): + protocol_version = "HTTP/1.1" + + def log_message(self, *args): + pass + + def do_GET(self): + mirror = self.server + with mirror.lock: + mirror.hits += 1 + hit = mirror.hits + mirror.ranges.append(self.headers.get("Range")) + if hit > 1: + mirror.retried.set() + step = mirror.plan[min(hit, len(mirror.plan)) - 1] + if isinstance(step, str): + getattr(self, f"_{step}")() + elif isinstance(step, tuple): + self._status(*step) + else: + self._status(step, None) + + def _status(self, code, retry_after): + self.send_response(code) + if retry_after is not None: + self.send_header("Retry-After", retry_after) + self.send_header("Content-Length", "9") + self.end_headers() + self.wfile.write(b"error\n\n\n\n") + + def _reset(self): + self.close_connection = True + self.connection.setsockopt( + socket.SOL_SOCKET, socket.SO_LINGER, b"\x01\x00\x00\x00\x00\x00\x00\x00" + ) + os.close(self.connection.detach()) + + def _send(self, cut): + offset = int((self.headers.get("Range") or "bytes=0-")[6:-1]) + self.send_response(206 if offset else 200) + if offset: + self.send_header( + "Content-Range", f"bytes {offset}-{len(DATA) - 1}/{len(DATA)}" + ) + self.send_header("Content-Length", str(len(DATA) - offset)) + self.end_headers() + rest = DATA[offset:] + if not cut: + self.wfile.write(rest) + return + self.wfile.write(rest[: len(rest) // 2]) + self.wfile.flush() + self._reset() + + def _ok(self): + self._send(cut=False) + + def _cut(self): + self._send(cut=True) + + +class TestPacmanXfer(unittest.TestCase): + def setUp(self): + tmp = tempfile.TemporaryDirectory() + self.addCleanup(tmp.cleanup) + self.root = Path(tmp.name) + self.bin_dir = self.root / "bin" + self.bin_dir.mkdir() + self.stub("sleep", SLEEP_STUB) + self.sleep_log = self.root / "sleeps" + self.output = self.root / "core.db.part" + self.plain_env = dict(os.environ) + self.plain_env["no_proxy"] = self.plain_env["NO_PROXY"] = "127.0.0.1" + self.env = dict(self.plain_env) + self.env["PATH"] = f"{self.bin_dir}{os.pathsep}{self.env['PATH']}" + self.env["SLEEP_LOG"] = str(self.sleep_log) + + def stub(self, name, body): + """Put an executable stub first on the wrapper's PATH. + + Args: + name: the command the stub replaces. + body: the stub's shell script. + """ + stub = self.bin_dir / name + stub.write_text(body) + stub.chmod(0o755) + + def serve(self, plan): + """Start a mirror on 127.0.0.1 that answers PATH by plan. + + Args: + plan: one step per request, the last one repeats: "ok", "cut" + (half the body, then a reset) or "reset" (before any + response), an HTTP status code, or a (status, Retry-After) + tuple. + + Returns: + The running mirror and the URL of PATH on it. + """ + mirror = _Mirror(plan) + threading.Thread( + target=mirror.serve_forever, kwargs={"poll_interval": 0.05}, daemon=True + ).start() + self.addCleanup(mirror.server_close) + self.addCleanup(mirror.shutdown) + return mirror, f"http://127.0.0.1:{mirror.server_address[1]}{PATH}" + + def xfer(self, plan, partial=None, env=None): + """Run the wrapper the way pacman does, with %o and %u filled in. + + Args: + plan: the mirror's steps, see serve(). + partial: text the output file holds before the run, or None + when it does not exist. + env: environment for the run, or None for the one with the + sleep stub. + + Returns: + Tuple of exit code, the mirror, and the output file's text after + the run (None when it does not exist). + """ + mirror, url = self.serve(plan) + if partial is not None: + self.output.write_text(partial) + done = subprocess.run( + ["bash", str(SCRIPT), str(self.output), url], + env=self.env if env is None else env, + capture_output=True, + timeout=60, + check=False, + ) + after = read_text(str(self.output)) if self.output.exists() else None + return done.returncode, mirror, after + + def sleeps(self): + """Return the arguments of every sleep the wrapper ran, in order.""" + if not self.sleep_log.exists(): + return [] + return read_text(str(self.sleep_log)).split() + + def assert_retried_once(self, step): + """Assert that one failing answer is retried and the retry succeeds. + + Args: + step: the mirror's first answer, see serve(). + """ + code, mirror, after = self.xfer([step, "ok"]) + self.assertEqual(code, 0) + self.assertEqual(mirror.hits, 2) + self.assertEqual(after, BODY) + + def test_old_xfercommand_retries_a_404(self): + mirror, url = self.serve([404]) + argv = [ + arg.replace("%o", str(self.output)).replace("%u", url) + for arg in OLD_XFERCOMMAND.split() + ] + proc = subprocess.Popen( + argv, env=self.env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL + ) + try: + retried = mirror.retried.wait(timeout=30) + finally: + proc.kill() + proc.wait() + self.assertTrue(retried) + + def test_a_404_fails_at_once_without_a_file(self): + code, mirror, after = self.xfer([404]) + self.assertEqual(code, 22) + self.assertEqual(mirror.hits, 1) + self.assertIsNone(after) + + def test_a_410_fails_at_once(self): + code, mirror, _ = self.xfer([410]) + self.assertEqual(code, 22) + self.assertEqual(mirror.hits, 1) + + def test_a_404_leaves_a_partial_download_untouched(self): + code, mirror, after = self.xfer([404], partial=BODY[:1000]) + self.assertEqual(code, 22) + self.assertEqual(mirror.hits, 1) + self.assertEqual(after, BODY[:1000]) + + def test_a_403_is_retried(self): + self.assert_retried_once(403) + + def test_a_408_is_retried(self): + self.assert_retried_once(408) + + def test_a_421_is_retried(self): + self.assert_retried_once(421) + + def test_a_429_is_retried(self): + self.assert_retried_once(429) + + def test_a_503_is_retried(self): + self.assert_retried_once(503) + + def test_a_reset_connection_is_retried(self): + self.assert_retried_once("reset") + + def test_a_cut_transfer_drops_its_bytes_before_the_retry(self): + code, mirror, after = self.xfer(["cut", "ok"]) + self.assertEqual(code, 0) + self.assertEqual(mirror.ranges, [None, None]) + self.assertEqual(after, BODY) + + def test_a_cut_resume_retries_from_the_original_offset(self): + code, mirror, after = self.xfer(["cut", "ok"], partial=BODY[:1000]) + self.assertEqual(code, 0) + self.assertEqual(mirror.ranges, ["bytes=1000-", "bytes=1000-"]) + self.assertEqual(after, BODY) + + def test_a_partial_download_resumes(self): + code, mirror, after = self.xfer(["ok"], partial=BODY[:1000]) + self.assertEqual(code, 0) + self.assertEqual(mirror.ranges, ["bytes=1000-"]) + self.assertEqual(after, BODY) + + def test_a_lasting_failure_exits_non_zero_after_three_retries(self): + code, mirror, _ = self.xfer([503]) + self.assertEqual(code, 22) + self.assertEqual(mirror.hits, 4) + self.assertEqual(self.sleeps(), ["2", "2", "2"]) + + def test_a_retry_after_waits_that_long(self): + start = time.monotonic() + code, mirror, _ = self.xfer([(429, "4"), "ok"], env=self.plain_env) + waited = time.monotonic() - start + self.assertEqual(code, 0) + self.assertEqual(mirror.hits, 2) + self.assertGreaterEqual(waited, 4) + self.assertLess(waited, 15) + + def test_a_retry_after_on_503_sets_the_delay(self): + self.xfer([(503, "4"), "ok"]) + self.assertEqual(self.sleeps(), ["4"]) + + def test_a_retry_after_is_capped_at_30_seconds(self): + self.xfer([(503, "120"), "ok"]) + self.assertEqual(self.sleeps(), ["30"]) + + def test_a_retry_after_below_2_seconds_keeps_2(self): + self.xfer([(429, "0"), "ok"]) + self.assertEqual(self.sleeps(), ["2"]) + + def test_a_retry_after_that_is_no_seconds_count_keeps_2(self): + values = ("Wed, 21 Oct 2026 07:28:00 GMT", "soon", "-5", "4.5") + for index, value in enumerate(values): + with self.subTest(value=value): + self.sleep_log = self.root / f"sleeps-{index}" + self.env["SLEEP_LOG"] = str(self.sleep_log) + self.output = self.root / f"core-{index}.db.part" + self.xfer([(503, value), "ok"]) + self.assertEqual(self.sleeps(), ["2"]) + + def test_a_retry_after_on_another_status_is_ignored(self): + self.xfer([(500, "4"), "ok"]) + self.assertEqual(self.sleeps(), ["2"]) + + def test_curl_gets_a_connect_timeout(self): + argv_log = self.root / "curl-argv" + self.stub("curl", CURL_STUB) + self.env["CURL_ARGV"] = str(argv_log) + code, _, _ = self.xfer(["ok"]) + argv = read_text(str(argv_log)).splitlines() + self.assertEqual(code, 0) + self.assertIn("--connect-timeout", argv) + self.assertEqual(argv[argv.index("--connect-timeout") + 1], "5") + + +if __name__ == "__main__": + unittest.main()