8

I am doing request() of aiohttp.ClientSession instance and sometimes asyncio.TimeoutError is raised. I thought that aiohttp.ServerTimeoutError must be raised in this cases, which derived from asyncio.TimeoutError, as this doc says: http://docs.aiohttp.org/en/stable/client_reference.html#aiohttp.ServerTimeoutError Why is it happening? Maybe because I am using old version of aiohttp? 2.3.8

UPD this may happen in very simple code like this

async def example_of_code():
    session = aiohttp.ClientSession()
    response = await session.request(
        method='POST',
        url='some_url',
        params={'some': 'params'},
        data={'some': 'data'},
        headers={'some': 'headers'},
        timeout=10
    )
    return await response.json()
sanyassh
  • 8,100
  • 13
  • 36
  • 70
  • Please provide the sample code that produced an error. We can't tell you what's causing the error without seeing the code that caused it. – Calvin Godfrey Jan 10 '19 at 16:27

1 Answers1

7

aiohttp.ServerTimeoutError and asyncio.TimeoutError are different types of timeout.

asyncio.TimeoutError is a general timeout that can happen due to many different reasons starting from unexisting domain or too much data to read.

aiohttp.ServerTimeoutError as search in aiohttp source code reveales is used in one place only - when connection with server established, but some reading from socket takes too long. You can also check aiohttp tests to see real situations, where you get ServerTimeoutError.

Operation of network request is complex and may go wrong in many different places. Don't try to understand them all (if that's not you purpose). As long as you just want to do request, catch TimeoutError (since ServerTimeoutError is a subclass) to see if you possibly should alter timeout kwarg.

Mikhail Gerasimov
  • 36,989
  • 16
  • 116
  • 159
  • It's also raised here after catching the asyncio.TimeoutError: https://github.com/aio-libs/aiohttp/blob/9e83d04782b5eec1e5dfafc8bac50192473da4db/aiohttp/client.py#L483 – Break Aug 09 '21 at 21:28