aiohttp icon indicating copy to clipboard operation
aiohttp copied to clipboard

Detailed TimeoutError message not captured when handling asyncio.TimeoutError in aiohttp

Open a76yyyy opened this issue 5 months ago • 8 comments

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, only The error msg is is displayed without any specifics of the timeout error.

To Reproduce

  1. Implement the following client code snippet:
import asyncio
import aiohttp

async def api_req():
    try:
        async with aiohttp.ClientSession() as client:
            res = await client.get('https://httpbin.org/delay/10', timeout=.1)
    except asyncio.TimeoutError as e:
        print(f'The error msg is {e}')
        raise e
    except Exception as e:
        print(f'This error ({e}) should not happen.')
        raise e
    print(res)
    return

asyncio.run(api_req())
  1. Run the script using your Python interpreter.
  2. Observe that when the API request times out, it prints "The error msg is" without any further details.

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:

The error msg is: asyncio.TimeoutError: Timed out after 0.1 seconds during connection establishment.
The error msg is: asyncio.TimeoutError: Timed out after 0.1 seconds during response processing.

Logs/tracebacks

The output from running the above code will be:

The error msg is

The following log traceback was generated when running the script with asyncio.run(api_req(), debug=True):

The error msg is 
Traceback (most recent call last):
  File "[YOUR_FILE_PATH]\test.py", line 19, in <module>
    asyncio.run(api_req(), debug=True)
  File "...\Python3.11.6\Lib\asyncio\runners.py", line 190, in run
    return runner.run(main)
  File "...\Python3.11.6\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
  File "...\Python3.11.6\Lib\asyncio\base_events.py", line 653, in run_until_complete
    return future.result()
  File "[YOUR_FILE_PATH]\test.py", line 12, in api_req
    raise e
  File "[YOUR_FILE_PATH]\test.py", line 9, in api_req
    res = await client.get('https://httpbin.org/delay/10', timeout=.1)
  File "...\site-packages\aiohttp\client.py", line 504, in _request
    with timer:
  File "...\site-packages\aiohttp\helpers.py", line 735, in __exit__
    raise asyncio.TimeoutError from None
TimeoutError

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

$ python --version
Python 3.11.6

aiohttp Version

$ python -m pip show aiohttp
Name: aiohttp
Version: 3.9.3
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author: 
Author-email: 
License: Apache 2
Requires: aiosignal, attrs, frozenlist, multidict, yarl
Required-by:

multidict Version

$ python -m pip show multidict
Name: multidict
Version: 6.0.5
Summary: multidict implementation
Home-page: https://github.com/aio-libs/multidict
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache 2
Requires: 
Required-by: aiohttp, yarl

yarl Version

$ python -m pip show yarl
Name: yarl
Version: 1.9.4
Summary: Yet another URL library
Home-page: https://github.com/aio-libs/yarl
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache-2.0
Requires: idna, multidict
Required-by: aiohttp

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

  • [X] I agree to follow the aio-libs Code of Conduct

a76yyyy avatar Feb 03 '24 15:02 a76yyyy