[New-bugs-announce] [issue46726] Thread spuriously marked dead after interrupting a join call
report at bugs.python.org
Fri Feb 11 18:47:13 EST 2022
New submission from Kevin Shweh <kevin.shweh at gmail.com>:
This code in Thread._wait_for_tstate_lock:
if lock.acquire(block, timeout):
# bpo-45274: lock.acquire() acquired the lock, but the function
# was interrupted with an exception before reaching the
# lock.release(). It can happen if a signal handler raises an
# exception, like CTRL+C which raises KeyboardInterrupt.
has a bug. The "if lock.locked()" check doesn't check whether this code managed to acquire the lock. It checks if *anyone at all* is holding the lock. The lock is almost always locked, so this code will perform a spurious call to self._stop() if it gets interrupted while trying to acquire the lock.
Thread.join uses this method to wait for a thread to finish, so a thread will spuriously be marked dead if you interrupt a join call with Ctrl-C while it's trying to acquire the lock. Here's a reproducer:
event = threading.Event()
t = threading.Thread(target=target)
Interrupt this code with Ctrl-C during the join(), and print(t.is_alive()) will print False.
components: Library (Lib)
nosy: Kevin Shweh
title: Thread spuriously marked dead after interrupting a join call
versions: Python 3.10, Python 3.11, Python 3.9
Python tracker <report at bugs.python.org>
More information about the New-bugs-announce