44import logging
55import random
66import time
7+ from contextlib import suppress
78from datetime import timedelta
89from http import HTTPStatus
910from typing import TYPE_CHECKING , Any , TypeVar
@@ -53,6 +54,13 @@ def _is_retryable_error(exc: Exception) -> bool:
5354 )
5455
5556
57+ def _stop_retrying_if_permanent (exc : Exception , * , stop_retrying : Callable [[], None ]) -> None :
58+ """Stop the retry loop when an exception is not a transient transport failure."""
59+ if not _is_retryable_error (exc ):
60+ logger .debug ('Exception is not retryable' , exc_info = exc )
61+ stop_retrying ()
62+
63+
5664@docs_group ('HTTP clients' )
5765class ImpitHttpClient (HttpClient ):
5866 """Synchronous HTTP client for the Apify API built on top of [Impit](https://github.com/apify/impit).
@@ -232,9 +240,7 @@ def _make_request(
232240
233241 except Exception as exc :
234242 logger .debug ('Request threw exception' , exc_info = exc )
235- if not _is_retryable_error (exc ):
236- logger .debug ('Exception is not retryable' , exc_info = exc )
237- stop_retrying ()
243+ _stop_retrying_if_permanent (exc , stop_retrying = stop_retrying )
238244 raise
239245
240246 # Retry only server errors (5xx) and rate limits (429).
@@ -246,8 +252,17 @@ def _make_request(
246252 logger .debug ('Status code is not retryable' , extra = {'status_code' : response .status_code })
247253 stop_retrying ()
248254
249- # Read the response in case it is a stream, so we can raise the error properly.
250- response .read ()
255+ # Read the response in case it is a stream, so we can raise the error properly. A read that fails is a
256+ # transport failure like any other, so it goes through the same classification as a failed send.
257+ try :
258+ response .read ()
259+ except Exception as exc :
260+ logger .debug ('Reading the error response failed' , exc_info = exc )
261+ with suppress (Exception ):
262+ response .close ()
263+ _stop_retrying_if_permanent (exc , stop_retrying = stop_retrying )
264+ raise
265+
251266 raise ApifyApiError (response , attempt , method = method )
252267
253268 @staticmethod
@@ -494,9 +509,7 @@ async def _make_request(
494509
495510 except Exception as exc :
496511 logger .debug ('Request threw exception' , exc_info = exc )
497- if not _is_retryable_error (exc ):
498- logger .debug ('Exception is not retryable' , exc_info = exc )
499- stop_retrying ()
512+ _stop_retrying_if_permanent (exc , stop_retrying = stop_retrying )
500513 raise
501514
502515 # Retry only server errors (5xx) and rate limits (429).
@@ -508,8 +521,17 @@ async def _make_request(
508521 logger .debug ('Status code is not retryable' , extra = {'status_code' : response .status_code })
509522 stop_retrying ()
510523
511- # Read the response in case it is a stream, so we can raise the error properly.
512- await response .aread ()
524+ # Read the response in case it is a stream, so we can raise the error properly. A read that fails is a
525+ # transport failure like any other, so it goes through the same classification as a failed send.
526+ try :
527+ await response .aread ()
528+ except Exception as exc :
529+ logger .debug ('Reading the error response failed' , exc_info = exc )
530+ with suppress (Exception ):
531+ await response .aclose ()
532+ _stop_retrying_if_permanent (exc , stop_retrying = stop_retrying )
533+ raise
534+
513535 raise ApifyApiError (response , attempt , method = method )
514536
515537 @staticmethod
0 commit comments