Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jaketesler mannequin opened this issue Nov 5, 2019 · 8 comments
Closed

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

jaketesler mannequin opened this issue Nov 5, 2019 · 8 comments
Labels
3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@jaketesler
Copy link
Mannequin

jaketesler mannequin commented Nov 5, 2019

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']

    @jaketesler
    Copy link
    Mannequin Author

    jaketesler mannequin commented Nov 5, 2019

    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

    @jaketesler jaketesler mannequin added 3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Nov 5, 2019
    @vstinner
    Copy link
    Member

    vstinner commented Nov 7, 2019

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

    Can you please create a PR?

    @jaketesler
    Copy link
    Mannequin Author

    jaketesler mannequin commented Nov 8, 2019

    @vstinner PR created :)
    #17088

    @jaketesler
    Copy link
    Mannequin Author

    jaketesler mannequin commented Nov 13, 2019

    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.

    @miss-islington
    Copy link
    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

    @miss-islington
    Copy link
    Contributor

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

    @pitrou
    Copy link
    Member

    pitrou commented Nov 19, 2019

    Thank you Jake for the report and PR!

    @pitrou pitrou added the 3.9 only security fixes label Nov 19, 2019
    @pitrou pitrou closed this as completed Nov 19, 2019
    @vstinner
    Copy link
    Member

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

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants