Skip to content

Commit ca7fe01

Browse files
committed
bpo-37322: Fix ResourceWarning and exception handling in test
Handle all OSErrors in a single block. OSError also takes care of SSLError and socket's connection errors. Partly reverts commit fb7e750. The threaded connection handler must not raise an unhandled exception.
1 parent b2fac1a commit ca7fe01

File tree

1 file changed

+11
-24
lines changed

1 file changed

+11
-24
lines changed

Lib/test/test_ssl.py

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2482,31 +2482,18 @@ def run(self):
24822482
sys.stdout.write(" server: read %r (%s), sending back %r (%s)...\n"
24832483
% (msg, ctype, msg.lower(), ctype))
24842484
self.write(msg.lower())
2485-
except (ConnectionResetError, ConnectionAbortedError):
2486-
# XXX: OpenSSL 1.1.1 sometimes raises ConnectionResetError
2487-
# when connection is not shut down gracefully.
2485+
except OSError as e:
2486+
# handles SSLError and socket errors
24882487
if self.server.chatty and support.verbose:
2489-
sys.stdout.write(
2490-
" Connection reset by peer: {}\n".format(
2491-
self.addr)
2492-
)
2493-
self.close()
2494-
self.running = False
2495-
except ssl.SSLError as err:
2496-
# On Windows sometimes test_pha_required_nocert receives the
2497-
# PEER_DID_NOT_RETURN_A_CERTIFICATE exception
2498-
# before the 'tlsv13 alert certificate required' exception.
2499-
# If the server is stopped when PEER_DID_NOT_RETURN_A_CERTIFICATE
2500-
# is received test_pha_required_nocert fails with ConnectionResetError
2501-
# because the underlying socket is closed
2502-
if 'PEER_DID_NOT_RETURN_A_CERTIFICATE' == err.reason:
2503-
if self.server.chatty and support.verbose:
2504-
sys.stdout.write(err.args[1])
2505-
# test_pha_required_nocert is expecting this exception
2506-
raise ssl.SSLError('tlsv13 alert certificate required')
2507-
except OSError:
2508-
if self.server.chatty:
2509-
handle_error("Test server failure:\n")
2488+
if isinstance(e, ConnectionError):
2489+
# OpenSSL 1.1.1 sometimes raises
2490+
# ConnectionResetError when connection is not
2491+
# shut down gracefully.
2492+
print(
2493+
f" Connection reset by peer: {self.addr}"
2494+
)
2495+
else:
2496+
handle_error("Test server failure:\n")
25102497
self.close()
25112498
self.running = False
25122499

0 commit comments

Comments
 (0)