Skip to content
Draft
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,4 @@
---
title: Fix aiohttp buffered response body read failures to raise retryable APIConnectionError exceptions
semver_level: patch
---
7 changes: 6 additions & 1 deletion stripe/_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1514,7 +1514,12 @@ async def request_async(
method, url, headers, post_data=post_data
)

return (await content.read()), status_code, response_headers
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
96 changes: 96 additions & 0 deletions tests/test_http_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import importlib
import io
import base64
Expand All @@ -6,6 +7,7 @@
from typing import Any, Callable, Dict, List, Optional
from unittest.mock import MagicMock, Mock, call, patch

import aiohttp
import pytest
from typing_extensions import Type

Expand Down Expand Up @@ -2118,6 +2120,100 @@ class TestAIOHTTPClientRetryBehavior(TestAIOHTTPClient):
_mocked_request_lib = None
max_retries = 3

@pytest.mark.parametrize("anyio_backend", ["asyncio"])
@pytest.mark.parametrize(
"error_type", [aiohttp.ClientPayloadError, asyncio.TimeoutError]
)
@pytest.mark.parametrize("max_retries", [0, 2])
@pytest.mark.anyio
async def test_body_read_error_until_exceeded(
self,
mocked_request_lib,
mock_response,
make_async_request,
error_type,
max_retries,
):
response = mock_response(mocked_request_lib)
read_errors = [
error_type("Response body read failed")
for _ in range(max_retries + 1)
]
response.content.read = AsyncMock(side_effect=read_errors)

with pytest.raises(stripe.APIConnectionError) as exc_info:
await make_async_request(
"GET", self.valid_url, {}, None, max_retries=max_retries
)

assert exc_info.value.should_retry is True
assert exc_info.value.__cause__ is read_errors[-1]
assert mocked_request_lib.ClientSession().request.await_count == (
max_retries + 1
)
assert response.content.read.await_count == max_retries + 1

@pytest.mark.parametrize("anyio_backend", ["asyncio"])
@pytest.mark.parametrize(
"error_type", [aiohttp.ClientPayloadError, asyncio.TimeoutError]
)
@pytest.mark.anyio
async def test_body_read_error_until_response(
self,
mocked_request_lib,
mock_response,
make_async_request,
error_type,
):
failed_response = mock_response(mocked_request_lib)
failed_response.content.read = AsyncMock(
side_effect=error_type("Response body read failed")
)
successful_response = mock_response(
mocked_request_lib, b"complete response", code=202
)
successful_response.headers = {"Content-Type": "application/json"}
successful_response.content.read = AsyncMock(
wraps=successful_response.content.read
)
request = mocked_request_lib.ClientSession().request
request.side_effect = lambda *args, **kwargs: (
failed_response
if request.await_count == 1
else successful_response
)

result = await make_async_request(
"GET", self.valid_url, {}, None, max_retries=2
)

assert result == (
b"complete response",
202,
{"Content-Type": "application/json"},
)
assert request.await_count == 2
failed_response.content.read.assert_awaited_once_with()
successful_response.content.read.assert_awaited_once_with()

@pytest.mark.parametrize("anyio_backend", ["asyncio"])
@pytest.mark.anyio
async def test_body_read_cancellation(
self, mocked_request_lib, mock_response, make_async_request
):
response = mock_response(mocked_request_lib)
cancellation = asyncio.CancelledError()
response.content.read = AsyncMock(side_effect=cancellation)

with pytest.raises(asyncio.CancelledError) as exc_info:
await make_async_request(
"GET", self.valid_url, {}, None, max_retries=2
)

assert exc_info.value is cancellation
assert mocked_request_lib.ClientSession().request.await_count == 1
response.content.read.assert_awaited_once_with()

@pytest.fixture
def mock_retry(self, mocked_request_lib):
def mock_retry(
Expand Down
Loading