estuary-cdk: improve HTTP connection timeout retry logic#3067
Merged
Conversation
My earlier attempt at retrying connection timeout errors wasn't thorough enough. Connectors still encountered connection timeout errors when establishing the connection in `request_stream` and `request_lines`, and also when processing the response in `request`. After digging into the specific errors further, I learned that: - `aiohttp.client_exceptions.ConnectionTimeoutError` is raised when _establishing_ the connection. It is not raised after the connection is established and we've begun receiving chunks from the server. - `asyncio.TimeoutError` is raised when the aiohttp client timeout is reached after we've started receiving chunks from the server but before we've received the final chunk. With this knowledge, my previous assertion in e7c8c7d about it not being safe to retry timeout errors in `request_stream` and `request_lines` isn't precisely right. It's safe to retry timeouts when establishing the connection, but not once we return the headers and body generator. Since `aiohttp.client_excepitons.ConnectionTimeoutError` is a subclass of `asyncio.TimeoutError` and `request_stream`/`request_lines` establish the HTTP connection but don't process the body (that's the responsibility of individual connectors), it's safe to use a general `_retry_on_timeout` wrapper for all of HTTPSession's public methods to retry any `asyncio.TimeoutError`s. This means that all of the public methods retry connection establishment errors, but only `request` retries timeout errors when processing the response body. Co-Authored-By: Claude <noreply@anthropic.com>
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.
Description:
My earlier attempt at retrying connection timeout errors in #3065 wasn't thorough enough. Connectors still encountered
ConnectionTimeoutErrors when establishing the connection inrequest_streamandrequest_lines, andasyncio.TimeoutErrors when reading the response body inrequest. After digging into the specific errors further, I learned that:aiohttp.client_exceptions.ConnectionTimeoutErroris raised when establishing the connection. It is not raised after the connection is established and we've begun receiving chunks from the server.asyncio.TimeoutErroris raised when theaiohttpclient timeout is reached after we've started reading chunks of the body but before we've received the final chunk.With this knowledge, my previous assertion in e7c8c7d about it not being safe to retry timeout errors in
request_streamandrequest_linesisn't precisely right. When we want to stream a response body, it's safe to retry timeouts when establishing the connection but not once we return the headers and body generator. Sinceaiohttp.client_excepitons.ConnectionTimeoutErroris a subclass ofasyncio.TimeoutError, andrequest_stream/request_linesestablish the HTTP connection but don't iterate through the body, it's safe to use a general_retry_on_timeoutwrapper for all of HTTPSession's public methods to retry anyasyncio.TimeoutErrors. This means that all of the public methods retry connection establishment errors, but onlyrequestretries timeout errors when processing the response body.Workflow steps:
(How does one use this feature, and how has it changed)
Documentation links affected:
(list any documentation links that you created, or existing ones that you've identified as needing updates, along with a brief description)
Notes for reviewers:
Tested behavior with a simple script. Confirmed:
aiohttp.client_exceptions.ConnectionTimeoutErrors andasyncio.TimeoutErrors before consuming the body.requestmethod retriesasyncio.TimeoutErrors while reading the response body.I also cleaned up a couple unused imports & variables in
http.pywhile I was in there.This change is