From f27ce207933f8d5227196594341905a84f6cc6e9 Mon Sep 17 00:00:00 2001 From: devteamaegis Date: Sat, 14 Nov 2026 14:16:08 -0500 Subject: [PATCH] Wrap AIOHTTPClient body-read errors in APIConnectionError aiohttp's session.request() returns once the response headers arrive, and request_async read the body afterwards, outside the try that maps transport errors to APIConnectionError. A dropped connection or timeout mid-body therefore escaped as a raw aiohttp.ClientPayloadError or TimeoutError, and max_network_retries never applied. Fixes #1993 --- ...amaegis_aiohttp-body-read-errors.change.md | 8 ++++ stripe/_http_client.py | 9 ++++- tests/test_http_client.py | 37 +++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 .hark/changes/2026-11-14_devteamaegis_aiohttp-body-read-errors.change.md diff --git a/.hark/changes/2026-11-14_devteamaegis_aiohttp-body-read-errors.change.md b/.hark/changes/2026-11-14_devteamaegis_aiohttp-body-read-errors.change.md new file mode 100644 index 000000000..61adc17dc --- /dev/null +++ b/.hark/changes/2026-11-14_devteamaegis_aiohttp-body-read-errors.change.md @@ -0,0 +1,8 @@ +--- +title: Raise `APIConnectionError` and retry when `AIOHTTPClient` fails while reading a response body +semver_level: patch +github_issues_resolved: +- https://github.com/stripe/stripe-python/issues/1993 +--- + +Previously a dropped connection or timeout after the response headers arrived surfaced as a raw `aiohttp.ClientPayloadError` or `TimeoutError` and was not retried. diff --git a/stripe/_http_client.py b/stripe/_http_client.py index f11f6c600..fd92c7bca 100644 --- a/stripe/_http_client.py +++ b/stripe/_http_client.py @@ -1448,7 +1448,14 @@ async def request_async( method, url, headers, post_data=post_data ) - return (await content.read()), status_code, response_headers + # aiohttp only reads the body here, after `request()` has returned, so + # a dropped connection or timeout mid-body surfaces from `read()` + try: + body = await content.read() + except Exception as e: + self._handle_request_error(e) + + return body, status_code, response_headers def _handle_request_error(self, e: Exception) -> NoReturn: msg = ( diff --git a/tests/test_http_client.py b/tests/test_http_client.py index 153704ed8..d089acc61 100644 --- a/tests/test_http_client.py +++ b/tests/test_http_client.py @@ -2015,6 +2015,20 @@ async def test_exception( with pytest.raises(stripe.APIConnectionError): await make_async_request("get", self.valid_url, {}, None) + @pytest.mark.parametrize("anyio_backend", ["asyncio"]) + @pytest.mark.anyio + async def test_body_read_exception( + self, mocked_request_lib, mock_response, make_async_request + ): + response = mock_response(mocked_request_lib, '{"foo": "baz"}', 200) + response.content.read = AsyncMock(side_effect=TimeoutError()) + + with pytest.raises(stripe.APIConnectionError) as error: + await make_async_request("get", self.valid_url, {}, None) + + assert error.value.should_retry + assert isinstance(error.value.__cause__, TimeoutError) + def test_timeout(self): pass @@ -2197,6 +2211,29 @@ async def test_retry_codes_until_exceeded( assert code == 409 check_call_numbers(self.max_retries + 1) + @pytest.mark.parametrize("anyio_backend", ["asyncio"]) + @pytest.mark.anyio + async def test_retry_body_read_error_until_response( + self, + mock_retry, + mock_response, + mocked_request_lib, + check_call_numbers, + make_async_request_with_args, + ): + truncated = mock_response(mocked_request_lib, code=200) + truncated.content.read = AsyncMock(side_effect=TimeoutError()) + mock_retry( + responses=[ + truncated, + mock_response(mocked_request_lib, '{"foo": "baz"}', 202), + ] + ) + body, code, _ = await make_async_request_with_args() + assert code == 202 + assert body == '{"foo": "baz"}' + check_call_numbers(2) + def connection_error(self, client, given_exception): with pytest.raises(stripe.APIConnectionError) as error: client._handle_request_error(given_exception)