Repository navigation
Raise APIConnectionError and retry when AIOHTTPClient fails reading the response body - #1994
Open
devteamaegis wants to merge 1 commit into
Open
devteamaegis wants to merge 1 commit into
devteamaegis wants to merge 1 commit into
Conversation
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
requested review from
mark-kogan-stripe
and removed request for
a team
November 14, 2026 19:16
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why?
Fixes #1993.
aiohttp's
session.request()returns as soon as the response headers arrive.AIOHTTPClient.request_asyncthen reads the body withawait content.read(), but that read sits outside thetrythat turns transport errors intoAPIConnectionError. If the connection drops or the timeout fires during the body read, the caller gets a rawaiohttp.ClientPayloadErrororTimeoutError, andmax_network_retriesnever applies.RequestsClientandHTTPXClientboth read the body inside their error handling.What?
AIOHTTPClient.request_asyncwith the client's existing_handle_request_error. Read failures now raiseAPIConnectionError(should_retry=True), the same as otherAIOHTTPClientrequest errors, so they are retried.test_body_read_exception: a failingread()raisesAPIConnectionErrorchained 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.masterand pass with this change.Testing
pytest tests/test_http_client.py --nomock -k AIOHTTP: 27 passed.pytest --nomock: 681 passed, 3793 skipped. 3 invoice tests failed because they call stripe-mock without being marked, and they fail the same way onmaster. I did not run stripe-mock.ruff format --check,flake8 stripe tests, andpyrightare clean.Content-Length: 100, then 5 bytes, then closes (and a variant that stalls pastClientTimeout(total=0.5)). Before:ClientPayloadError/TimeoutErrorafter 1 attempt. After:APIConnectionErrorafter 3 attempts withmax_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.