From abab458d743b87b2d37bbbfc2aae9b54bb8d27e2 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sat, 24 Oct 2026 14:44:12 +0200 Subject: [PATCH] Feed reads: one more try when the first times out or the connection fails (the first wait is 12 s, the second the full 30 s); about one github.com request in 25 hung for 30 s and the first-run card said offline --- strata_control/transfer.py | 26 +++++++++++-- tests/terminal/test_read_small_retry.py | 51 +++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 tests/terminal/test_read_small_retry.py diff --git a/strata_control/transfer.py b/strata_control/transfer.py index aaeadfd7..0ffbd9dc 100644 --- a/strata_control/transfer.py +++ b/strata_control/transfer.py @@ -156,17 +156,35 @@ def _remember_date(response) -> None: pass +FIRST_TRY_SECONDS = 12.0 + + +def _open_small(request, timeout: float, limit: int) -> bytes: + with _opener.open(request, timeout=timeout) as response: + _remember_date(response) + return response.read(limit + 1) + + def read_small(location: str, *, limit: int = SMALL_LIMIT, timeout: float = 30) -> bytes: - """A small document (the feed, its signature). Larger than `limit` is refused, not truncated.""" + """A small document (the feed, its signature). Larger than `limit` is refused, not truncated. + A web address gets one more try when the first one times out or the connection fails: about one request in 25 to github.com hung for the whole + timeout on a PC whose next 24 answered in half a second (measured on a 3060 PC: the first-run card said "offline" a moment before the install's + own read worked). The first try therefore waits at most 12 s; the second the full `timeout`.""" kind = kind_of(location) _SERVER_DATE.clear() # only the answer of this read counts try: if kind in ("http", "https"): check_url_policy(location) request = urllib.request.Request(location, headers={"User-Agent": _USER_AGENT, "Cache-Control": "no-cache"}) - with _opener.open(request, timeout=timeout) as response: - _remember_date(response) - data = response.read(limit + 1) + try: + data = _open_small(request, min(timeout, FIRST_TRY_SECONDS), limit) + except urllib.error.HTTPError: + raise + except (urllib.error.URLError, OSError, TimeoutError) as first: + if tls.is_certificate_error(first): + raise + _SERVER_DATE.clear() + data = _open_small(request, timeout, limit) else: path = _local_path(location) if path.stat().st_size > limit: diff --git a/tests/terminal/test_read_small_retry.py b/tests/terminal/test_read_small_retry.py new file mode 100644 index 00000000..561b1082 --- /dev/null +++ b/tests/terminal/test_read_small_retry.py @@ -0,0 +1,51 @@ +"""A feed read that hangs once is tried once more: about one request in 25 to github.com hung for the whole 30 s while the next answered in 0.4 s, +and the app's first-run card said "offline" a moment before the install's own read worked.""" +import io +import unittest +import urllib.error +from unittest import mock + +from strata_control import transfer + + +class Answer(io.BytesIO): + headers = {} + + def __enter__(self): + return self + + def __exit__(self, *a): + return False + + +class ReadSmallRetry(unittest.TestCase): + def read(self, side_effect): + with mock.patch.object(transfer, "check_url_policy", lambda url: None), mock.patch.object(transfer._opener, "open", side_effect=side_effect) as op: + try: + return transfer.read_small("https://updates.example.org/feed.json", timeout=30), op + except transfer.TransferError as exc: + return exc, op + + def test_one_timeout_is_forgiven_and_the_first_wait_is_short(self): + got, op = self.read([urllib.error.URLError(TimeoutError("timed out")), Answer(b"{}")]) + self.assertEqual(got, b"{}") + self.assertEqual([c.kwargs["timeout"] for c in op.call_args_list], [transfer.FIRST_TRY_SECONDS, 30]) + + def test_two_timeouts_still_say_so(self): + got, op = self.read([urllib.error.URLError(TimeoutError("timed out"))] * 2) + self.assertIsInstance(got, transfer.TransferError) + self.assertEqual(op.call_count, 2) + + def test_an_answer_of_404_is_not_tried_again(self): + err = urllib.error.HTTPError("https://x", 404, "no", {}, None) + got, op = self.read([err]) + self.assertIsInstance(got, transfer.TransferError) + self.assertEqual(op.call_count, 1) + + def test_a_good_first_answer_is_one_request(self): + got, op = self.read([Answer(b"{}")]) + self.assertEqual((got, op.call_count), (b"{}", 1)) + + +if __name__ == "__main__": + unittest.main()