Skip to content

Commit

Permalink
fix None _shutdown_timeout_handle issue
Browse files Browse the repository at this point in the history
* Fixes #255
* Also make handles more robust
  • Loading branch information
fantix authored and 1st1 committed Oct 25, 2019
1 parent b2aa8db commit 7fcbfed
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
44 changes: 44 additions & 0 deletions tests/test_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2891,6 +2891,50 @@ async def client(addr, ctx):
# SSLProtocol should be DECREF to 0
self.assertIsNone(ctx())

def test_shutdown_timeout_handler_not_set(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.send(b'hello')
assert sock.recv(1024) == b'world'
time.sleep(0.1)
sock.send(b'extra bytes' * 1)
# sending EOF here
sock.shutdown(socket.SHUT_WR)
# make sure we have enough time to reproduce the issue
time.sleep(0.1)
sock.close()

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

def connection_made(self, transport):
self.transport = transport

def data_received(self, data):
self.transport.write(b'world')
# pause reading would make incoming data stay in the sslobj
self.transport.pause_reading()
# resume for AIO to pass
loop.call_later(0.2, self.transport.resume_reading)

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

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

with self.tcp_server(server) as srv:
loop.run_until_complete(client(srv.addr))


class Test_UV_TCPSSL(_TestSSL, tb.UVTestCase):
pass
Expand Down
10 changes: 8 additions & 2 deletions uvloop/sslproto.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,10 @@ cdef class SSLProtocol:

if self._shutdown_timeout_handle:
self._shutdown_timeout_handle.cancel()
self._shutdown_timeout_handle = None
if self._handshake_timeout_handle:
self._handshake_timeout_handle.cancel()
self._handshake_timeout_handle = None

def get_buffer(self, n):
cdef size_t want = n
Expand Down Expand Up @@ -495,7 +497,9 @@ cdef class SSLProtocol:
self._on_handshake_complete(None)

cdef _on_handshake_complete(self, handshake_exc):
self._handshake_timeout_handle.cancel()
if self._handshake_timeout_handle is not None:
self._handshake_timeout_handle.cancel()
self._shutdown_timeout_handle = None

sslobj = self._sslobj
try:
Expand Down Expand Up @@ -588,7 +592,9 @@ cdef class SSLProtocol:
self._on_shutdown_complete(None)

cdef _on_shutdown_complete(self, shutdown_exc):
self._shutdown_timeout_handle.cancel()
if self._shutdown_timeout_handle is not None:
self._shutdown_timeout_handle.cancel()
self._shutdown_timeout_handle = None

if shutdown_exc:
self._fatal_error(shutdown_exc)
Expand Down

0 comments on commit 7fcbfed

Please sign in to comment.