Skip to content

Commit c6b20be

Browse files
jaketeslermiss-islington
authored andcommitted
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
1 parent 892221b commit c6b20be

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
@@ -361,6 +361,28 @@ def test_process(self):
361361
self.assertNotIn(p, self.active_children())
362362
close_queue(q)
363363

364+
@unittest.skipUnless(threading._HAVE_THREAD_NATIVE_ID, "needs native_id")
365+
def test_process_mainthread_native_id(self):
366+
if self.TYPE == 'threads':
367+
self.skipTest('test not appropriate for {}'.format(self.TYPE))
368+
369+
current_mainthread_native_id = threading.main_thread().native_id
370+
371+
q = self.Queue(1)
372+
p = self.Process(target=self._test_process_mainthread_native_id, args=(q,))
373+
p.start()
374+
375+
child_mainthread_native_id = q.get()
376+
p.join()
377+
close_queue(q)
378+
379+
self.assertNotEqual(current_mainthread_native_id, child_mainthread_native_id)
380+
381+
@classmethod
382+
def _test_process_mainthread_native_id(cls, q):
383+
mainthread_native_id = threading.main_thread().native_id
384+
q.put(mainthread_native_id)
385+
364386
@classmethod
365387
def _sleep_some(cls):
366388
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)