[Patches] [ python-Patches-1187312 ] subprocess: optional auto-reaping fixing os.wait() lossage

SourceForge.net noreply at sourceforge.net
Fri Apr 22 18:20:41 CEST 2005


Patches item #1187312, was opened at 2005-04-21 13:40
Message generated for change (Comment added) made by yorick
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1187312&group_id=5470

Category: Library (Lib)
Group: Python 2.4
Status: Open
Resolution: None
Priority: 5
Submitted By: Mattias Engdegård (yorick)
Assigned to: Nobody/Anonymous (nobody)
Summary: subprocess: optional auto-reaping fixing os.wait() lossage

Initial Comment:
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.


----------------------------------------------------------------------

>Comment By: Mattias Engdegård (yorick)
Date: 2005-04-22 18:20

Message:
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.


----------------------------------------------------------------------

Comment By: Mattias Engdegård (yorick)
Date: 2005-04-21 13:48

Message:
Logged In: YES 
user_id=432579

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

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


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1187312&group_id=5470


More information about the Patches mailing list