-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
aiohttp socket leak and incorrect connection close #1568
Comments
per the headers returned it's an apache server, I even tried reproducing with the OSX built-in apache server and it doesn't reproduce, so it seems like it's something particular to this server. |
this is in fact related to SSL as the http endpoint ends up redirecting to the https endpoint :) |
ends up being a asyncio issue! Here's the testcase: import asyncio
import ssl
if True: # madis
HOST = 'madis-data.ncep.noaa.gov'
PORT = 443
PATH = '/madisPublic1/data/archive/2001/07/01/LDAD/mesonet/netCDF/20010701_0800.gz'
else: # localhost
HOST = 'localhost'
PORT = 443
PATH = '/20010701_0800.gz'
MSG = 'GET {path} HTTP/1.1\r\nIf-Modified-Since: Sun, 01 Jul 2001 10:24:33 GMT\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nHost: madis-data.ncep.noaa.gov\r\nUser-Agent: Python/3.5 aiohttp/1.3.0a0\r\nContent-Length: 0\r\n\r\n'.format(path=PATH)
class HttpClient(asyncio.Protocol):
def connection_made(self, transport):
self.transport = transport
self.transport.write(MSG.encode('ascii'))
print('data sent:', MSG)
def data_received(self, data):
print('data received:', data)
asyncio.get_event_loop().call_later(1.0, self.transport.close)
def eof_received(self):
pass
def connection_lost(self, exc):
print('connection lost:', exc)
asyncio.get_event_loop().stop()
async def main():
loop = asyncio.get_event_loop()
ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
ssl_context.set_default_verify_paths()
ssl_context.load_cert_chain('/tmp/cert', '/tmp/key')
conn = await loop.create_connection(HttpClient, HOST, PORT, ssl=ssl_context)
if __name__ == '__main__':
_loop = asyncio.get_event_loop()
_loop.run_until_complete(main())
_loop.run_forever() will write up a bug a backref here |
Could you post response data that you receive in 'data_received' |
madis:
my server:
|
issue 1 should be fixed in master |
@thehesiod i added special code for closed ssl transport, in addition to close aiohttp aborts all ssl transport after 2 seconds. this should solve leaked sockets |
@fafhrd91 thank you sir! |
could you test if it works? |
will test asap. |
looks like the PR broke master:
|
should be fixed |
MUCH better, yay, thank you! |
Just mentioning for others that might be mislead here: they circled back later and disabled this fix by default (see 02b0951). |
Long story short
Issue 1
it appears for certain servers the ssl sockets are not released correctly.
Issue 2:
I believe
aiohttp.client._RequestContextManager.__aexit__ ()
should notclose()
the connector on exception as this should be up to the client of aiohttp. Instead it should always opportunistically release the socket. Incidentally changing this to release also fixes #1 in my scenario.Expected behaviour
Issue 1
With the sample script the number of open handles should stay relatively stable.
Issue 2
raising a custom exception from an aiohttp request contest should not cause the connector to be closed and a new one opened.
Actual behaviour
Issue 1
number of context handles grows very quickly with test script. Is resolved by changing https://github.com/KeepSafe/aiohttp/blob/master/aiohttp/connector.py#L392 to be abort(), or changing https://github.com/KeepSafe/aiohttp/blob/master/aiohttp/client.py#L555 to always release. However there appears to be an underlying issue with closing SSL sockets with certain servers.
Issue 2
tcpconnector is closed and re-opened when exception is raised from response context.
Steps to reproduce
Run sample script:
keep track of open files:
and note how the number of handles grows very quickly. It should remain stable. I was unable to reproduce this with the sample server included so it must be some kind of special behavior of the server this testcase hits.
Your environment
OSX and linux, python3.5.3 with latest aiohttp
The text was updated successfully, but these errors were encountered: