[3.12] gh-102512: Turn _DummyThread into _MainThread after os.fork() called from a foreign thread (GH-113261) (GH-114430)

Always set a _MainThread as a main thread after os.fork() is called from
a thread started not by the threading module.

A new _MainThread was already set as a new main thread after fork if
threading.current_thread() was not called for a foreign thread before fork.
Now, if it was called before fork, the implicitly created _DummyThread will
be turned into _MainThread after fork.

It fixes, in particularly, an incompatibility of _DummyThread with
the threading shutdown logic which relies on the main thread
having tstate_lock.

(cherry picked from commit 49785b06de)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
This commit is contained in:
Miss Islington (bot) 2024-01-22 16:24:43 +01:00 committed by GitHub
parent d1f731ff08
commit 1bd2c93474
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 98 additions and 9 deletions

View file

@ -1460,7 +1460,6 @@ class _DummyThread(Thread):
def __init__(self):
Thread.__init__(self, name=_newname("Dummy-%d"),
daemon=_daemon_threads_allowed())
self._started.set()
self._set_ident()
if _HAVE_THREAD_NATIVE_ID:
@ -1685,6 +1684,11 @@ def _after_fork():
# its new value since it can have changed.
thread._reset_internal_locks(True)
ident = get_ident()
if isinstance(thread, _DummyThread):
thread.__class__ = _MainThread
thread._name = 'MainThread'
thread._daemonic = False
thread._set_tstate_lock()
thread._ident = ident
new_active[ident] = thread
else: