diff -r 45bb27eae8d3 Lib/subprocess.py --- a/Lib/subprocess.py Fri Aug 21 09:40:38 2015 -0400 +++ b/Lib/subprocess.py Fri Aug 21 12:39:05 2015 -0400 @@ -427,6 +427,7 @@ import threading import msvcrt import _winapi + _global_win32_popen_lock = threading.Lock() class STARTUPINFO: dwFlags = 0 hStdInput = None @@ -909,6 +910,13 @@ # are -1 when not using PIPEs. The child objects are -1 # when not redirecting. + if _mswindows: + # If we are on Windows, acquire the global Popen lock. The lock + # ensures that only one thread at a time is creating inheritable + # handles. That way, if two threads call Popen at the same time, + # inheritable handles from thread 1 won't be leaked into thread 2. + _global_win32_popen_lock.acquire() + (p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite) = self._get_handles(stdin, stdout, stderr) @@ -973,6 +981,10 @@ pass raise + finally: + if _mswindows: + # Release the Popen lock. + _global_win32_popen_lock.release() def _translate_newlines(self, data, encoding):