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

subprocess: optional auto-reaping fixing os.wait() lossage #41885

Closed
yorick mannequin opened this issue Apr 21, 2005 · 5 comments
Closed

subprocess: optional auto-reaping fixing os.wait() lossage #41885

yorick mannequin opened this issue Apr 21, 2005 · 5 comments
Labels
stdlib Python modules in the Lib dir

Comments

@yorick
Copy link
Mannequin

yorick mannequin commented Apr 21, 2005

BPO 1187312
Nosy @loewis
Files
  • subprocess.patch: subprocess.patch
  • 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 2020-03-26.17:08:48.160>
    created_at = <Date 2005-04-21.11:40:17.000>
    labels = ['library']
    title = 'subprocess: optional auto-reaping fixing os.wait() lossage'
    updated_at = <Date 2020-03-26.17:08:48.160>
    user = 'https://bugs.python.org/yorick'

    bugs.python.org fields:

    activity = <Date 2020-03-26.17:08:48.160>
    actor = 'Justin.Lebar'
    assignee = 'none'
    closed = True
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2005-04-21.11:40:17.000>
    creator = 'yorick'
    dependencies = []
    files = ['6629']
    hgrepos = []
    issue_num = 1187312
    keywords = ['patch']
    message_count = 5.0
    messages = ['48250', '48251', '48252', '48253', '365096']
    nosy_count = 3.0
    nosy_names = ['loewis', 'yorick', 'Justin.Lebar']
    pr_nums = []
    priority = 'normal'
    resolution = 'out of date'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue1187312'
    versions = ['Python 2.4']

    @yorick
    Copy link
    Mannequin Author

    yorick mannequin commented Apr 21, 2005

    The subprocess module automatically reaps child
    processes. It maintains a list of Popen instances, and
    each time a new Popen is created, the list is traversed
    and a non-polling wait is done for each instance.

    I discussed this with the author, Peter Åstrand, and
    this behaviour was inherited from the older popen2
    code, and is intended to avoid a limitless accretion of
    zombies when the user does not take care to wait for
    the processes.

    However, the auto-reaping interacts badly with
    os.wait()/waitpid() since the user is not aware that
    the module is reaping children behind her back. In
    particular, os.wait(), which is very useful when a
    blocking wait for many children is desired, may not
    work at all, which caused me to look at the problem in
    the first case.

    The solution is to allow the user to create Popen
    instances that are not auto-reaped. The interface is
    otherwise unchanged, and existing code will see no
    change in behaviour.

    This patch does three things:

    • Adds an autoreap parameter to the Popen constructor,
      defaulting to True (the previous behaviour)
    • Documents the auto-reaper and its interaction with
      os.wait()/waitpid(), which was previously missing
    • Changes the list of instances to a set, to avoid O(N)
      element removal.

    For completeness, here is a test case:

    import os, subprocess, time
    p = subprocess.Popen(["/bin/true"]).pid
    time.sleep(1)
    subprocess.call(["/bin/false"])
    (pid, status) = os.wait()
    print "got", pid, "expected", p

    The above code will throw an exception. With the patch,
    it will work as expected if autoreap=False is added to
    the Popen call.

    @yorick yorick mannequin closed this as completed Apr 21, 2005
    @yorick yorick mannequin added the stdlib Python modules in the Lib dir label Apr 21, 2005
    @yorick yorick mannequin closed this as completed Apr 21, 2005
    @yorick yorick mannequin added the stdlib Python modules in the Lib dir label Apr 21, 2005
    @yorick
    Copy link
    Mannequin Author

    yorick mannequin commented Apr 21, 2005

    Logged In: YES
    user_id=432579

    and a non-polling wait is done for each instance.

    Sorry, this should be "non-blocking wait".

    @yorick
    Copy link
    Mannequin Author

    yorick mannequin commented Apr 22, 2005

    Logged In: YES
    user_id=432579

    Revised patch, using a dict instead of a set (for
    compatibility with python 2.2, following PEP-291), and
    rename autoreap parameter to "autowait", after discussion
    with Peter Åstrand.

    @loewis
    Copy link
    Mannequin

    loewis mannequin commented Apr 10, 2006

    Logged In: YES
    user_id=21627

    This has been fixed in the subversion trunk in a different
    way: processes are added to _active only inside __del__. So
    as long as the application keeps a reference to the
    subprocess object, it can wait for it; auto-reaping only
    starts when the last reference was dropped. If you still see
    a problem in that approach, please submit a new patch
    (relative to svn trunk, preferably).

    Marking this one as outdated.

    @JustinLebar
    Copy link
    Mannequin

    JustinLebar mannequin commented Mar 26, 2020

    15 years later, it seems asyncio subprocesses may have the same issue. :)

    https://bugs.python.org/issue40078

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 9, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    0 participants