Skip to content

Commit 2760aa6

Browse files
authored
fix: Classify and retry failures while reading a streamed error body (#1020)
Before raising `ApifyApiError`, `_make_request` buffers the response so a streamed error body reaches the exception. That read sat outside the `try` block that classifies an attempt, so a failure there escaped the retry policy entirely: a non-retryable failure was retried through the whole backoff, and the response was left open. The read now goes through the same classification as a failed send — retried when it is a transient transport failure, surfaced immediately when it is not — and the response is closed before the exception propagates. Split out of #1006 and re-targeted at `master`, so the retry fixes can ship in a patch release ahead of the pipeline refactor in #1011. *✍️ Drafted by Claude Code*
1 parent 48f6735 commit 2760aa6

2 files changed

Lines changed: 104 additions & 4 deletions

File tree

‎src/apify_client/http_clients/_impit.py‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import logging
55
import random
66
import time
7+
from contextlib import suppress
78
from datetime import timedelta
89
from http import HTTPStatus
910
from typing import TYPE_CHECKING, Any, TypeVar
@@ -257,8 +258,19 @@ def _make_request(
257258
logger.debug('Status code is not retryable', extra={'status_code': response.status_code})
258259
stop_retrying()
259260

260-
# Read the response in case it is a stream, so we can raise the error properly.
261-
response.read()
261+
# Read the response in case it is a stream, so we can raise the error properly. A failed read goes through
262+
# the same classification as a failed send.
263+
try:
264+
response.read()
265+
except Exception as exc:
266+
logger.debug('Reading the error response failed', exc_info=exc)
267+
with suppress(Exception):
268+
response.close()
269+
if not _is_retryable_error(exc):
270+
logger.debug('Exception is not retryable', exc_info=exc)
271+
stop_retrying()
272+
raise
273+
262274
raise ApifyApiError(response, attempt, method=method)
263275

264276
@staticmethod
@@ -519,8 +531,19 @@ async def _make_request(
519531
logger.debug('Status code is not retryable', extra={'status_code': response.status_code})
520532
stop_retrying()
521533

522-
# Read the response in case it is a stream, so we can raise the error properly.
523-
await response.aread()
534+
# Read the response in case it is a stream, so we can raise the error properly. A failed read goes through
535+
# the same classification as a failed send.
536+
try:
537+
await response.aread()
538+
except Exception as exc:
539+
logger.debug('Reading the error response failed', exc_info=exc)
540+
with suppress(Exception):
541+
await response.aclose()
542+
if not _is_retryable_error(exc):
543+
logger.debug('Exception is not retryable', exc_info=exc)
544+
stop_retrying()
545+
raise
546+
524547
raise ApifyApiError(response, attempt, method=method)
525548

526549
@staticmethod

‎tests/unit/test_http_clients.py‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,83 @@ def test_transient_transport_error_is_retried() -> None:
387387
assert request.call_count == 3
388388

389389

390+
def test_error_response_read_failure_is_retried_and_closed() -> None:
391+
"""A failure while buffering a streamed error body is retried like a failed send, and the response is closed."""
392+
client = ImpitHttpClient(token='test_token', max_retries=1, min_delay_between_retries=timedelta(0))
393+
responses = [
394+
Mock(status_code=500, read=Mock(side_effect=impit.ReadError('truncated')), close=Mock()) for _ in range(2)
395+
]
396+
request = Mock(side_effect=responses)
397+
client._impit_client = Mock(request=request)
398+
399+
with pytest.raises(impit.ReadError):
400+
client.call(method='GET', url='https://api.test.com/endpoint', stream=True)
401+
402+
assert request.call_count == 2
403+
for response in responses:
404+
response.close.assert_called_once()
405+
406+
407+
async def test_error_response_read_failure_is_retried_and_closed_async() -> None:
408+
"""The async client also retries a failed buffering of a streamed error body and closes the response."""
409+
client = ImpitHttpClientAsync(token='test_token', max_retries=1, min_delay_between_retries=timedelta(0))
410+
responses = [
411+
Mock(status_code=500, aread=AsyncMock(side_effect=impit.ReadError('truncated')), aclose=AsyncMock())
412+
for _ in range(2)
413+
]
414+
request = AsyncMock(side_effect=responses)
415+
client._impit_async_client = Mock(request=request)
416+
417+
with pytest.raises(impit.ReadError):
418+
await client.call(method='GET', url='https://api.test.com/endpoint', stream=True)
419+
420+
assert request.await_count == 2
421+
for response in responses:
422+
response.aclose.assert_awaited_once()
423+
424+
425+
def test_non_retryable_error_response_read_failure_stops_retrying() -> None:
426+
"""A read failure that is not a transport error stops the retry loop instead of being retried."""
427+
client = ImpitHttpClient(token='test_token', min_delay_between_retries=timedelta(0))
428+
response = Mock(status_code=500, read=Mock(side_effect=ValueError('broken response')), close=Mock())
429+
request = Mock(return_value=response)
430+
client._impit_client = Mock(request=request)
431+
432+
with pytest.raises(ValueError, match='broken response'):
433+
client.call(method='GET', url='https://api.test.com/endpoint', stream=True)
434+
435+
request.assert_called_once()
436+
response.close.assert_called_once()
437+
438+
439+
async def test_non_retryable_error_response_read_failure_stops_retrying_async() -> None:
440+
"""The async client also stops retrying when a read failure is not a transport error."""
441+
client = ImpitHttpClientAsync(token='test_token', min_delay_between_retries=timedelta(0))
442+
response = Mock(status_code=500, aread=AsyncMock(side_effect=ValueError('broken response')), aclose=AsyncMock())
443+
request = AsyncMock(return_value=response)
444+
client._impit_async_client = Mock(request=request)
445+
446+
with pytest.raises(ValueError, match='broken response'):
447+
await client.call(method='GET', url='https://api.test.com/endpoint', stream=True)
448+
449+
request.assert_awaited_once()
450+
response.aclose.assert_awaited_once()
451+
452+
453+
def test_error_response_read_failure_on_non_retryable_status_is_not_retried() -> None:
454+
"""A transient read failure on a status that is not retryable surfaces immediately instead of being retried."""
455+
client = ImpitHttpClient(token='test_token', min_delay_between_retries=timedelta(0))
456+
response = Mock(status_code=404, read=Mock(side_effect=impit.ReadError('truncated')), close=Mock())
457+
request = Mock(return_value=response)
458+
client._impit_client = Mock(request=request)
459+
460+
with pytest.raises(impit.ReadError):
461+
client.call(method='GET', url='https://api.test.com/endpoint', stream=True)
462+
463+
request.assert_called_once()
464+
response.close.assert_called_once()
465+
466+
390467
@pytest.fixture(
391468
params=[
392469
pytest.param((GzipHttpCompressor(), 'gzip', gzip.decompress), id='gzip'),

0 commit comments

Comments
 (0)