Skip to content
Open
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
58 changes: 51 additions & 7 deletions tests/test_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,52 @@ def run(coro):
with self._silence_eof_received_warning():
run(client_sock)

def test_ssl_tcp_eof_while_reading_paused(self):
loop = self.loop
sslctx = self._create_server_ssl_context(self.ONLYCERT, self.ONLYKEY)
client_sslctx = self._create_client_ssl_context()
data = b'hello' * 4096

def server(sock):
sock.starttls(sslctx, server_side=True)
self.assertEqual(sock.recv_all(2), b'go')
sock.sendall(data)
sock.close()

class Client(MyBaseProto):
def connection_made(self, transport):
super().connection_made(transport)
transport.pause_reading()
transport.write(b'go')
self.resume = loop.call_later(0.1, transport.resume_reading)

class BufferedClient(Client, asyncio.BufferedProtocol):
def get_buffer(self, sizehint):
self.buffer = bytearray(4096)
return self.buffer

def buffer_updated(self, nbytes):
self.data_received(self.buffer[:nbytes])

async def client(addr, protocol_factory):
transport, protocol = await loop.create_connection(
lambda: protocol_factory(loop), *addr, ssl=client_sslctx)
try:
await asyncio.wait_for(protocol.done, 5)
self.assertEqual(protocol.nbytes, len(data))
finally:
protocol.resume.cancel()
transport.abort()

protocols = [Client]
if self.implementation == 'uvloop':
protocols.append(BufferedClient)

for protocol in protocols:
with self.subTest(protocol=protocol.__name__):
with self.tcp_server(server, max_clients=1) as srv:
loop.run_until_complete(client(srv.addr, protocol))

def test_create_connection_ssl_slow_handshake(self):
if self.implementation == 'asyncio':
raise unittest.SkipTest()
Expand Down Expand Up @@ -3045,8 +3091,9 @@ def server(sock):
sock.send(b'extra bytes')
# sending EOF here
sock.shutdown(socket.SHUT_WR)
# make sure we have enough time to reproduce the issue
self.assertEqual(sock.recv(1024), b'')
# Drain the client's close_notify and wait for TCP EOF.
while sock.recv(1024):
pass
sock.close()

class Protocol(asyncio.Protocol):
Expand All @@ -3062,6 +3109,7 @@ def data_received(self, data):
self.transport.write(b'world')
# pause reading would make incoming data stay in the sslobj
self.transport.pause_reading()
self.transport.close()
else:
nonlocal extra
extra = data
Expand All @@ -3072,14 +3120,10 @@ def connection_lost(self, exc):
else:
self.fut.set_exception(exc)

def eof_received(self):
self.transport.resume_reading()

async def client(addr):
ctx = self._create_client_ssl_context()
tr, pr = await loop.create_connection(Protocol, *addr, ssl=ctx)
_, pr = await loop.create_connection(Protocol, *addr, ssl=ctx)
await pr.fut
tr.close()
# extra data received after transport.close() should be ignored
self.assertIsNone(extra)

Expand Down
3 changes: 2 additions & 1 deletion uvloop/sslproto.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ cdef class SSLProtocol:

bint _ssl_writing_paused
bint _app_reading_paused
bint _tcp_eof_received

size_t _incoming_high_water
size_t _incoming_low_water
Expand Down Expand Up @@ -121,7 +122,7 @@ cdef class SSLProtocol:
# Incoming flow

cdef inline _do_read(self)
cdef inline _do_read__buffered(self)
cdef inline bint _do_read__buffered(self) except -1
cdef inline _do_read__copied(self)
cdef inline _call_eof_received(self, object context=*)

Expand Down
38 changes: 29 additions & 9 deletions uvloop/sslproto.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -411,12 +411,14 @@ cdef class SSLProtocol:
if self._state == DO_HANDSHAKE:
self._on_handshake_complete(ConnectionResetError)

elif self._state == WRAPPED or self._state == FLUSHING:
# We treat a low-level EOF as a critical situation similar to a
# broken connection - just send whatever is in the buffer and
# close. No application level eof_received() is called -
# because we don't want the user to think that this is a
# graceful shutdown triggered by SSL "close_notify".
elif self._state == WRAPPED:
# Drain buffered data before closing, respecting read flow
# control. A TCP EOF is not an SSL close_notify.
self._tcp_eof_received = True
self._do_read()
return self._state == WRAPPED

elif self._state == FLUSHING:
self._set_state(SHUTDOWN)
self._on_shutdown_complete(None)

Expand Down Expand Up @@ -578,6 +580,10 @@ cdef class SSLProtocol:
self._abort(None)
else:
self._set_state(FLUSHING)
if self._tcp_eof_received:
self._set_state(SHUTDOWN)
self._on_shutdown_complete(None)
return
self._shutdown_timeout_handle = \
self._loop.call_later(self._ssl_shutdown_timeout,
lambda: self._check_shutdown_timeout())
Expand Down Expand Up @@ -714,23 +720,32 @@ cdef class SSLProtocol:
# Incoming flow

cdef _do_read(self):
cdef bint read_scheduled = False

if self._state != WRAPPED:
return
try:
if not self._app_reading_paused:
if self._app_protocol_is_buffer:
self._do_read__buffered()
read_scheduled = self._do_read__buffered()
else:
self._do_read__copied()
if self._write_backlog:
self._do_write()
self._process_outgoing()
self._control_app_writing()
if (self._tcp_eof_received and self._state == WRAPPED
and not read_scheduled
and (not self._app_reading_paused
or not (self._incoming.pending
or self._sslobj_pending()))):
self._set_state(SHUTDOWN)
self._on_shutdown_complete(None)
self._control_ssl_reading()
except Exception as ex:
self._fatal_error(ex, 'Fatal error on SSL protocol')

cdef _do_read__buffered(self):
cdef bint _do_read__buffered(self) except -1:
cdef:
Py_ssize_t total_pending = (<Py_ssize_t>self._incoming.pending
+ <Py_ssize_t>self._sslobj_pending())
Expand All @@ -741,13 +756,14 @@ cdef class SSLProtocol:
Py_ssize_t app_buffer_size = len(app_buffer)

if app_buffer_size == 0:
return
return False

cdef:
Py_ssize_t last_bytes_read = -1
Py_ssize_t total_bytes_read = 0
Py_buffer pybuf
bint pybuf_initialized = False
bint read_scheduled = False

try:
# SSLObject.read may not return all available data in one go.
Expand Down Expand Up @@ -801,6 +817,7 @@ cdef class SSLProtocol:
<method_t> self._do_read,
None, # current context is good
self))
read_scheduled = True
break

total_pending = (<Py_ssize_t>self._incoming.pending +
Expand All @@ -820,6 +837,7 @@ cdef class SSLProtocol:
# close_notify
self._call_eof_received()
self._start_shutdown()
return read_scheduled

cdef _do_read__copied(self):
cdef:
Expand Down Expand Up @@ -946,6 +964,8 @@ cdef class SSLProtocol:
# Flow control for reads from SSL socket

cdef _control_ssl_reading(self):
if self._tcp_eof_received:
return
cdef size_t size = self._get_read_buffer_size()
if size >= self._incoming_high_water and not self._ssl_reading_paused:
self._ssl_reading_paused = True
Expand Down
Loading