Skip to content

Raise APIConnectionError and retry when AIOHTTPClient fails reading the response body - #1994

Open
devteamaegis wants to merge 1 commit into
stripe:masterfrom
devteamaegis:fix/aiohttp-body-read-errors
Open

devteamaegis wants to merge 1 commit into
stripe:masterfrom
devteamaegis:fix/aiohttp-body-read-errors

Conversation

@devteamaegis

Copy link
Copy Markdown

Why?

Fixes #1993.

aiohttp's session.request() returns as soon as the response headers arrive. AIOHTTPClient.request_async then reads the body with await content.read(), but that read sits outside the try that turns transport errors into APIConnectionError. If the connection drops or the timeout fires during the body read, the caller gets a raw aiohttp.ClientPayloadError or TimeoutError, and max_network_retries never applies. RequestsClient and HTTPXClient both read the body inside their error handling.

What?

  • Wrap the body read in AIOHTTPClient.request_async with the client's existing _handle_request_error. Read failures now raise APIConnectionError(should_retry=True), the same as other AIOHTTPClient request errors, so they are retried.
  • Tests:
    • test_body_read_exception: a failing read() raises APIConnectionError chained to the original error. It also runs in the retry subclass.
    • test_retry_body_read_error_until_response: a failed body read is retried and the next response is returned.
    • All 3 new test cases fail on master and pass with this change.
  • Added a changefile.

Testing

  • pytest tests/test_http_client.py --nomock -k AIOHTTP: 27 passed.
  • Full pytest --nomock: 681 passed, 3793 skipped. 3 invoice tests failed because they call stripe-mock without being marked, and they fail the same way on master. I did not run stripe-mock.
  • ruff format --check, flake8 stripe tests, and pyright are clean.
  • I also checked the change against a throwaway local socket server that sends Content-Length: 100, then 5 bytes, then closes (and a variant that stalls past ClientTimeout(total=0.5)). Before: ClientPayloadError / TimeoutError after 1 attempt. After: APIConnectionError after 3 attempts with max_network_retries=2.

See Also

Configuration

  • skip-changefile: This PR is not a user-facing change, so there's no changefile.

This was investigated and written with an AI coding agent (Claude) working on my behalf.

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 stripe#1993
@devteamaegis
devteamaegis requested a review from a team as a code owner November 14, 2026 19:16
@devteamaegis
devteamaegis requested review from mark-kogan-stripe and removed request for a team November 14, 2026 19:16

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AIOHTTPClient: errors while reading the response body escape as raw aiohttp/asyncio exceptions and are never retried

1 participant