Skip to content

Commit 318f5df

Browse files
authored
gh-110167: Fix test_socket deadlock in doCleanups() (#110416)
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().
1 parent 1f3af03 commit 318f5df

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

Lib/test/test_socket.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,13 @@ def setUp(self):
218218
class ThreadSafeCleanupTestCase:
219219
"""Subclass of unittest.TestCase with thread-safe cleanup methods.
220220
221-
This subclass protects the addCleanup() and doCleanups() methods
222-
with a recursive lock.
221+
This subclass protects the addCleanup() method with a recursive lock.
222+
223+
doCleanups() is called when the server completed, but the client can still
224+
be running in its thread especially if the server failed with a timeout.
225+
Don't put a lock on doCleanups() to prevent deadlock between addCleanup()
226+
called in the client and doCleanups() waiting for self.done.wait of
227+
ThreadableTest._setUp() (gh-110167)
223228
"""
224229

225230
def __init__(self, *args, **kwargs):
@@ -230,9 +235,6 @@ def addCleanup(self, *args, **kwargs):
230235
with self._cleanup_lock:
231236
return super().addCleanup(*args, **kwargs)
232237

233-
def doCleanups(self, *args, **kwargs):
234-
with self._cleanup_lock:
235-
return super().doCleanups(*args, **kwargs)
236238

237239
class SocketCANTest(unittest.TestCase):
238240

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix a deadlock in test_socket when server fails with a timeout but the
2+
client is still running in its thread. Don't hold a lock to call cleanup
3+
functions in doCleanups(). One of the cleanup function waits until the
4+
client completes, whereas the client could deadlock if it called
5+
addCleanup() in such situation. Patch by Victor Stinner.

0 commit comments

Comments
 (0)