[Python-checkins] [3.9] bpo-45212: Fix dangling threads in skipped tests in test_socket (GH-28361) (GH-28408)

serhiy-storchaka webhook-mailer at python.org
Fri Sep 17 06:11:54 EDT 2021


https://github.com/python/cpython/commit/10c3cf78518f4b31e1527c2795694b1bcb092696
commit: 10c3cf78518f4b31e1527c2795694b1bcb092696
branch: 3.9
author: Serhiy Storchaka <storchaka at gmail.com>
committer: serhiy-storchaka <storchaka at gmail.com>
date: 2021-09-17T13:11:50+03:00
summary:

[3.9] bpo-45212: Fix dangling threads in skipped tests in test_socket (GH-28361) (GH-28408)

tearDown() is not called if setUp() raises an exception
(including SkipTest). addCleanup() should be used for guaranteed
execution of the cleanup code.
(cherry picked from commit 7dacb70485a0910eb298c24b4d051720ca56fb91)

files:
M Lib/test/test_socket.py

diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index e0f80ca59cbff..fde306035cc6c 100755
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -324,9 +324,7 @@ def _testFoo(self):
     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
@@ -338,6 +336,7 @@ def serverExplicitReady(self):
     def _setUp(self):
         self.wait_threads = support.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()
@@ -345,6 +344,11 @@ def _setUp(self):
         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('.')
@@ -361,15 +365,7 @@ def _setUp(self):
         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()
@@ -6172,6 +6168,7 @@ def _testWithTimeoutTriggeredSend(self):
     def testWithTimeoutTriggeredSend(self):
         conn = self.accept_conn()
         conn.recv(88192)
+        time.sleep(1)
 
     # errors
 



More information about the Python-checkins mailing list