HTTP response not released with raise_for_status=True #3364
Closed
Description
Long story short
When using ClientSession with raise_for_status=True, HTTP responses are not released. This means the related TCP connection will remain in CLOSE_WAIT state indefinitely, when the called server returns an error status code and a large body. Eventually, these waiting connections will hit the connection limit and cause timeout errors on ALL open connections.
Expected behaviour
When using Client Session with raise_for_status=True, TCP connections are released properly and they are either closed or reused (depending on keep alive configuration).
Actual behaviour
When using Client Session with raise_for_status=True, the TCP connections
Steps to reproduce
client.py
from aiohttp import ClientSession, ClientResponseError
import asyncio
async def send_request():
client = ClientSession(raise_for_status=True)
try:
async with client.get('http://hansadema.com/404.php') as response:
print('Received good status %d' % response.status)
except ClientResponseError as e:
print('Received bad status %d' % e.status)
loop = asyncio.get_event_loop()
asyncio.ensure_future(send_request())
loop.run_forever()
Dockerfile
FROM python:3.6-slim
COPY . /usr/src/app
RUN pip install aiohttp
CMD python /usr/src/app/client.py
Commands:
$ docker build -t aiohttp-test .
$ docker run --rm --name aiohttp-test aiohttp-test
# Linux command to check open connections
$ sudo nsenter -t (docker inspect -f '{{.State.Pid}}' "aiohttp-test") -n ss --numeric --tcp
Your environment
aiohttp 3.4.4
Python 3.6.6
Debian 9.5