asyncio: sync with Tulip · pythoncapi/cpython@2934262 · GitHub
Skip to content

Commit 2934262

Browse files
committed
asyncio: sync with Tulip
* Cleanup gather(): use cancelled() method instead of using private Future attribute * Fix _UnixReadPipeTransport and _UnixWritePipeTransport. Only start reading when connection_made() has been called. * Issue python#23333: Fix BaseSelectorEventLoop._accept_connection(). Close the transport on error. In debug mode, log errors using call_exception_handler()
1 parent 54a231d commit 2934262

5 files changed

Lines changed: 85 additions & 44 deletions

File tree

Lib/asyncio/selector_events.py

Lines changed: 38 additions & 6 deletions

Lib/asyncio/tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ def _done_callback(i, fut):
592592
fut.exception()
593593
return
594594

595-
if fut._state == futures._CANCELLED:
595+
if fut.cancelled():
596596
res = futures.CancelledError()
597597
if not return_exceptions:
598598
outer.set_exception(res)

Lib/asyncio/unix_events.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,10 @@ def __init__(self, loop, pipe, protocol, waiter=None, extra=None):
298298
_set_nonblocking(self._fileno)
299299
self._protocol = protocol
300300
self._closing = False
301-
self._loop.add_reader(self._fileno, self._read_ready)
302301
self._loop.call_soon(self._protocol.connection_made, self)
302+
# only start reading when connection_made() has been called
303+
self._loop.call_soon(self._loop.add_reader,
304+
self._fileno, self._read_ready)
303305
if waiter is not None:
304306
# only wake up the waiter when connection_made() has been called
305307
self._loop.call_soon(waiter._set_result_unless_cancelled, None)
@@ -401,13 +403,16 @@ def __init__(self, loop, pipe, protocol, waiter=None, extra=None):
401403
self._conn_lost = 0
402404
self._closing = False # Set when close() or write_eof() called.
403405

404-
# On AIX, the reader trick only works for sockets.
405-
# On other platforms it works for pipes and sockets.
406-
# (Exception: OS X 10.4? Issue #19294.)
406+
self._loop.call_soon(self._protocol.connection_made, self)
407+
408+
# On AIX, the reader trick (to be notified when the read end of the
409+
# socket is closed) only works for sockets. On other platforms it
410+
# works for pipes and sockets. (Exception: OS X 10.4? Issue #19294.)
407411
if is_socket or not sys.platform.startswith("aix"):
408-
self._loop.add_reader(self._fileno, self._read_ready)
412+
# only start reading when connection_made() has been called
413+
self._loop.call_soon(self._loop.add_reader,
414+
self._fileno, self._read_ready)
409415

410-
self._loop.call_soon(self._protocol.connection_made, self)
411416
if waiter is not None:
412417
# only wake up the waiter when connection_made() has been called
413418
self._loop.call_soon(waiter._set_result_unless_cancelled, None)

Lib/test/test_asyncio/test_events.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -886,13 +886,18 @@ def test_create_server_ssl_verify_failed(self):
886886
if hasattr(sslcontext_client, 'check_hostname'):
887887
sslcontext_client.check_hostname = True
888888

889+
889890
# no CA loaded
890891
f_c = self.loop.create_connection(MyProto, host, port,
891892
ssl=sslcontext_client)
892-
with test_utils.disable_logger():
893-
with self.assertRaisesRegex(ssl.SSLError,
894-
'certificate verify failed '):
895-
self.loop.run_until_complete(f_c)
893+
with mock.patch.object(self.loop, 'call_exception_handler'):
894+
with test_utils.disable_logger():
895+
with self.assertRaisesRegex(ssl.SSLError,
896+
'certificate verify failed '):
897+
self.loop.run_until_complete(f_c)
898+
899+
# execute the loop to log the connection error
900+
test_utils.run_briefly(self.loop)
896901

897902
# close connection
898903
self.assertIsNone(proto.transport)
@@ -919,15 +924,20 @@ def test_create_unix_server_ssl_verify_failed(self):
919924
f_c = self.loop.create_unix_connection(MyProto, path,
920925
ssl=sslcontext_client,
921926
server_hostname='invalid')
922-
with test_utils.disable_logger():
923-
with self.assertRaisesRegex(ssl.SSLError,
924-
'certificate verify failed '):
925-
self.loop.run_until_complete(f_c)
927+
with mock.patch.object(self.loop, 'call_exception_handler'):
928+
with test_utils.disable_logger():
929+
with self.assertRaisesRegex(ssl.SSLError,
930+
'certificate verify failed '):
931+
self.loop.run_until_complete(f_c)
932+
933+
# execute the loop to log the connection error
934+
test_utils.run_briefly(self.loop)
926935

927936
# close connection
928937
self.assertIsNone(proto.transport)
929938
server.close()
930939

940+
931941
def test_legacy_create_unix_server_ssl_verify_failed(self):
932942
with test_utils.force_legacy_ssl_support():
933943
self.test_create_unix_server_ssl_verify_failed()
@@ -949,11 +959,12 @@ def test_create_server_ssl_match_failed(self):
949959
# incorrect server_hostname
950960
f_c = self.loop.create_connection(MyProto, host, port,
951961
ssl=sslcontext_client)
952-
with test_utils.disable_logger():
953-
with self.assertRaisesRegex(
954-
ssl.CertificateError,
955-
"hostname '127.0.0.1' doesn't match 'localhost'"):
956-
self.loop.run_until_complete(f_c)
962+
with mock.patch.object(self.loop, 'call_exception_handler'):
963+
with test_utils.disable_logger():
964+
with self.assertRaisesRegex(
965+
ssl.CertificateError,
966+
"hostname '127.0.0.1' doesn't match 'localhost'"):
967+
self.loop.run_until_complete(f_c)
957968

958969
# close connection
959970
proto.transport.close()

Lib/test/test_asyncio/test_unix_events.py

Lines changed: 11 additions & 18 deletions

0 commit comments

Comments
 (0)