]> granicus.if.org Git - python/commitdiff
- Issue #12044: Fixed subprocess.Popen when used as a context manager to
authorGregory P. Smith <greg@krypto.org>
Thu, 12 May 2011 04:42:08 +0000 (21:42 -0700)
committerGregory P. Smith <greg@krypto.org>
Thu, 12 May 2011 04:42:08 +0000 (21:42 -0700)
  wait for the process to end when exiting the context to avoid unintentionally
  leaving zombie processes around.

Doc/library/subprocess.rst
Lib/subprocess.py
Lib/test/test_subprocess.py
Misc/NEWS

index bb4884903c5626601258794a9bf30efc1d63cf72..ad5a535a7df03041561fe703876e3bedf00da159 100644 (file)
@@ -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:
index a7c68a5d6ef4f11258c2aac1cb61fe9484756a74..cd8aa6bc56c590c747ce331f45cbdefb48a6e835 100644 (file)
@@ -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:
index e8abfef82979c6550ae2bc7cede3d6647c3a2654..776e1437c4ab0e9aa57d545b8f6a3b1e9a4d744c 100644 (file)
@@ -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):
index be26c72d895600d689bf71bc352ee1722bade75a..751f1e307c164c9ddffa29009f7ccef7185892d3 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -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.