- Issue #12044: Fixed subprocess.Popen when used as a context manager to

wait for the process to end when exiting the context to avoid unintentionally
  leaving zombie processes around.
This commit is contained in:
Gregory P. Smith 2011-05-11 21:42:08 -07:00
parent 4e19e11958
commit 6b65745430
4 changed files with 10 additions and 3 deletions

View File

@ -219,8 +219,8 @@ This module defines one class called :class:`Popen`:
*creationflags*, if given, can be :data:`CREATE_NEW_CONSOLE` or *creationflags*, if given, can be :data:`CREATE_NEW_CONSOLE` or
:data:`CREATE_NEW_PROCESS_GROUP`. (Windows only) :data:`CREATE_NEW_PROCESS_GROUP`. (Windows only)
Popen objects are supported as context managers via the :keyword:`with` statement, Popen objects are supported as context managers via the :keyword:`with` statement:
closing any open file descriptors on exit. on exit, standard file descriptors are closed, and the process is waited for.
:: ::
with Popen(["ifconfig"], stdout=PIPE) as proc: with Popen(["ifconfig"], stdout=PIPE) as proc:

View File

@ -796,6 +796,8 @@ class Popen(object):
self.stderr.close() self.stderr.close()
if self.stdin: if self.stdin:
self.stdin.close() self.stdin.close()
# Wait for the process to terminate, to avoid zombies.
self.wait()
def __del__(self, _maxsize=sys.maxsize, _active=_active): def __del__(self, _maxsize=sys.maxsize, _active=_active):
if not self._child_created: if not self._child_created:

View File

@ -1590,7 +1590,8 @@ class ContextManagerTests(ProcessTestCase):
def test_returncode(self): def test_returncode(self):
with subprocess.Popen([sys.executable, "-c", with subprocess.Popen([sys.executable, "-c",
"import sys; sys.exit(100)"]) as proc: "import sys; sys.exit(100)"]) as proc:
proc.wait() pass
# __exit__ calls wait(), so the returncode should be set
self.assertEqual(proc.returncode, 100) self.assertEqual(proc.returncode, 100)
def test_communicate_stdin(self): def test_communicate_stdin(self):

View File

@ -10,6 +10,10 @@ What's New in Python 3.3 Alpha 1?
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #12044: Fixed subprocess.Popen when used as a context manager to
wait for the process to end when exiting the context to avoid unintentionally
leaving zombie processes around.
- Issue #1195: Fix input() if it is interrupted by CTRL+d and then CTRL+c, - Issue #1195: Fix input() if it is interrupted by CTRL+d and then CTRL+c,
clear the end-of-file indicator after CTRL+d. clear the end-of-file indicator after CTRL+d.