bpo-45212: Fix dangling threads in skipped tests in test_socket (GH-28361)

tearDown() is not called if setUp() raises an exception
(including SkipTest). addCleanup() should be used for guaranteed
execution of the cleanup code.
This commit is contained in:
Serhiy Storchaka 2021-09-16 13:30:00 +03:00 committed by GitHub
parent 800bd01b6d
commit 7dacb70485
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 11 deletions

View File

@ -326,9 +326,7 @@ class ThreadableTest:
def __init__(self):
# Swap the true setup function
self.__setUp = self.setUp
self.__tearDown = self.tearDown
self.setUp = self._setUp
self.tearDown = self._tearDown
def serverExplicitReady(self):
"""This method allows the server to explicitly indicate that
@ -340,6 +338,7 @@ class ThreadableTest:
def _setUp(self):
self.wait_threads = threading_helper.wait_threads_exit()
self.wait_threads.__enter__()
self.addCleanup(self.wait_threads.__exit__, None, None, None)
self.server_ready = threading.Event()
self.client_ready = threading.Event()
@ -347,6 +346,11 @@ class ThreadableTest:
self.queue = queue.Queue(1)
self.server_crashed = False
def raise_queued_exception():
if self.queue.qsize():
raise self.queue.get()
self.addCleanup(raise_queued_exception)
# Do some munging to start the client test.
methodname = self.id()
i = methodname.rfind('.')
@ -363,15 +367,7 @@ class ThreadableTest:
finally:
self.server_ready.set()
self.client_ready.wait()
def _tearDown(self):
self.__tearDown()
self.done.wait()
self.wait_threads.__exit__(None, None, None)
if self.queue.qsize():
exc = self.queue.get()
raise exc
self.addCleanup(self.done.wait)
def clientRun(self, test_func):
self.server_ready.wait()
@ -6211,6 +6207,7 @@ class SendfileUsingSendTest(ThreadedTCPSocketTest):
def testWithTimeoutTriggeredSend(self):
conn = self.accept_conn()
conn.recv(88192)
time.sleep(1)
# errors