diff --git a/Lib/test/test_asyncio/test_sslproto.py b/Lib/test/test_asyncio/test_sslproto.py index 464d55cf80ca03..08f680893ad792 100644 --- a/Lib/test/test_asyncio/test_sslproto.py +++ b/Lib/test/test_asyncio/test_sslproto.py @@ -1099,7 +1099,15 @@ def server(sock): orig_sock.send(b'please corrupt the SSL connection') # the client now closes the connection; although its # TLS shutdown fails on the corrupted record, it must - # still send close_notify, completing our unwrap() + # still send close_notify, on which recv() returns b''. + # Wait for it before sending ours: unwrap() right away + # would send close_notify to a peer that is about to + # close its socket, which turns that close into a TCP + # reset, and on Windows a reset discards the client's + # close_notify that is still unread (gh-158646). + sock.recv(1) + # recv() also returns b'' on a ragged EOF, i.e. without + # close_notify, but then unwrap() fails with an SSLError. sock.unwrap() except ssl.SSLError as exc: server_err = exc diff --git a/Misc/NEWS.d/next/Tests/2026-10-28-20-40-00.gh-issue-158646.k2Xw7q.rst b/Misc/NEWS.d/next/Tests/2026-10-28-20-40-00.gh-issue-158646.k2Xw7q.rst new file mode 100644 index 00000000000000..93a7c26c632140 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2026-10-28-20-40-00.gh-issue-158646.k2Xw7q.rst @@ -0,0 +1,3 @@ +Fix a race in ``test_asyncio.test_sslproto`` where the server side of +``test_shutdown_corrupted_ssl_sends_close_notify`` could fail with a +connection reset on Windows.