]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-40089: Fix threading._after_fork() (GH-19191)
authorVictor Stinner <vstinner@python.org>
Fri, 27 Mar 2020 16:50:42 +0000 (17:50 +0100)
committerGitHub <noreply@github.com>
Fri, 27 Mar 2020 16:50:42 +0000 (17:50 +0100)
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.

Lib/threading.py
Misc/NEWS.d/next/Library/2020-03-27-17-22-34.bpo-40089.-lFsD0.rst [new file with mode: 0644]

index 59323679c80dcdbb7ecd1b76a96555a6a2189ebb..46eb1b918a9a74a6df6cd341e2cd7929c1d660bd 100644 (file)
@@ -1423,7 +1423,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 (file)
index 0000000..f5335a3
--- /dev/null
@@ -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.