aiohttp icon indicating copy to clipboard operation
aiohttp copied to clipboard

Client raises asyncio.TimeoutError

Open poofeg opened this issue 2 years ago • 4 comments

Describe the bug

Client raises asyncio.TimeoutError, although according to the documentation it seems that aiohttp.ServerTimeoutError should be raised.

To Reproduce

  1. Run nc -l 8000
  2. Run:
import aiohttp
import asyncio


async def main():
    timeout = aiohttp.ClientTimeout(total=10)
    async with aiohttp.ClientSession(timeout=timeout) as session:
        try:
            async with session.get('http://127.0.0.1:8000'):
                pass
        except aiohttp.ServerTimeoutError:
            print('aiohttp.ServerTimeoutError')
        except aiohttp.ClientError:
            print('aiohttp.ClientError')
        except asyncio.TimeoutError:
            print('asyncio.TimeoutError')


if __name__ == '__main__':
    asyncio.run(main())
  1. Wait 10 seconds
  2. Get result: "asyncio.TimeoutError"

Expected behavior

Result: "aiohttp.ServerTimeoutError"

Logs/tracebacks

asyncio.TimeoutError

Python Version

3.11.0

aiohttp Version

3.8.3

multidict Version

6.0.2

yarl Version

1.8.1

OS

macOS 12.6.1

Related component

Client

Additional context

No response

Code of Conduct

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

poofeg avatar Dec 07 '22 11:12 poofeg

Hey @graingert, can this be related to your patch #6877?

webknjaz avatar Dec 07 '22 17:12 webknjaz

Looks like the code does raise ServerTimeoutError on (1) connection timeout (https://github.com/aio-libs/aiohttp/blob/0d39c84ad47115b7b0af411fa3a6080c56f3cdff/aiohttp/client_proto.py#L197) and on read timeout (https://github.com/aio-libs/aiohttp/blob/0d39c84ad47115b7b0af411fa3a6080c56f3cdff/aiohttp/client.py#L504)

webknjaz avatar Dec 07 '22 17:12 webknjaz

This is not a new behavior. At least since version 2.3.10, this case raises asyncio.TimeoutError exception.

poofeg avatar Dec 07 '22 22:12 poofeg

Thanks for posting this question @poofeg. I had thought we had handled timeouts in our retry logic, but we didn't identify this exception as one that needs to be handled as well.

I would suggest:

  1. Update the documentation to clarify that asyncio.TimeoutError may be raised in the event of a timed-out request.
  2. Consider (but probably avoid) raising ServerTimeoutError instead of asyncio.TimeoutError in this case.

Do people agree the documentation should be updated to call out this behavior?

nick-merrill avatar Nov 03 '23 10:11 nick-merrill