bpo-44422: Fix threading.enumerate() reentrant call (GH-26727) (GH-26738)

The threading.enumerate() function now uses a reentrant lock to
prevent a hang on reentrant call.
(cherry picked from commit 243fd01047)

Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Miss Islington (bot) 2021-06-15 09:30:26 -07:00 committed by GitHub
parent 0d0a9eaa82
commit 8fe57aacc7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 3 deletions

View file

@ -750,8 +750,11 @@ _counter() # Consume 0 so first non-main thread has id 1.
def _newname(template="Thread-%d"):
return template % _counter()
# Active thread administration
_active_limbo_lock = _allocate_lock()
# Active thread administration.
#
# bpo-44422: Use a reentrant lock to allow reentrant calls to functions like
# threading.enumerate().
_active_limbo_lock = RLock()
_active = {} # maps thread id to Thread object
_limbo = {}
_dangling = WeakSet()
@ -1474,7 +1477,7 @@ def _after_fork():
# by another (non-forked) thread. http://bugs.python.org/issue874900
global _active_limbo_lock, _main_thread
global _shutdown_locks_lock, _shutdown_locks
_active_limbo_lock = _allocate_lock()
_active_limbo_lock = RLock()
# fork() only copied the current thread; clear references to others.
new_active = {}

View file

@ -0,0 +1,3 @@
The :func:`threading.enumerate` function now uses a reentrant lock to
prevent a hang on reentrant call.
Patch by Victor Stinner.