From 7dacb70485a0910eb298c24b4d051720ca56fb91 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 16 Sep 2021 13:30:00 +0300 Subject: 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. --- Lib/test/test_socket.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 7ac9260..382e726 100755 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -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 -- cgit v0.12