[Python-checkins] bpo-38707: Fix for multiprocessing.Process MainThread.native_id (GH-17088)
Miss Islington (bot)
webhook-mailer at python.org
Tue Nov 19 15:11:25 EST 2019
https://github.com/python/cpython/commit/829593a9262e67c72167c6cb20d383203b2ea410
commit: 829593a9262e67c72167c6cb20d383203b2ea410
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-11-19T12:11:20-08:00
summary:
bpo-38707: Fix for multiprocessing.Process MainThread.native_id (GH-17088)
This PR implements a fix for `multiprocessing.Process` objects; the error occurs when Processes are created using either `fork` or `forkserver` as the `start_method`.
In these instances, the `MainThread` of the newly created `Process` object retains all attributes from its parent's `MainThread` object, including the `native_id` attribute. The resulting behavior is such that the new process' `MainThread` captures an incorrect/outdated `native_id` (the parent's instead of its own).
This change forces the Process object to update its `native_id` attribute during the bootstrap process.
cc @vstinner
https://bugs.python.org/issue38707
Automerge-Triggered-By: @pitrou
(cherry picked from commit c6b20be85c0de6f2355c67ae6e7e578941275cc0)
Co-authored-by: Jake Tesler <jake.tesler at gmail.com>
files:
A Misc/NEWS.d/next/Core and Builtins/2019-11-08-00-36-10.bpo-38707.SZL036.rst
M Lib/multiprocessing/process.py
M Lib/test/_test_multiprocessing.py
diff --git a/Lib/multiprocessing/process.py b/Lib/multiprocessing/process.py
index c62c826cff958..be13c079bb89b 100644
--- a/Lib/multiprocessing/process.py
+++ b/Lib/multiprocessing/process.py
@@ -301,6 +301,8 @@ def _bootstrap(self, parent_sentinel=None):
_current_process = self
_parent_process = _ParentProcess(
self._parent_name, self._parent_pid, parent_sentinel)
+ if threading._HAVE_THREAD_NATIVE_ID:
+ threading.main_thread()._set_native_id()
try:
util._finalizer_registry.clear()
util._run_after_forkers()
diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py
index 1474624ac5d5b..b7f3d253c4d13 100644
--- a/Lib/test/_test_multiprocessing.py
+++ b/Lib/test/_test_multiprocessing.py
@@ -367,6 +367,28 @@ def test_process(self):
self.assertNotIn(p, self.active_children())
close_queue(q)
+ @unittest.skipUnless(threading._HAVE_THREAD_NATIVE_ID, "needs native_id")
+ def test_process_mainthread_native_id(self):
+ if self.TYPE == 'threads':
+ self.skipTest('test not appropriate for {}'.format(self.TYPE))
+
+ current_mainthread_native_id = threading.main_thread().native_id
+
+ q = self.Queue(1)
+ p = self.Process(target=self._test_process_mainthread_native_id, args=(q,))
+ p.start()
+
+ child_mainthread_native_id = q.get()
+ p.join()
+ close_queue(q)
+
+ self.assertNotEqual(current_mainthread_native_id, child_mainthread_native_id)
+
+ @classmethod
+ def _test_process_mainthread_native_id(cls, q):
+ mainthread_native_id = threading.main_thread().native_id
+ q.put(mainthread_native_id)
+
@classmethod
def _sleep_some(cls):
time.sleep(100)
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-11-08-00-36-10.bpo-38707.SZL036.rst b/Misc/NEWS.d/next/Core and Builtins/2019-11-08-00-36-10.bpo-38707.SZL036.rst
new file mode 100644
index 0000000000000..4ef9ed81931b7
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2019-11-08-00-36-10.bpo-38707.SZL036.rst
@@ -0,0 +1 @@
+``MainThread.native_id`` is now correctly reset in child processes spawned using :class:`multiprocessing.Process`, instead of retaining the parent's value.
More information about the Python-checkins
mailing list