Skip to content

Commit 7e6977a

Browse files
committed
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.
1 parent 6423429 commit 7e6977a

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

Lib/test/support/script_helper.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,16 @@ def run_python_until_end(*args, **env_vars):
8383
env = {}
8484
env.update(env_vars)
8585
cmd_line.extend(args)
86-
p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
86+
proc = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
8787
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
8888
env=env)
89-
try:
90-
out, err = p.communicate()
91-
finally:
92-
subprocess._cleanup()
93-
p.stdout.close()
94-
p.stderr.close()
95-
rc = p.returncode
89+
with proc:
90+
try:
91+
out, err = proc.communicate()
92+
finally:
93+
proc.kill()
94+
subprocess._cleanup()
95+
rc = proc.returncode
9696
err = strip_python_stderr(err)
9797
return _PythonRunResult(rc, out, err), cmd_line
9898

0 commit comments

Comments
 (0)