]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-110167: Fix test_socket deadlock in doCleanups() (#110416)
authorVictor Stinner <vstinner@python.org>
Thu, 5 Oct 2023 18:53:03 +0000 (20:53 +0200)
committerGitHub <noreply@github.com>
Thu, 5 Oct 2023 18:53:03 +0000 (20:53 +0200)
Fix a deadlock in test_socket when server fails with a timeout but
the client is still running in its thread. Don't hold a lock to call
cleanup functions in doCleanups(). One of the cleanup function waits
until the client completes, whereas the client could deadlock if it
called addCleanup() in such situation.

doCleanups() is called when the server completed, but the client can
still be running in its thread especially if the server failed with a
timeout. Don't put a lock on doCleanups() to prevent deadlock between
addCleanup() called in the client and doCleanups() waiting for
self.done.wait of ThreadableTest._setUp().

Lib/test/test_socket.py
Misc/NEWS.d/next/Tests/2023-10-05-19-33-49.gh-issue-110167.mIdj3v.rst [new file with mode: 0644]

index 09605f7e774dda60818831440cf9311f9e81526d..0d1c1867a25c407c48943f43003b3213a598cca9 100644 (file)
@@ -218,8 +218,13 @@ class SocketUDPLITETest(SocketUDPTest):
 class ThreadSafeCleanupTestCase:
     """Subclass of unittest.TestCase with thread-safe cleanup methods.
 
-    This subclass protects the addCleanup() and doCleanups() methods
-    with a recursive lock.
+    This subclass protects the addCleanup() method with a recursive lock.
+
+    doCleanups() is called when the server completed, but the client can still
+    be running in its thread especially if the server failed with a timeout.
+    Don't put a lock on doCleanups() to prevent deadlock between addCleanup()
+    called in the client and doCleanups() waiting for self.done.wait of
+    ThreadableTest._setUp() (gh-110167)
     """
 
     def __init__(self, *args, **kwargs):
@@ -230,9 +235,6 @@ class ThreadSafeCleanupTestCase:
         with self._cleanup_lock:
             return super().addCleanup(*args, **kwargs)
 
-    def doCleanups(self, *args, **kwargs):
-        with self._cleanup_lock:
-            return super().doCleanups(*args, **kwargs)
 
 class SocketCANTest(unittest.TestCase):
 
diff --git a/Misc/NEWS.d/next/Tests/2023-10-05-19-33-49.gh-issue-110167.mIdj3v.rst b/Misc/NEWS.d/next/Tests/2023-10-05-19-33-49.gh-issue-110167.mIdj3v.rst
new file mode 100644 (file)
index 0000000..d0cbbf9
--- /dev/null
@@ -0,0 +1,5 @@
+Fix a deadlock in test_socket when server fails with a timeout but the
+client is still running in its thread. Don't hold a lock to call cleanup
+functions in doCleanups(). One of the cleanup function waits until the
+client completes, whereas the client could deadlock if it called
+addCleanup() in such situation. Patch by Victor Stinner.