From 8b4a65c78454c87413ef606e2d9afdc304eea16e Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Fri, 21 Apr 2017 02:18:21 +0300 Subject: [PATCH 1/2] bpo-30121: Fix debug assert in subprocess on Windows This is caused by closing HANDLEs using os.close which is for CRT file descriptors and not for HANDLEs. --- Lib/subprocess.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 551aad342b8304..010730fdb992c1 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -727,7 +727,10 @@ def __init__(self, args, bufsize=-1, executable=None, to_close.append(self._devnull) for fd in to_close: try: - os.close(fd) + if _mswindows and isinstance(fd, Handle): + fd.Close() + else: + os.close(fd) except OSError: pass @@ -1005,6 +1008,9 @@ def _execute_child(self, args, executable, preexec_fn, close_fds, errwrite.Close() if hasattr(self, '_devnull'): os.close(self._devnull) + # Prevent a double close of these handles/fds from __init__ + # on error. + self._closed_child_pipe_fds = True # Retain the process handle, but close the thread handle self._child_created = True From 6dd5a06c0fc6c30c715e6393a1e5332dc2016b7f Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Tue, 6 Jun 2017 20:24:25 +0300 Subject: [PATCH 2/2] bpo-30121: Suppress debug assertion in test_subprocess when ran directly --- Lib/test/test_subprocess.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index 7fabe6ad765332..9fae5360dd8ed0 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -1068,10 +1068,11 @@ def _test_bufsize_equal_one(self, line, expected, universal_newlines): p.stdin.write(line) # expect that it flushes the line in text mode os.close(p.stdin.fileno()) # close it without flushing the buffer read_line = p.stdout.readline() - try: - p.stdin.close() - except OSError: - pass + with support.SuppressCrashReport(): + try: + p.stdin.close() + except OSError: + pass p.stdin = None self.assertEqual(p.returncode, 0) self.assertEqual(read_line, expected)