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 cannot spawn grandchild from a Windows service #70621

Closed
schlamar mannequin opened this issue Feb 25, 2016 · 9 comments
Closed

multiprocessing cannot spawn grandchild from a Windows service #70621

schlamar mannequin opened this issue Feb 25, 2016 · 9 comments
Assignees
Labels
OS-windows stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@schlamar
Copy link
Mannequin

schlamar mannequin commented Feb 25, 2016

BPO 26434
Nosy @pfmoore, @tjguk, @schlamar, @zware, @zooba, @applio
PRs
  • bpo-26434: Fix multiprocessing grandchilds in a Windows service #1167
  • Files
  • test_mp_service.py
  • issue_26434_fix_win32_service_parent_origin.patch: patch sans test for 2.7 branch
  • 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 = 'https://github.com/applio'
    closed_at = <Date 2017-05-19.00:42:32.226>
    created_at = <Date 2016-02-25.09:56:47.427>
    labels = ['type-bug', 'library', 'OS-windows']
    title = 'multiprocessing cannot spawn grandchild from a Windows service'
    updated_at = <Date 2017-05-19.00:42:32.225>
    user = 'https://github.com/schlamar'

    bugs.python.org fields:

    activity = <Date 2017-05-19.00:42:32.225>
    actor = 'davin'
    assignee = 'davin'
    closed = True
    closed_date = <Date 2017-05-19.00:42:32.226>
    closer = 'davin'
    components = ['Library (Lib)', 'Windows']
    creation = <Date 2016-02-25.09:56:47.427>
    creator = 'schlamar'
    dependencies = []
    files = ['42025', '42034']
    hgrepos = []
    issue_num = 26434
    keywords = ['patch']
    message_count = 9.0
    messages = ['260846', '260895', '260910', '261149', '261165', '261166', '292967', '293945', '293946']
    nosy_count = 6.0
    nosy_names = ['paul.moore', 'tim.golden', 'schlamar', 'zach.ware', 'steve.dower', 'davin']
    pr_nums = ['1167']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue26434'
    versions = ['Python 2.7']

    @schlamar
    Copy link
    Mannequin Author

    schlamar mannequin commented Feb 25, 2016

    This is a follow up of bpo-5162.

    There are some occasions where you can still run into this issue. One example is if you want to spawn a new multiprocessing.Process as a child of a multiprocessing.Process:

    pythonservice.exe
      - multiprocessing.Process
        - multiprocessing.Process (does not start!)
    

    Attached is a test case. If you run this in pywin32 service debug mode, you see that the process crashes with:

        Traceback (most recent call last):
          File "<string>", line 1, in <module>
          File "C:\Python27\lib\multiprocessing\forking.py", line 380, in main
            prepare(preparation_data)
          File "C:\Python27\lib\multiprocessing\forking.py", line 503, in prepare
            file, path_name, etc = imp.find_module(main_name, dirs)
        ImportError: No module named PythonService

    In get_preparation_data is the following state:

    WINSERVICE: False
    WINEXE: False
    _python_exe: C:\Python27\python.exe

    And so you get as preparation data:

    {'authkey': '...', 'sys_path': [...], 'name': 'test', 'orig_dir': '...', 'sys_argv': ['C:\\Python27\\lib\\site-packages\\win32\\PythonService.exe'], 'main_path': 'C:\\Python27\\lib\\site-packages\\win32\\PythonService.exe', 'log_to_stderr': False}

    A workaround for me is patching get_preparation_data as follows:

        import multiprocessing.forking
    
        _org_get_preparation_data = multiprocessing.forking.get_preparation_data
    
        def _get_preparation_data(*args):
            data = _org_get_preparation_data(*args)
            main_path = data.get('main_path')
            if main_path is not None and main_path.endswith('exe'):
                data.pop('main_path')
            return data
    
        multiprocessing.forking.get_preparation_data = _get_preparation_data

    BTW, the test case does not run on Python 3.5, but starting the service manually did work. So this is probably a Python 2 issue only.

    @schlamar schlamar mannequin added type-crash A hard crash of the interpreter, possibly with a core dump stdlib Python modules in the Lib dir OS-windows labels Feb 25, 2016
    @applio
    Copy link
    Member

    applio commented Feb 26, 2016

    I can reproduce the problem under Windows 7. Thank you for your example and description -- they were very helpful.

    Detection that the original parent was PythonService.exe is necessary to avoid undoing the paths set appropriately for running under a service. The current code only detects the immediate parent.

    Modifying get_preparation_data() in lib/multiprocessing/forking.py to perform an additional test on the inherited values from the original parent's sys.argv (available via the preparation_data key 'sys_argv') would resolve this:
    if not d['sys_argv'][0].lower().endswith("pythonservice.exe"):
    ....

    Potential complications to existing code appear very unlikely given its nature.

    Patch forthcoming after running tests unless someone wants to beat me to it.

    @applio applio self-assigned this Feb 26, 2016
    @applio applio added type-bug An unexpected behavior, bug, or error and removed type-crash A hard crash of the interpreter, possibly with a core dump labels Feb 26, 2016
    @applio
    Copy link
    Member

    applio commented Feb 26, 2016

    Attached is a patch for the 2.7 branch which adds the check described in the previous message.

    I don't see a reasonable way to provide an accompanying test because it requires registering a win32 service (requiring administrative privileges) temporarily to attempt to provoke the issue.

    Can I request that someone else run the full regression tests on this patch? I am unable to complete a standard run of tests as my Windows 7 development system is set up for building Python 3.x (newer Visual Studio) and am hesitant to fight with Visual Studio configurations.

    @schlamar
    Copy link
    Mannequin Author

    schlamar mannequin commented Mar 3, 2016

    I can confirm that this patch is working.

    Maybe the test case can be modified to simulate a "pythonservice.exe" run (by patching sys.executable or something like that) so it can be run without admin privileges.

    @schlamar
    Copy link
    Mannequin Author

    schlamar mannequin commented Mar 3, 2016

    We have some business privilege management solution running which might have corrupted the installation. As no one else is reporting this issue, this is my best guest for this phenomena and I'm going to close this issue.

    @schlamar schlamar mannequin closed this as completed Mar 3, 2016
    @schlamar
    Copy link
    Mannequin Author

    schlamar mannequin commented Mar 3, 2016

    Wrong bug...

    @schlamar schlamar mannequin reopened this Mar 3, 2016
    @schlamar
    Copy link
    Mannequin Author

    schlamar mannequin commented May 4, 2017

    I opened a PR on GitHub, please review.

    @applio
    Copy link
    Member

    applio commented May 19, 2017

    New changeset c47c315 by Davin Potts (Marc Schlaich) in branch '2.7':
    bpo-26434: Fix multiprocessing grandchilds in a Windows service (GH-1167)
    c47c315

    @applio
    Copy link
    Member

    applio commented May 19, 2017

    Patch committed in 2.7 branch.

    Thanks for your help, Marc.

    @applio applio closed this as completed May 19, 2017
    @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
    OS-windows stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant