[issue12044] subprocess.Popen.__exit__ doesn't wait for process end

Charles-François Natali report at bugs.python.org
Tue May 10 18:08:27 CEST 2011


Charles-François Natali <neologix at free.fr> added the comment:

There's just one thing I'm concerned with.
People using context managers tend to expect the __exit__ method to
perform cleanup actions and release corresponding resources if
necessary, for example closing the underlying file or socket.
So my guess is that most people won't explicitly wait for the process
termination. You can already find such examples inside
test_subprocess.py, Lib/ctypes/util.py (to parse ldconfig's output),
and even in subprocess' documentation:

"""
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
"""

The problem is that until a child process is wait()ed on or its parent
process terminates (so that the child gets re-parented to init), the
child remains a zombie/defunct process. So if you have long-running
process spawning many subprocesses, you could end up having many
zombie processes, potentially filling up your process table at some
point. And this really sucks.
So I think it could be worth mentioning this somewhere in the
documentation (I mean, if we can find find such leaks inside CPython
code base, then it's definitely something that should be documented).

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12044>
_______________________________________


More information about the Python-bugs-list mailing list