summaryrefslogtreecommitdiffstats
path: root/Lib/threading.py
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2023-10-02 20:12:12 (GMT)
committerGitHub <noreply@github.com>2023-10-02 20:12:12 (GMT)
commit1dd9dee45d2591b4e701039d1673282380696849 (patch)
tree14199e2d03ef74ca575d64552676d4797d792d9c /Lib/threading.py
parenta040a32ea2f13f16172394d3e3e3f80f47f25a68 (diff)
downloadcpython-1dd9dee45d2591b4e701039d1673282380696849.zip
cpython-1dd9dee45d2591b4e701039d1673282380696849.tar.gz
cpython-1dd9dee45d2591b4e701039d1673282380696849.tar.bz2
gh-105716: Support Background Threads in Subinterpreters Consistently (gh-109921)
The existence of background threads running on a subinterpreter was preventing interpreters from getting properly destroyed, as well as impacting the ability to run the interpreter again. It also affected how we wait for non-daemon threads to finish. We add PyInterpreterState.threads.main, with some internal C-API functions.
Diffstat (limited to 'Lib/threading.py')
-rw-r--r--Lib/threading.py4
1 files changed, 3 insertions, 1 deletions
diff --git a/Lib/threading.py b/Lib/threading.py
index 31cefd2..0edfaf8 100644
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -38,6 +38,7 @@ _daemon_threads_allowed = _thread.daemon_threads_allowed
_allocate_lock = _thread.allocate_lock
_set_sentinel = _thread._set_sentinel
get_ident = _thread.get_ident
+_is_main_interpreter = _thread._is_main_interpreter
try:
get_native_id = _thread.get_native_id
_HAVE_THREAD_NATIVE_ID = True
@@ -1574,7 +1575,7 @@ def _shutdown():
# the main thread's tstate_lock - that won't happen until the interpreter
# is nearly dead. So we release it here. Note that just calling _stop()
# isn't enough: other threads may already be waiting on _tstate_lock.
- if _main_thread._is_stopped:
+ if _main_thread._is_stopped and _is_main_interpreter():
# _shutdown() was already called
return
@@ -1627,6 +1628,7 @@ def main_thread():
In normal conditions, the main thread is the thread from which the
Python interpreter was started.
"""
+ # XXX Figure this out for subinterpreters. (See gh-75698.)
return _main_thread
# get thread-local implementation, either from the thread