Skip to content

Fixes #220 sslproto memory issue #222

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

Merged
merged 1 commit into from
Feb 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions tests/test_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2651,6 +2651,73 @@ async def test():
else:
self.fail('Unexpected ResourceWarning: {}'.format(cm.warning))

def test_handshake_timeout_handler_leak(self):
if self.implementation == 'asyncio':
# Okay this turns out to be an issue for asyncio.sslproto too
raise unittest.SkipTest()

s = socket.socket(socket.AF_INET)
s.bind(('127.0.0.1', 0))
s.listen(1)
addr = s.getsockname()

async def test(ctx):
try:
await asyncio.wait_for(
self.loop.create_connection(asyncio.Protocol, *addr,
ssl=ctx),
0.1, loop=self.loop)
except (ConnectionRefusedError, asyncio.TimeoutError):
pass
else:
self.fail('TimeoutError is not raised')

with s:
ctx = ssl.create_default_context()
self.loop.run_until_complete(test(ctx))
ctx = weakref.ref(ctx)

# SSLProtocol should be DECREF to 0
self.assertIsNone(ctx())

def test_shutdown_timeout_handler_leak(self):
loop = self.loop

def server(sock):
sslctx = self._create_server_ssl_context(self.ONLYCERT,
self.ONLYKEY)
sock = sslctx.wrap_socket(sock, server_side=True)
sock.recv(32)
sock.close()

class Protocol(asyncio.Protocol):
def __init__(self):
self.fut = asyncio.Future(loop=loop)

def connection_lost(self, exc):
self.fut.set_result(None)

async def client(addr, ctx):
tr, pr = await loop.create_connection(Protocol, *addr, ssl=ctx)
tr.close()
await pr.fut

with self.tcp_server(server) as srv:
ctx = self._create_client_ssl_context()
loop.run_until_complete(client(srv.addr, ctx))
ctx = weakref.ref(ctx)

if self.implementation == 'asyncio':
# asyncio has no shutdown timeout, but it ends up with a circular
# reference loop - not ideal (introduces gc glitches), but at least
# not leaking
gc.collect()
gc.collect()
gc.collect()

# SSLProtocol should be DECREF to 0
self.assertIsNone(ctx())


class Test_UV_TCPSSL(_TestSSL, tb.UVTestCase):
pass
Expand Down
14 changes: 12 additions & 2 deletions uvloop/sslproto.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,9 @@ cdef class SSLProtocol:
self._app_transport = None
self._wakeup_waiter(exc)

if getattr(self, '_shutdown_timeout_handle', None):
if self._shutdown_timeout_handle:
self._shutdown_timeout_handle.cancel()
if getattr(self, '_handshake_timeout_handle', None):
if self._handshake_timeout_handle:
self._handshake_timeout_handle.cancel()

def get_buffer(self, n):
Expand Down Expand Up @@ -453,8 +453,13 @@ cdef class SSLProtocol:
self._set_state(DO_HANDSHAKE)

# start handshake timeout count down
# XXX: This is a workaround to call cdef method and clear reference to
# self when the handle is cancelled (because uvloop currently clears
# only args, it'll be okay to remove the lambda x: x() after the fix to
# clear also the callback)
self._handshake_timeout_handle = \
self._loop.call_later(self._ssl_handshake_timeout,
lambda x: x(),
lambda: self._check_handshake_timeout())

try:
Expand Down Expand Up @@ -533,8 +538,13 @@ cdef class SSLProtocol:
self._abort(None)
else:
self._set_state(FLUSHING)
# XXX: This is a workaround to call cdef method and clear reference
# to self when the handle is cancelled (because uvloop currently
# clears only args, it'll be okay to remove the lambda x: x() after
# the fix to clear also the callback)
self._shutdown_timeout_handle = \
self._loop.call_later(self._ssl_shutdown_timeout,
lambda x: x(),
lambda: self._check_shutdown_timeout())
self._do_flush()

Expand Down