New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Detailed TimeoutError message not captured when handling asyncio.TimeoutError in aiohttp #8133
Comments
Perhaps #8089 will help a bit. |
The request is for an error message to be added to the exception. However, in many places we use asyncio.timeout(), which doesn't support any parameters. It's not commonplace to include messages in TimeoutError, and given that we might want to replace the other instances with asyncio.timeout() later, I'd suggest we don't commit to this feature. |
Appreciate your response to this issue. However, I would like to emphasize the importance of providing error types and details in timeout exceptions, and offer a more accurate comparison based on how requests and httpx handle timeouts. Current Scenario: aiohttp Example: Currently, when a timeout occurs in aiohttp, it raises a generic async with aiohttp.ClientSession() as client:
res = await client.get('https://postman-echo.com/delay/3', timeout=1)
Traceback (most recent call last):
...
TimeoutError: (No detailed message provided) requests Example (Connection Timeout): The requests library does indeed provide a specific exception type res = requests.get('https://postman-echo.com/delay/3', timeout=0.1)
Traceback (most recent call last):
...
requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='postman-echo.com', port=443): Max retries exceeded with url: /delay/3 (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x107fe8d90>, 'Connection to postman-echo.com timed out. (connect timeout=0.1)')) requests Example (Read Timeout): Similarly, for read timeouts, requests provides a distinct exception type res = requests.get('https://postman-echo.com/delay/3', timeout=1)
Traceback (most recent call last):
...
requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='postman-echo.com', port=443): Max retries exceeded with url: /delay/3 (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x107fe8d90>, 'Connection to postman-echo.com timed out. (connect timeout=0.1)')) httpx Example (Connection & Read Timeouts): While httpx offers separate exception types async with httpx.AsyncClient(verify=False) as client:
res = await client.get('https://postman-echo.com/delay/3', timeout=.1)
Traceback (most recent call last):
...
httpx.ConnectTimeout
async with httpx.AsyncClient(verify=False) as client:
res = await client.get('https://postman-echo.com/delay/3', timeout=1)
Traceback (most recent call last):
...
httpx.ReadTimeout Considering aiohttp's future plans to utilize Look forward to further discussing. |
Then maybe also make a proposal to cpython? If they add a message argument or similar to
|
My understanding of this response is that you propose enhancing # Hypothetical usage after an update to asyncio.timeout()
class CustomTimeoutError(Exception):
def __init__(self, message, **kwargs):
self.url = url
super().__init__(message)
try:
with asyncio.timeout(5.0, exception_cls=CustomTimeoutError, url="https://example.com"):
# Perform asynchronous operations
...
except CustomTimeoutError as e:
print(f"Timed out while accessing {e.url}: {e}") I wholeheartedly agree that implementing such a feature would be beneficial. It would significantly improve developers' ability to understand and troubleshoot timeout issues by providing contextually rich error messages. Meanwhile, I acknowledge your concern about having to introduce awkward re-raising logic within aiohttp if this feature is not available. However, even without the direct support from |
That's probably more flexible that my initial thought, which was just to have a |
I think, we should consider implementing the suggested context augmentation where possible, if the contributor is willing to present a PoC PR. I'm sure it's useful. That https://github.com/aio-libs/aiohttp/blob/cdd5c6802e669c93f0077448430349b6dadd8580/aiohttp/helpers.py#L720C45-L720C49 place is an example where we force a |
Describe the bug
When handling a timeout with aiohttp's
asyncio.TimeoutError
exception in an aiohttp request, the error message does not provide detailed information about the timeout. Despite attempting to print the exception within the except block, onlyThe error msg is
is displayed without any specifics of the timeout error.To Reproduce
Expected behavior
I expect that upon encountering an asyncio.TimeoutError, the error message would contain detailed information about the timeout, such as the reason for the timeout or potentially the elapsed time before the timeout occurred.
For example:
Logs/tracebacks
The output from running the above code will be:
The following log traceback was generated when running the script with
asyncio.run(api_req(), debug=True)
:This traceback shows that an asyncio.TimeoutError occurred during the execution of
client.get()
on line 9 in the api_req function. Despite catching the exception, no detailed message about the timeout was printed before raising it again. The traceback confirms that the operation timed out as expected but does not provide additional context for the timeout event.Python Version
aiohttp Version
multidict Version
yarl Version
OS
All
Related component
Client
Additional context
The environment leading to this issue includes a vanilla setup of Python and aiohttp used for making asynchronous HTTP requests. There are no proxy servers or other middleware involved that could affect the outcome. The expected behavior is to have access to more descriptive information about the TimeoutError in order to better understand the cause and troubleshoot timeouts in the application.
Code of Conduct
The text was updated successfully, but these errors were encountered: