classification
Title: sys.exit() in a multiprocessing.Process does not align with Python behavior
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.8, Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: chrahunt, davin, eamanu
Priority: normal Keywords: patch

Created on 2019-01-12 19:33 by chrahunt, last changed 2019-05-13 13:00 by cheryl.sabella.

Files
File name Uploaded Description Edit
multiprocessing-exitcode-3.7.1.patch chrahunt, 2019-01-12 19:33 patch
Pull Requests
URL Status Linked Edit
PR 11538 open chrahunt, 2019-01-12 20:23
PR 11538 open chrahunt, 2019-01-12 20:23
PR 11538 open chrahunt, 2019-01-12 20:23
Messages (2)
msg333531 - (view) Author: Christopher Hunt (chrahunt) * Date: 2019-01-12 19:33
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.
msg333553 - (view) Author: Emmanuel Arias (eamanu) * Date: 2019-01-13 11:49
The same behavior on 3.8 and 3.5
History
Date User Action Args
2019-05-13 13:00:12cheryl.sabellasetnosy: + davin

versions: - Python 3.5
2019-01-13 11:49:14eamanusetnosy: + eamanu

messages: + msg333553
versions: + Python 3.5, Python 3.8
2019-01-12 20:23:42chrahuntsetstage: patch review
pull_requests: + pull_request11145
2019-01-12 20:23:40chrahuntsetstage: (no value)
pull_requests: + pull_request11144
2019-01-12 20:23:36chrahuntsetstage: (no value)
pull_requests: + pull_request11143
2019-01-12 20:08:41chrahuntsetversions: - Python 2.7, Python 3.4, Python 3.5, Python 3.6
2019-01-12 19:33:37chrahuntcreate