@@ -5420,6 +5420,81 @@ def test_got_eof(self):
54205420
54215421 self .assertEqual (sslsock .pending (), 0 )
54225422
5423+ def test_oserror_does_not_leak_to_other_sockets (self ):
5424+ # gh-158313: An OSError for one connection must not stay on the
5425+ # thread's OpenSSL error queue and be raised for another connection.
5426+ client_context , server_context , hostname = testing_context ()
5427+ server_context .num_tickets = 0
5428+
5429+ def connect ():
5430+ csock , ssock = socket .socketpair ()
5431+ client = client_context .wrap_socket (
5432+ csock , server_hostname = hostname , do_handshake_on_connect = False )
5433+ server = server_context .wrap_socket (
5434+ ssock , server_side = True , do_handshake_on_connect = False )
5435+ thread = threading .Thread (target = server .do_handshake )
5436+ thread .start ()
5437+ client .do_handshake ()
5438+ thread .join ()
5439+ return client , server
5440+
5441+ client , server = connect ()
5442+ client2 , server2 = connect ()
5443+ with client , server , client2 , server2 :
5444+ server .close ()
5445+ client .settimeout (support .SHORT_TIMEOUT )
5446+ with self .assertRaises (OSError ):
5447+ while True :
5448+ client .send (b'x' * 1024 )
5449+
5450+ client2 .setblocking (False )
5451+ self .assertRaises (ssl .SSLWantReadError , client2 .recv , 1 )
5452+
5453+ def test_truncated_read_does_not_leak_to_other_sockets (self ):
5454+ # gh-158313: If the peer sends FIN then RST without close_notify,
5455+ # OpenSSL 3.5 queues an unexpected EOF error, followed by a system
5456+ # error from its attempt to send an alert. SSL_get_error() reports
5457+ # SSL_ERROR_SSL, whereas a failed write reports SSL_ERROR_SYSCALL.
5458+ client_context , server_context , hostname = testing_context ()
5459+ # Processing a session ticket would clear the error queue.
5460+ server_context .num_tickets = 0
5461+
5462+ listener = socket .create_server ((HOST , 0 ))
5463+ self .addCleanup (listener .close )
5464+
5465+ def connect ():
5466+ csock = socket .create_connection (listener .getsockname (),
5467+ timeout = support .SHORT_TIMEOUT )
5468+ ssock , _ = listener .accept ()
5469+ ssock .settimeout (support .SHORT_TIMEOUT )
5470+ client = client_context .wrap_socket (
5471+ csock , server_hostname = hostname , do_handshake_on_connect = False )
5472+ server = server_context .wrap_socket (
5473+ ssock , server_side = True , do_handshake_on_connect = False )
5474+ thread = threading .Thread (target = server .do_handshake )
5475+ thread .start ()
5476+ client .do_handshake ()
5477+ thread .join ()
5478+ return client , server
5479+
5480+ client , server = connect ()
5481+ client2 , server2 = connect ()
5482+ with client , server , client2 , server2 :
5483+ server .sendall (b'part of a response' )
5484+ server .shutdown (socket .SHUT_WR ) # FIN, without close_notify
5485+ server .setsockopt (socket .SOL_SOCKET , socket .SO_LINGER ,
5486+ struct .pack ('ii' , 1 , 0 ))
5487+ server .close () # RST
5488+
5489+ # How the truncation is reported depends on the platform and the
5490+ # OpenSSL version: EOF (b'') or OSError.
5491+ with contextlib .suppress (OSError ):
5492+ while client .recv (1024 ):
5493+ pass
5494+
5495+ client2 .setblocking (False )
5496+ self .assertRaises (ssl .SSLWantReadError , client2 .recv , 1 )
5497+
54235498
54245499@unittest .skipUnless (has_tls_version ('TLSv1_3' ) and ssl .HAS_PHA ,
54255500 "Test needs TLS 1.3 PHA" )
0 commit comments