Skip to content

Commit 5c18f4b

Browse files
committed
fix: Fail fast on permanent transport errors and retry streamed error-read failures
1 parent e6f24c5 commit 5c18f4b

4 files changed

Lines changed: 167 additions & 14 deletions

File tree

‎src/apify_client/http_clients/_base.py‎

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import random
88
import sys
99
import time
10+
from contextlib import suppress
1011
from datetime import UTC, datetime, timedelta
1112
from http import HTTPStatus
1213
from importlib import metadata
@@ -365,6 +366,22 @@ def _handle_request_exception(self, exc: Exception, *, stop_retrying: Callable[[
365366
logger.debug('Exception is not retryable', exc_info=exc)
366367
stop_retrying()
367368

369+
@staticmethod
370+
def _is_transient_transport_error(
371+
exc: Exception,
372+
*,
373+
transport_errors: type[Exception] | tuple[type[Exception], ...],
374+
permanent_errors: tuple[type[Exception], ...],
375+
) -> bool:
376+
"""Apply the shared retry policy to an exception raised by an HTTP library.
377+
378+
Every error from the library's own hierarchy counts as transient except the permanently-failing types the
379+
adapter lists. Retrying is the default because transports also report genuinely transient failures through
380+
their generic base class, e.g. Impit raises a bare `impit.HTTPError` for a body that ends mid-chunk. Sharing
381+
one policy across adapters keeps a change of transport from changing which failures are retried.
382+
"""
383+
return isinstance(exc, transport_errors) and not isinstance(exc, permanent_errors)
384+
368385

369386
@docs_group('HTTP clients')
370387
class HttpClient(HttpClientBase):
@@ -559,8 +576,14 @@ def _make_request(
559576
logger.debug('Status code is not retryable', extra={'status_code': response.status_code})
560577
stop_retrying()
561578

562-
# Read the response in case it is a stream, so the error can be raised properly.
563-
response.read()
579+
try:
580+
response.read()
581+
except Exception as exc:
582+
with suppress(Exception):
583+
response.close()
584+
self._handle_request_exception(exc, stop_retrying=stop_retrying)
585+
raise
586+
564587
raise ApifyApiError(response, attempt, method=method)
565588

566589

@@ -764,6 +787,12 @@ async def _make_request(
764787
logger.debug('Status code is not retryable', extra={'status_code': response.status_code})
765788
stop_retrying()
766789

767-
# Read the response in case it is a stream, so the error can be raised properly.
768-
await response.aread()
790+
try:
791+
await response.aread()
792+
except Exception as exc:
793+
with suppress(Exception):
794+
await response.aclose()
795+
self._handle_request_exception(exc, stop_retrying=stop_retrying)
796+
raise
797+
769798
raise ApifyApiError(response, attempt, method=method)

‎src/apify_client/http_clients/_impit.py‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@
2222
from apify_client._statistics import ClientStatistics
2323
from apify_client.http_compressors._base import HttpCompressor
2424

25+
_PERMANENT_ERRORS = (
26+
# A bad URL scheme or a request Impit itself rejects cannot succeed on a retry.
27+
impit.UnsupportedProtocol,
28+
impit.LocalProtocolError,
29+
# An over-long redirect chain is a routing loop, which repeating the request cannot break.
30+
impit.TooManyRedirects,
31+
# Status codes are retried by `_make_request` based on the status itself, never as a transport error.
32+
impit.HTTPStatusError,
33+
)
34+
"""Impit errors that a retry cannot fix. Everything else in the `impit.HTTPError` tree is treated as transient."""
35+
2536

2637
@docs_group('HTTP clients')
2738
class ImpitHttpClient(HttpClient):
@@ -115,9 +126,11 @@ def send_request(
115126

116127
@override
117128
def is_retryable_transport_error(self, exc: Exception) -> bool:
118-
# All errors from Impit's own hierarchy count as transient - they represent transport-level failures
119-
# (network issues, timeouts, protocol errors, body decoding errors) that are typically transient.
120-
return isinstance(exc, impit.HTTPError)
129+
return self._is_transient_transport_error(
130+
exc,
131+
transport_errors=impit.HTTPError,
132+
permanent_errors=_PERMANENT_ERRORS,
133+
)
121134

122135

123136
@docs_group('HTTP clients')
@@ -208,6 +221,8 @@ async def send_request(
208221

209222
@override
210223
def is_retryable_transport_error(self, exc: Exception) -> bool:
211-
# All errors from Impit's own hierarchy count as transient - they represent transport-level failures
212-
# (network issues, timeouts, protocol errors, body decoding errors) that are typically transient.
213-
return isinstance(exc, impit.HTTPError)
224+
return self._is_transient_transport_error(
225+
exc,
226+
transport_errors=impit.HTTPError,
227+
permanent_errors=_PERMANENT_ERRORS,
228+
)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
from __future__ import annotations
2+
3+
from datetime import timedelta
4+
from typing import TYPE_CHECKING
5+
from unittest.mock import AsyncMock, Mock
6+
7+
import impit
8+
import pytest
9+
10+
from apify_client.http_clients import ImpitHttpClient, ImpitHttpClientAsync
11+
12+
if TYPE_CHECKING:
13+
from _pytest.monkeypatch import MonkeyPatch
14+
15+
16+
def successful_response() -> Mock:
17+
return Mock(status_code=200)
18+
19+
20+
def test_generic_http_error_is_retried(monkeypatch: MonkeyPatch) -> None:
21+
"""A bare Impit HTTPError from a truncated body remains retryable."""
22+
client = ImpitHttpClient(min_delay_between_retries=timedelta(0))
23+
send_request = Mock(
24+
side_effect=[impit.HTTPError('unexpected EOF'), impit.HTTPError('unexpected EOF'), successful_response()]
25+
)
26+
monkeypatch.setattr(client, 'send_request', send_request)
27+
28+
response = client.call(method='GET', url='https://example.com')
29+
30+
assert response.status_code == 200
31+
assert send_request.call_count == 3
32+
33+
34+
async def test_generic_http_error_is_retried_async(monkeypatch: MonkeyPatch) -> None:
35+
"""The async Impit adapter treats a bare HTTPError from a truncated body the same way."""
36+
client = ImpitHttpClientAsync(min_delay_between_retries=timedelta(0))
37+
send_request = AsyncMock(
38+
side_effect=[impit.HTTPError('unexpected EOF'), impit.HTTPError('unexpected EOF'), successful_response()]
39+
)
40+
monkeypatch.setattr(client, 'send_request', send_request)
41+
42+
response = await client.call(method='GET', url='https://example.com')
43+
44+
assert response.status_code == 200
45+
assert send_request.call_count == 3
46+
47+
48+
def test_permanent_transport_error_is_not_retried(monkeypatch: MonkeyPatch) -> None:
49+
"""A transport error a retry cannot fix must fail on the first attempt instead of burning the whole backoff."""
50+
client = ImpitHttpClient(min_delay_between_retries=timedelta(0))
51+
send_request = Mock(side_effect=impit.UnsupportedProtocol('unsupported scheme'))
52+
monkeypatch.setattr(client, 'send_request', send_request)
53+
54+
with pytest.raises(impit.UnsupportedProtocol):
55+
client.call(method='GET', url='https://example.com')
56+
57+
assert send_request.call_count == 1
58+
59+
60+
def test_error_response_read_failure_uses_transport_retry_policy(monkeypatch: MonkeyPatch) -> None:
61+
"""Failures while reading a streamed 5xx error body are classified and retried like send failures."""
62+
client = ImpitHttpClient(max_retries=1, min_delay_between_retries=timedelta(0))
63+
responses = [
64+
Mock(status_code=500, read=Mock(side_effect=impit.ReadError('truncated')), close=Mock()) for _ in range(2)
65+
]
66+
send_request = Mock(side_effect=responses)
67+
monkeypatch.setattr(client, 'send_request', send_request)
68+
69+
with pytest.raises(impit.ReadError):
70+
client.call(method='GET', url='https://example.com', stream=True)
71+
72+
assert send_request.call_count == 2
73+
for response in responses:
74+
response.close.assert_called_once()
75+
76+
77+
async def test_error_response_read_failure_uses_transport_retry_policy_async(monkeypatch: MonkeyPatch) -> None:
78+
"""The async base also classifies failures while buffering streamed error responses."""
79+
client = ImpitHttpClientAsync(max_retries=1, min_delay_between_retries=timedelta(0))
80+
responses = [
81+
Mock(status_code=500, aread=AsyncMock(side_effect=impit.ReadError('truncated')), aclose=AsyncMock())
82+
for _ in range(2)
83+
]
84+
send_request = AsyncMock(side_effect=responses)
85+
monkeypatch.setattr(client, 'send_request', send_request)
86+
87+
with pytest.raises(impit.ReadError):
88+
await client.call(method='GET', url='https://example.com', stream=True)
89+
90+
assert send_request.call_count == 2
91+
for response in responses:
92+
response.aclose.assert_awaited_once()

‎tests/unit/test_http_clients.py‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,22 +337,39 @@ def test_parse_params_mixed() -> None:
337337
}
338338

339339

340-
TRANSPORT_ERRORS = (
340+
RETRYABLE_TRANSPORT_ERRORS = (
341+
# Impit raises a bare `HTTPError` for a body that ends mid-chunk, so even the generic base class is transient.
341342
impit.HTTPError,
342343
impit.TimeoutException,
343344
impit.NetworkError,
344345
impit.RemoteProtocolError,
345346
impit.DecodingError,
347+
# A proxy rejecting the tunnel is often transient, e.g. one that is overloaded or rate-limiting.
348+
impit.ProxyError,
346349
)
347-
"""Transport errors from Impit's own hierarchy, all classified as retryable."""
350+
"""Transport errors that must stay retryable."""
351+
352+
NON_RETRYABLE_TRANSPORT_ERRORS = (
353+
impit.UnsupportedProtocol,
354+
impit.LocalProtocolError,
355+
impit.TooManyRedirects,
356+
)
357+
"""Transport errors that a retry cannot fix, so they must fail on the first attempt.
358+
359+
`impit.HTTPStatusError` belongs here too, but no built-in adapter ever raises it, because `_make_request` decides on
360+
status codes from the response itself.
361+
"""
348362

349363

350364
def test_builtin_http_client_retry_policy() -> None:
351-
"""Every transport-level Impit failure is classified as retryable, everything else is not."""
365+
"""Transient transport failures are retried, and the ones a retry cannot fix stop the loop immediately."""
352366
with ImpitHttpClient() as client:
353-
for error_class in TRANSPORT_ERRORS:
367+
for error_class in RETRYABLE_TRANSPORT_ERRORS:
354368
assert client.is_retryable_transport_error(error_class('test')), error_class.__name__
355369

370+
for error_class in NON_RETRYABLE_TRANSPORT_ERRORS:
371+
assert not client.is_retryable_transport_error(error_class('test')), error_class.__name__
372+
356373
# `InvalidResponseBodyError` is retried by `_handle_request_exception`, not as a transport failure.
357374
assert not client.is_retryable_transport_error(InvalidResponseBodyError(Mock()))
358375
assert not client.is_retryable_transport_error(ValueError('test'))

0 commit comments

Comments
 (0)