8000 SSL transports now clear their reference to the waiter · python/asyncio@aed248b · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

Commit aed248b

Browse files
committed
SSL transports now clear their reference to the waiter
* Rephrase also the comment explaining why the waiter is not awaken immediatly. * SSLProtocol.eof_received() doesn't instanciate ConnectionResetError exception directly, it will be done by Future.set_exception(). The exception is not used if the waiter was cancelled or if there is no waiter.
1 parent 63d8fc1 commit aed248b

File tree

4 files changed

+32
-21
lines changed

4 files changed

+32
-21
lines changed

asyncio/proactor_events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __init__(self, loop, sock, protocol, waiter=None,
3838
self._server._attach()
3939
self._loop.call_soon(self._protocol.connection_made, self)
4040
if waiter is not None:
41-
# wait until protocol.connection_made() has been called
41+
# only wake up the waiter when connection_made() has been called
4242
self._loop.call_soon(waiter._set_result_unless_cancelled, None)
4343

4444
def __repr__(self):

asyncio/selector_events.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ def __init__(self, loop, sock, protocol, waiter=None,
581581
self._loop.add_reader(self._sock_fd, self._read_ready)
582582
self._loop.call_soon(self._protocol.connection_made, self)
583583
if waiter is not None:
584-
# wait until protocol.connection_made() has been called
584+
# only wake up the waiter when connection_made() has been called
585585
self._loop.call_soon(waiter._set_result_unless_cancelled, None)
586586

587587
def pause_reading(self):
@@ -732,6 +732,16 @@ def __init__(self, loop, rawsock, protocol, sslcontext, waiter=None,
732732
start_time = None
733733
self._on_handshake(start_time)
734734

735+
def _wakeup_waiter(self, exc=None):
736+
if self._waiter is None:
737+
return
738+
if not self._waiter.cancelled():
739+
if exc is not None:
740+
self._waiter.set_exception(exc)
741+
else:
742+
self._waiter.set_result(None)
743+
self._waiter = None
744+
735745
def _on_handshake(self, start_time):
736746
try:
737747
self._sock.do_handshake()
@@ -750,8 +760,7 @@ def _on_handshake(self, start_time):
750760
self._loop.remove_reader(self._sock_fd)
751761
self._loop.remove_writer(self._sock_fd)
752762
self._sock.close()
753-
if self._waiter is not None and not self._waiter.cancelled():
754-
self._waiter.set_exception(exc)
763+
self._wakeup_waiter(exc)
755764
if isinstance(exc, Exception):
756765
return
757766
else:
@@ -774,9 +783,7 @@ def _on_handshake(self, start_time):
774783
"on matching the hostname",
775784
self, exc_info=True)
776785
self._sock.close()
777-
if (self._waiter is not None
778-
and not self._waiter.cancelled()):
779-
self._waiter.set_exception(exc)
786+
self._wakeup_waiter(exc)
780787
return
781788

782789
# Add extra info that becomes available after handshake.
@@ -789,10 +796,8 @@ def _on_handshake(self, start_time):
789796
self._write_wants_read = False
790797
self._loop.add_reader(self._sock_fd, self._read_ready)
791798
self._loop.call_soon(self._protocol.connection_made, self)
792-
if self._waiter is not None:
793-
# wait until protocol.connection_made() has been called
794-
self._loop.call_soon(self._waiter._set_result_unless_cancelled,
795-
None)
799+
# only wake up the waiter when connection_made() has been called
800+
self._loop.call_soon(self._wakeup_waiter)
796801

797802
if self._loop.get_debug():
798803
dt = self._loop.time() - start_time
@@ -924,7 +929,7 @@ def __init__(self, loop, sock, protocol, address=None,
924929
self._loop.add_reader(self._sock_fd, self._read_ready)
925930
self._loop.call_soon(self._protocol.connection_made, self)
926931
if waiter is not None:
927-
# wait until protocol.connection_made() has been called
932+
# only wake up the waiter when connection_made() has been called
928933
self._loop.call_soon(waiter._set_result_unless_cancelled, None)
929934

930935
def get_write_buffer_size(self):

asyncio/sslproto.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,16 @@ def __init__(self, loop, app_protocol, sslcontext, waiter,
418418
self._in_shutdown = False
419419
self._transport = None
420420

421+
def _wakeup_waiter(self, exc=None):
422+
if self._waiter is None:
423+
return
424+
if not self._waiter.cancelled():
425+
if exc is not None:
426+
self._waiter.set_exception(exc)
427+
else:
428+
self._waiter.set_result(None)
429+
self._waiter = None
430+
421431
def connection_made(self, transport):
422432
"""Called when the low-level connection is made.
423433
@@ -490,8 +500,7 @@ def eof_received(self):
490500
if self._loop.get_debug():
491501
logger.debug("%r received EOF", self)
492502

493-
if self._waiter is not None and not self._waiter.done():
494-
self._waiter.set_exception(ConnectionResetError())
503+
self._wakeup_waiter(ConnectionResetError)
495504

496505
if not self._in_handshake:
497506
keep_open = self._app_protocol.eof_received()
@@ -556,8 +565,7 @@ def _on_handshake_complete(self, handshake_exc):
556565
self, exc_info=True)
557566
self._transport.close()
< F438 div aria-hidden="true" class="position-absolute top-0 d-flex user-select-none DiffLineTableCellParts-module__comment-indicator--eI0hb">
558567
if isinstance(exc, Exception):
559-
if self._waiter is not None and not self._waiter.cancelled():
560-
self._waiter.set_exception(exc)
568+
self._wakeup_waiter(exc)
561569
return
562570
else:
563571
raise
@@ -572,9 +580,7 @@ def _on_handshake_complete(self, handshake_exc):
572580
compression=sslobj.compression(),
573581
)
574582
self._app_protocol.connection_made(self._app_transport)
575-
if self._waiter is not None:
576-
# wait until protocol.connection_made() has been called
577-
self._waiter._set_result_unless_cancelled(None)
583+
self._wakeup_waiter()
578584
self._session_established = True
579585
# In case transport.write() was already called. Don't call
580586
# immediatly _process_write_backlog(), but schedule it:

asyncio/unix_events.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def __init__(self, loop, pipe, protocol, waiter=None, extra=None):
301301
self._loop.add_reader(self._fileno, self._read_ready)
302302
self._loop.call_soon(self._protocol.connection_made, self)
303303
if waiter is not None:
304-
# wait until protocol.connection_made() has been called
304+
# only wake up the waiter when connection_made() has been called
305305
self._loop.call_soon(waiter._set_result_unless_cancelled, None)
306306

307307
def __repr__(self):
@@ -409,7 +409,7 @@ def __init__(self, loop, pipe, protocol, waiter=None, extra=None):
409409

410410
self._loop.call_soon(self._protocol.connection_made, self)
411411
if waiter is not None:
412-
# wait until protocol.connection_made() has been called
412+
# only wake up the waiter when connection_made() has been called
413413
self._loop.call_soon(waiter._set_result_unless_cancelled, None)
414414

415415
def __repr__(self):

0 commit comments

Comments
 (0)
0