This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author chrahunt
Recipients chrahunt
Date 2019-01-12.19:33:36
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1547321617.14.0.566390882886.issue35727@roundup.psfhosted.org>
In-reply-to
Content
When a function is executed by a multiprocessing.Process and uses sys.exit,
the actual exit code reported by multiprocessing is different than would be
expected given the Python interpreter behavior and documentation. For example,
given:

    from functools import partial
    from multiprocessing import get_context
    import sys
    
    
    def run(ctx, fn):
        p = ctx.Process(target=fn)
        p.start()
        p.join()
        return p.exitcode
    
    
    if __name__ == '__main__':
        ctx = get_context('fork')
        print(run(ctx, partial(sys.exit, 2)))
        print(run(ctx, partial(sys.exit, None)))
        print(run(ctx, sys.exit))
    
        ctx = get_context('spawn')
        print(run(ctx, partial(sys.exit, 2)))
        print(run(ctx, partial(sys.exit, None)))
        print(run(ctx, sys.exit))
    
        ctx = get_context('forkserver')
        print(run(ctx, partial(sys.exit, 2)))
        print(run(ctx, partial(sys.exit, None)))
        print(run(ctx, sys.exit))

when executed results in

    $ python exit.py
    2
    1
    1
    2
    1
    1
    2
    1
    1


but when Python itself is executed we see different behavior

    $ for arg in 2 None ''; do python -c "import sys; sys.exit($arg)"; echo $?; done
    2
    0
    0

The documentation states

> sys.exit([arg])
> ...
> The optional argument arg can be an integer giving the exit status
> (defaulting to zero), or another type of object.

The relevant line in multiprocessing (https://github.com/python/cpython/blame/1cffd0eed313011c0c2bb071c8affeb4a7ed05c7/Lib/multiprocessing/process.py#L307)
seems to be from the original pyprocessing module itself, and I could
not locate an active site that maintains the repository to see if there
was any justification for the behavior.
History
Date User Action Args
2019-01-12 19:33:40chrahuntsetrecipients: + chrahunt
2019-01-12 19:33:37chrahuntsetmessageid: <1547321617.14.0.566390882886.issue35727@roundup.psfhosted.org>
2019-01-12 19:33:37chrahuntlinkissue35727 messages
2019-01-12 19:33:36chrahuntcreate