Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
gh-110167: Fix test_socket deadlock in doCleanups()
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().
  • Loading branch information
vstinner committed Oct 5, 2023
commit 98ac8981de7b1818f0d1d7042878399a7d966754
12 changes: 7 additions & 5 deletions Lib/test/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,13 @@ def setUp(self):
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):
Expand All @@ -230,9 +235,6 @@ def addCleanup(self, *args, **kwargs):
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):

Expand Down
Original file line number Diff line number Diff line change
@@ -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.