Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
9 changes: 8 additions & 1 deletion stripe/_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
37 changes: 37 additions & 0 deletions tests/test_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down