diff --git a/tests/test_fetcher_ng.py b/tests/test_fetcher_ng.py index c956de20ac..53881365d2 100644 --- a/tests/test_fetcher_ng.py +++ b/tests/test_fetcher_ng.py @@ -103,6 +103,50 @@ def test_http_error(self) -> None: self.fetcher.fetch(self.url) self.assertEqual(cm.exception.status_code, 404) + @patch.object(urllib3.HTTPConnectionPool, "_make_request") + def test_http_error_retry(self, mock_request: Mock) -> None: + for status in (502, 503, 504): + with self.subTest(status=status): + mock_request.reset_mock() + mock_request.side_effect = [ + urllib3.HTTPResponse(status=status, body=io.BytesIO()), + urllib3.HTTPResponse( + status=200, + body=io.BytesIO(self.file_contents), + preload_content=False, + ), + ] + data = self.fetcher.download_bytes(self.url, self.file_length) + self.assertEqual(data, self.file_contents) + self.assertEqual(mock_request.call_count, 2) + + @patch.object(urllib3.HTTPConnectionPool, "_make_request") + def test_http_error_retry_exhausted(self, mock_request: Mock) -> None: + for status in (502, 503, 504): + with self.subTest(status=status): + mock_request.reset_mock() + mock_request.side_effect = [ + urllib3.HTTPResponse(status=status, body=io.BytesIO()) + for _ in range(4) + ] + with self.assertRaises(exceptions.DownloadHTTPError) as cm: + self.fetcher.fetch(self.url) + self.assertEqual(cm.exception.status_code, status) + self.assertEqual(mock_request.call_count, 4) + + @patch.object(urllib3.HTTPConnectionPool, "_make_request") + def test_http_error_no_retry(self, mock_request: Mock) -> None: + for status in (400, 403, 404, 500): + with self.subTest(status=status): + mock_request.reset_mock() + mock_request.return_value = urllib3.HTTPResponse( + status=status, body=io.BytesIO() + ) + with self.assertRaises(exceptions.DownloadHTTPError) as cm: + self.fetcher.fetch(self.url) + self.assertEqual(cm.exception.status_code, status) + mock_request.assert_called_once() + # Response read timeout error @patch.object(urllib3.PoolManager, "request") def test_response_read_timeout(self, mock_session_get: Mock) -> None: diff --git a/tuf/ngclient/urllib3_fetcher.py b/tuf/ngclient/urllib3_fetcher.py index 63cd9916d2..7f5b50388f 100644 --- a/tuf/ngclient/urllib3_fetcher.py +++ b/tuf/ngclient/urllib3_fetcher.py @@ -29,6 +29,8 @@ class Urllib3Fetcher(FetcherInterface): """An implementation of ``FetcherInterface`` based on the urllib3 library. + Requests are retried up to three times on HTTP 502, 503 and 504 errors. + Attributes: socket_timeout: Timeout in seconds, used for both initial connection delay and the maximum delay between bytes received. @@ -50,7 +52,14 @@ def __init__( if app_user_agent is not None: ua = f"{app_user_agent} {ua}" - self._proxy_env = ProxyEnvironment(headers={"User-Agent": ua}) + self._proxy_env = ProxyEnvironment( + headers={"User-Agent": ua}, + retries=urllib3.Retry( + total=3, + status_forcelist=(502, 503, 504), + raise_on_status=False, + ), + ) def _fetch(self, url: str) -> Iterator[bytes]: """Fetch the contents of HTTP/HTTPS url from a remote server.