Skip to content

Commit 1ecb641

Browse files
bpo-45274: Fix Thread._wait_for_tstate_lock() race condition (pythonGH-28532)
Fix a race condition in the Thread.join() method of the threading module. If the function is interrupted by a signal and the signal handler raises an exception, make sure that the thread remains in a consistent state to prevent a deadlock. (cherry picked from commit a22be49) Co-authored-by: Victor Stinner <[email protected]>
1 parent 08e387a commit 1ecb641

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

Lib/threading.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,11 +1064,24 @@ def _wait_for_tstate_lock(self, block=True, timeout=-1):
10641064
# If the lock is acquired, the C code is done, and self._stop() is
10651065
# called. That sets ._is_stopped to True, and ._tstate_lock to None.
10661066
lock = self._tstate_lock
1067-
if lock is None: # already determined that the C code is done
1067+
if lock is None:
1068+
# already determined that the C code is done
10681069
assert self._is_stopped
1069-
elif lock.acquire(block, timeout):
1070-
lock.release()
1071-
self._stop()
1070+
return
1071+
1072+
try:
1073+
if lock.acquire(block, timeout):
1074+
lock.release()
1075+
self._stop()
1076+
except:
1077+
if lock.locked():
1078+
# bpo-45274: lock.acquire() acquired the lock, but the function
1079+
# was interrupted with an exception before reaching the
1080+
# lock.release(). It can happen if a signal handler raises an
1081+
# exception, like CTRL+C which raises KeyboardInterrupt.
1082+
lock.release()
1083+
self._stop()
1084+
raise
10721085

10731086
@property
10741087
def name(self):
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix a race condition in the :meth:`Thread.join() <threading.Thread.join>`
2+
method of the :mod:`threading` module. If the function is interrupted by a
3+
signal and the signal handler raises an exception, make sure that the thread
4+
remains in a consistent state to prevent a deadlock. Patch by Victor
5+
Stinner.

0 commit comments

Comments
 (0)