Skip to content

Commit 829593a

Browse files
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 c6b20be) Co-authored-by: Jake Tesler <[email protected]>
1 parent 6c3b471 commit 829593a

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

Lib/multiprocessing/process.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ def _bootstrap(self, parent_sentinel=None):
301301
_current_process = self
302302
_parent_process = _ParentProcess(
303303
self._parent_name, self._parent_pid, parent_sentinel)
304+
if threading._HAVE_THREAD_NATIVE_ID:
305+
threading.main_thread()._set_native_id()
304306
try:
305307
util._finalizer_registry.clear()
306308
util._run_after_forkers()

Lib/test/_test_multiprocessing.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,28 @@ def test_process(self):
367367
self.assertNotIn(p, self.active_children())
368368
close_queue(q)
369369

370+
@unittest.skipUnless(threading._HAVE_THREAD_NATIVE_ID, "needs native_id")
371+
def test_process_mainthread_native_id(self):
372+
if self.TYPE == 'threads':
373+
self.skipTest('test not appropriate for {}'.format(self.TYPE))
374+
375+
current_mainthread_native_id = threading.main_thread().native_id
376+
377+
q = self.Queue(1)
378+
p = self.Process(target=self._test_process_mainthread_native_id, args=(q,))
379+
p.start()
380+
381+
child_mainthread_native_id = q.get()
382+
p.join()
383+
close_queue(q)
384+
385+
self.assertNotEqual(current_mainthread_native_id, child_mainthread_native_id)
386+
387+
@classmethod
388+
def _test_process_mainthread_native_id(cls, q):
389+
mainthread_native_id = threading.main_thread().native_id
390+
q.put(mainthread_native_id)
391+
370392
@classmethod
371393
def _sleep_some(cls):
372394
time.sleep(100)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``MainThread.native_id`` is now correctly reset in child processes spawned using :class:`multiprocessing.Process`, instead of retaining the parent's value.

0 commit comments

Comments
 (0)