Skip to content

Commit 1bceb0e

Browse files
authored
bpo-35998: Fix test_asyncio.test_start_tls_server_1() (GH-16815) (GH-16818)
main() is now responsible to send the ANSWER, rather than ServerProto. main() now waits until it got the HELLO before sending the ANSWER over the new transport. Previously, there was a race condition between main() replacing the protocol and the protocol sending the ANSWER once it gets the HELLO. TLSv1.3 was disabled for the test: reenable it. (cherry picked from commit fab4ef2)
1 parent 5ff8e2d commit 1bceb0e

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

Lib/test/test_asyncio/test_sslproto.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -495,14 +495,6 @@ def test_start_tls_server_1(self):
495495

496496
server_context = test_utils.simple_server_sslcontext()
497497
client_context = test_utils.simple_client_sslcontext()
498-
if (sys.platform.startswith('freebsd')
499-
or sys.platform.startswith('win')
500-
or sys.platform.startswith('darwin')):
501-
# bpo-35031: Some FreeBSD and Windows buildbots fail to run this test
502-
# as the eof was not being received by the server if the payload
503-
# size is not big enough. This behaviour only appears if the
504-
# client is using TLS1.3. Also seen on macOS.
505-
client_context.options |= ssl.OP_NO_TLSv1_3
506498
answer = None
507499

508500
def client(sock, addr):
@@ -519,9 +511,10 @@ def client(sock, addr):
519511
sock.close()
520512

521513
class ServerProto(asyncio.Protocol):
522-
def __init__(self, on_con, on_con_lost):
514+
def __init__(self, on_con, on_con_lost, on_got_hello):
523515
self.on_con = on_con
524516
self.on_con_lost = on_con_lost
517+
self.on_got_hello = on_got_hello
525518
self.data = b''
526519
self.transport = None
527520

@@ -535,7 +528,7 @@ def replace_transport(self, tr):
535528
def data_received(self, data):
536529
self.data += data
537530
if len(self.data) >= len(HELLO_MSG):
538-
self.transport.write(ANSWER)
531+
self.on_got_hello.set_result(None)
539532

540533
def connection_lost(self, exc):
541534
self.transport = None
@@ -544,7 +537,7 @@ def connection_lost(self, exc):
544537
else:
545538
self.on_con_lost.set_exception(exc)
546539

547-
async def main(proto, on_con, on_con_lost):
540+
async def main(proto, on_con, on_con_lost, on_got_hello):
548541
tr = await on_con
549542
tr.write(HELLO_MSG)
550543

@@ -554,17 +547,20 @@ async def main(proto, on_con, on_con_lost):
554547
tr, proto, server_context,
555548
server_side=True,
556549
ssl_handshake_timeout=self.TIMEOUT)
557-
558550
proto.replace_transport(new_tr)
559551

552+
await on_got_hello
553+
new_tr.write(ANSWER)
554+
560555
await on_con_lost
561556
self.assertEqual(proto.data, HELLO_MSG)
562557
new_tr.close()
563558

564559
async def run_main():
565560
on_con = self.loop.create_future()
566561
on_con_lost = self.loop.create_future()
567-
proto = ServerProto(on_con, on_con_lost)
562+
on_got_hello = self.loop.create_future()
563+
proto = ServerProto(on_con, on_con_lost, on_got_hello)
568564

569565
server = await self.loop.create_server(
570566
lambda: proto, '127.0.0.1', 0)
@@ -573,7 +569,7 @@ async def run_main():
573569
with self.tcp_client(lambda sock: client(sock, addr),
574570
timeout=self.TIMEOUT):
575571
await asyncio.wait_for(
576-
main(proto, on_con, on_con_lost),
572+
main(proto, on_con, on_con_lost, on_got_hello),
577573
loop=self.loop, timeout=self.TIMEOUT)
578574

579575
server.close()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix a race condition in test_asyncio.test_start_tls_server_1(). Previously,
2+
there was a race condition between the test main() function which replaces the
3+
protocol and the test ServerProto protocol which sends ANSWER once it gets
4+
HELLO. Now, only the test main() function is responsible to send data,
5+
ServerProto no longer sends data.

0 commit comments

Comments
 (0)