[Python-checkins] cpython (3.5): script_helper: kill the subprocess on error

victor.stinner python-checkins at python.org
Wed Aug 17 06:46:51 EDT 2016


https://hg.python.org/cpython/rev/242c89ab09fe
changeset:   102722:242c89ab09fe
branch:      3.5
parent:      102715:d1b93ce7dad8
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Wed Aug 17 12:29:58 2016 +0200
summary:
  script_helper: kill the subprocess on error

If Popen.communicate() raises an exception, kill the child process to not leave
a running child process in background and maybe create a zombi process.

This change fixes a ResourceWarning in Python 3.6 when unit tests are
interrupted by CTRL+c.

files:
  Lib/test/support/script_helper.py |  16 ++++++++--------
  1 files changed, 8 insertions(+), 8 deletions(-)


diff --git a/Lib/test/support/script_helper.py b/Lib/test/support/script_helper.py
--- a/Lib/test/support/script_helper.py
+++ b/Lib/test/support/script_helper.py
@@ -83,16 +83,16 @@
         env = {}
     env.update(env_vars)
     cmd_line.extend(args)
-    p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
+    proc = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                          env=env)
-    try:
-        out, err = p.communicate()
-    finally:
-        subprocess._cleanup()
-        p.stdout.close()
-        p.stderr.close()
-    rc = p.returncode
+    with proc:
+        try:
+            out, err = proc.communicate()
+        finally:
+            proc.kill()
+            subprocess._cleanup()
+    rc = proc.returncode
     err = strip_python_stderr(err)
     return _PythonRunResult(rc, out, err), cmd_line
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list