Search code examples
pythonpython-asyncioaiohttp

aiohttp freeze in ClientSession()


Why ClientSession in async context manager freeze until timeout approximately once in 3-4 runs?

When I await any coroutine from response inside async context manager everything works normal but if I try to do it outside (or return response) it freeze?

Here is minimal representation of code

from aiohttp import ClientSession
import asyncio

async def fetch():
    url = ...
    headers = {"User-Agent": ...}
    parameters = [...]

    async with ClientSession() as session:
        session.headers.update(headers)
        response = await session.post(url, data=parameters, ssl=False)
        # await response.json() # <-- This line fix problem
    return response

async def main():
    r = await fetch()
    print(await r.json())

asyncio.run(main())

Problem fixed by returning response inside async context manager but I want to know why this problem appears.


Solution

  • Here's what happens when the code runs outside of async with:

    1. Context Manager Exit: Exiting the async with ClientSession() block triggers cleanup, which tries to close the session.
    2. Unconsumed Response: If the response body has not been read or awaited inside the block, the session still holds a connection with pending data (the response from your POST request).
    3. Session Cleanup and Pending Connection: The session tries to close, but since the response is not fully consumed (i.e., the response data is not yet read), it leads to complications, potentially causing your program to hang or delay until a timeout occurs.

    When you return response outside of context, it's not awaited and consumed, so it doesn't work.

    When you return the response from inside the async with context, the response is consumed and awaited, so it works fine.