script_helper: kill the subprocess on error
authorVictor Stinner <victor.stinner@gmail.com>
Wed, 17 Aug 2016 10:29:58 +0000 (12:29 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Wed, 17 Aug 2016 10:29:58 +0000 (12:29 +0200)
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.

Lib/test/support/script_helper.py

index c45d010a7180bbcb5e890e4b275694fcd9724271..80889b17f3f376d875cc57d1a41d4f0c1653843a 100644 (file)
@@ -83,16 +83,16 @@ def run_python_until_end(*args, **env_vars):
         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