Skip to content

Multiprocessing: bug with Native ID for threading.mainthread() #82888

Closed

Description

@jaketesler
mannequin
BPO 38707
Nosy @pitrou, @vstinner, @applio, @miss-islington, @jaketesler
PRs
  • bpo-38707: Fix for multiprocessing.Process MainThread.native_id #17088
  • [3.8] bpo-38707: Fix for multiprocessing.Process MainThread.native_id (GH-17088) #17261
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2019-11-19.21:35:38.239>
    created_at = <Date 2019-11-05.22:39:45.970>
    labels = ['3.8', 'type-bug', 'library', '3.9']
    title = 'Multiprocessing: bug with Native ID for threading.mainthread()'
    updated_at = <Date 2019-11-19.22:27:08.361>
    user = 'https://github.com/jaketesler'

    bugs.python.org fields:

    activity = <Date 2019-11-19.22:27:08.361>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-11-19.21:35:38.239>
    closer = 'pitrou'
    components = ['Library (Lib)']
    creation = <Date 2019-11-05.22:39:45.970>
    creator = 'jaketesler'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38707
    keywords = ['patch']
    message_count = 8.0
    messages = ['356070', '356202', '356218', '356543', '356987', '356989', '356992', '356998']
    nosy_count = 5.0
    nosy_names = ['pitrou', 'vstinner', 'davin', 'miss-islington', 'jaketesler']
    pr_nums = ['17088', '17261']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue38707'
    versions = ['Python 3.8', 'Python 3.9']

    Activity

    1. jaketesler commented on Nov 5, 2019

      jaketeslermannequin
      MannequinAuthor

      I have encountered a minor bug with the new threading.get_native_id() featureset in Python 3.8. The bug occurs when creating a new multiprocessing.Process object on Unix (or on any platform where the multiprocessing start_method is 'fork' or 'forkserver').

      When creating a new process via fork, the Native ID in the new MainThread is incorrect. The new forked process' threading.MainThread object inherits the Native ID from the parent process' MainThread instead of capturing/updating its own (new) Native ID.

      See the following snippet:

      >>> import threading, multiprocessing
      >>> multiprocessing.set_start_method('fork') # or 'forkserver'
      >>> def proc(): print(threading.get_native_id(), threading.main_thread().native_id) # get_native_id(), mainthread.native_id
      >>> proc()
      22605 22605 # get_native_id(), mainthread.native_id
      >>> p = multiprocessing.Process(target=proc)
      >>> p.start()
      22648 22605 # get_native_id(), mainthread.native_id
      >>>
      >>> def update(): threading.main_thread()._set_native_id()
      >>> def print_and_update(): proc(); update(); proc()
      >>> print_and_update()
      22605 22605 # get_native_id(), mainthread.native_id
      22605 22605 
      >>> p2=multiprocessing.Process(target=print_and_update); p2.start()
      22724 22605 # get_native_id(), mainthread.native_id
      22724 22724
      >>> print_and_update()
      22605 22605 # get_native_id(), mainthread.native_id
      22605 22605

      As you can see, the new Process object's MainThread.native_id attribute matches that of the MainThread of its parent process.

      Unfortunately, I'm not too familiar with the underlying mechanisms that Multiprocessing uses to create forked processes.
      I believe this behavior occurs because (AFAIK) a forked multiprocessing.Process copies the MainThread object from its parent process, rather than reinitializing a new one. Looking further into the multiprocessing code, it appears the right spot to fix this would be in the multiprocessing.Process.bootstrap() function.

      I've created a branch containing a working fix - I'm also open to suggestions of how a fix might otherwise be implemented.
      If it looks correct I'll create a PR against the CPython 3.8 branch.

      See the branch here: https://github.com/jaketesler/cpython/tree/fix-mp-native-id

      Thanks all!
      -Jake

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Nov 5, 2019
    3. vstinner commented on Nov 7, 2019

      @vstinner
      Member

      See the branch here: https://github.com/jaketesler/cpython/tree/fix-mp-native-id

      Can you please create a PR?

    4. jaketesler commented on Nov 8, 2019

      jaketeslermannequin
      MannequinAuthor

      @vstinner PR created :)
      #17088

    5. jaketesler commented on Nov 13, 2019

      jaketeslermannequin
      MannequinAuthor

      PR was updated with tests and is ready for core developer review and then the merge to cpython:master. After that (if I understand correctly) a backport will automatically get picked into the 3.8 branch if there aren't any conflicts.

    6. miss-islington commented on Nov 19, 2019

      @miss-islington
      Contributor

      New changeset c6b20be by Miss Islington (bot) (Jake Tesler) in branch 'master':
      bpo-38707: Fix for multiprocessing.Process MainThread.native_id (GH-17088)
      c6b20be

    7. miss-islington commented on Nov 19, 2019

      @miss-islington
      Contributor

      New changeset 829593a by Miss Islington (bot) in branch '3.8':
      bpo-38707: Fix for multiprocessing.Process MainThread.native_id (GH-17088)
      829593a

    8. pitrou commented on Nov 19, 2019

      @pitrou
      Member

      Thank you Jake for the report and PR!

    9. vstinner commented on Nov 19, 2019

      @vstinner
      Member

      Thanks for the fix. That was an interesting bug ;-) I like the simplicity of the fix.

    10. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.8 (EOL)end of life3.9 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions