[Python-checkins] bpo-40089: Fix threading._after_fork() (GH-19191) (GH-19194)

Miss Islington (bot) webhook-mailer at python.org
Tue Apr 7 17:35:56 EDT 2020


https://github.com/python/cpython/commit/6318e45bda6c37d5497f33a6039cdb65aa494c93
commit: 6318e45bda6c37d5497f33a6039cdb65aa494c93
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-04-07T23:35:52+02:00
summary:

bpo-40089: Fix threading._after_fork() (GH-19191) (GH-19194)

If fork was not called by a thread spawned by threading.Thread,
threading._after_fork() now creates a _MainThread instance for
_main_thread, instead of a _DummyThread instance.
(cherry picked from commit d8ff44ce4cd6f3ec0fab5fccda6bf14afcb25c30)

Co-authored-by: Victor Stinner <vstinner at python.org>

files:
A Misc/NEWS.d/next/Library/2020-03-27-17-22-34.bpo-40089.-lFsD0.rst
M Lib/threading.py

diff --git a/Lib/threading.py b/Lib/threading.py
index 2f6ac7036f5f6..813dae2aa9f8e 100644
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -1421,7 +1421,15 @@ def _after_fork():
 
     # fork() only copied the current thread; clear references to others.
     new_active = {}
-    current = current_thread()
+
+    try:
+        current = _active[get_ident()]
+    except KeyError:
+        # fork() was called in a thread which was not spawned
+        # by threading.Thread. For example, a thread spawned
+        # by thread.start_new_thread().
+        current = _MainThread()
+
     _main_thread = current
 
     # reset _shutdown() locks: threads re-register their _tstate_lock below
diff --git a/Misc/NEWS.d/next/Library/2020-03-27-17-22-34.bpo-40089.-lFsD0.rst b/Misc/NEWS.d/next/Library/2020-03-27-17-22-34.bpo-40089.-lFsD0.rst
new file mode 100644
index 0000000000000..f5335a33c066c
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-03-27-17-22-34.bpo-40089.-lFsD0.rst
@@ -0,0 +1,3 @@
+Fix threading._after_fork(): if fork was not called by a thread spawned by
+threading.Thread, threading._after_fork() now creates a _MainThread instance
+for _main_thread, instead of a _DummyThread instance.



More information about the Python-checkins mailing list