Skip to content

Commit cc18279

Browse files
[3.15] gh-160206: Close connection explicitly in test_ssl (GH-160222) (#160273)
gh-160206: Close connection explicitly in test_ssl (GH-160222) test_https_client_non_tls_response_ignored() now uses a context manager for the server and always closes the connection to avoid ResourceWarning. Also add a timeout to SingleConnectionTestServerThread.__exit__() to not hang forever if something goes wrong. (cherry picked from commit 9d42aee) Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 86e3b9f commit cc18279

1 file changed

Lines changed: 27 additions & 21 deletions

File tree

‎Lib/test/test_ssl.py‎

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5920,7 +5920,11 @@ def __exit__(self, *args):
59205920
self.listener.close()
59215921
except OSError:
59225922
pass
5923-
self.join()
5923+
timeout = support.SHORT_TIMEOUT
5924+
self.join(timeout=timeout)
5925+
if self.is_alive():
5926+
raise AssertionError("failed to join the server thread "
5927+
f"in {timeout} seconds")
59245928
self.wrap_error = None # avoid dangling references
59255929

59265930
def start(self):
@@ -6126,28 +6130,30 @@ def call_after_accept(conn_to_client):
61266130
call_after_accept=call_after_accept,
61276131
name="non_tls_http_RST_responder",
61286132
timeout=timeout)
6129-
self.enterContext(server) # starts it & unittest.TestCase stops it.
6130-
# Redundant; call_after_accept sets SO_LINGER on the accepted conn.
6131-
set_socket_so_linger_on_with_zero_timeout(server.listener)
6132-
6133-
connection = SynchronizedHTTPSConnection(
6134-
server.listener.getsockname()[0],
6135-
port=server.port,
6136-
context=ssl.create_default_context(),
6137-
timeout=timeout,
6138-
)
61396133

6140-
# There are lots of reasons this raises as desired, long before this
6141-
# test was added. Sending the request requires a successful TLS wrapped
6142-
# socket; that fails if the connection is broken. It may seem pointless
6143-
# to test this. It serves as an illustration of something that we never
6144-
# want to happen... properly not happening.
6145-
with warnings_helper.check_no_resource_warning(self), \
6146-
self.assertRaises(OSError):
6147-
connection.request("HEAD", "/test", headers={"Host": "localhost"})
6148-
response = connection.getresponse()
6134+
with server:
6135+
# Redundant; call_after_accept sets SO_LINGER on the accepted conn.
6136+
set_socket_so_linger_on_with_zero_timeout(server.listener)
6137+
6138+
connection = SynchronizedHTTPSConnection(
6139+
server.listener.getsockname()[0],
6140+
port=server.port,
6141+
context=ssl.create_default_context(),
6142+
timeout=timeout,
6143+
)
61496144

6150-
server.join()
6145+
# There are lots of reasons this raises as desired, long before this
6146+
# test was added. Sending the request requires a successful TLS wrapped
6147+
# socket; that fails if the connection is broken. It may seem pointless
6148+
# to test this. It serves as an illustration of something that we never
6149+
# want to happen... properly not happening.
6150+
with warnings_helper.check_no_resource_warning(self), \
6151+
self.assertRaises(OSError):
6152+
try:
6153+
connection.request("HEAD", "/test", headers={"Host": "localhost"})
6154+
response = connection.getresponse()
6155+
finally:
6156+
connection.close()
61516157

61526158

61536159
class TestEnumerations(unittest.TestCase):

0 commit comments

Comments
 (0)