diff options
author | Gregory P. Smith <greg@krypto.org> | 2011-05-12 04:42:08 (GMT) |
---|---|---|
committer | Gregory P. Smith <greg@krypto.org> | 2011-05-12 04:42:08 (GMT) |
commit | 6b65745430245937c7ee088cffa22a2d419396e2 (patch) | |
tree | 975201beeeb96eb54b9c8fcc47468ea33ddb3db8 | |
parent | 4e19e1195817891fdc4ce4c3f0eddde845bf3214 (diff) | |
download | cpython-6b65745430245937c7ee088cffa22a2d419396e2.zip cpython-6b65745430245937c7ee088cffa22a2d419396e2.tar.gz cpython-6b65745430245937c7ee088cffa22a2d419396e2.tar.bz2 |
- 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.
-rw-r--r-- | Doc/library/subprocess.rst | 4 | ||||
-rw-r--r-- | Lib/subprocess.py | 2 | ||||
-rw-r--r-- | Lib/test/test_subprocess.py | 3 | ||||
-rw-r--r-- | Misc/NEWS | 4 |
4 files changed, 10 insertions, 3 deletions
diff --git a/Doc/library/subprocess.rst b/Doc/library/subprocess.rst index bb48849..ad5a535 100644 --- a/Doc/library/subprocess.rst +++ b/Doc/library/subprocess.rst @@ -219,8 +219,8 @@ This module defines one class called :class:`Popen`: *creationflags*, if given, can be :data:`CREATE_NEW_CONSOLE` or :data:`CREATE_NEW_PROCESS_GROUP`. (Windows only) - Popen objects are supported as context managers via the :keyword:`with` statement, - closing any open file descriptors on exit. + Popen objects are supported as context managers via the :keyword:`with` statement: + on exit, standard file descriptors are closed, and the process is waited for. :: with Popen(["ifconfig"], stdout=PIPE) as proc: diff --git a/Lib/subprocess.py b/Lib/subprocess.py index a7c68a5..cd8aa6b 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -796,6 +796,8 @@ class Popen(object): self.stderr.close() if self.stdin: self.stdin.close() + # Wait for the process to terminate, to avoid zombies. + self.wait() def __del__(self, _maxsize=sys.maxsize, _active=_active): if not self._child_created: diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index e8abfef..776e143 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -1590,7 +1590,8 @@ class ContextManagerTests(ProcessTestCase): def test_returncode(self): with subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(100)"]) as proc: - proc.wait() + pass + # __exit__ calls wait(), so the returncode should be set self.assertEqual(proc.returncode, 100) def test_communicate_stdin(self): @@ -10,6 +10,10 @@ What's New in Python 3.3 Alpha 1? 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, clear the end-of-file indicator after CTRL+d. |