From 90374f510223f559a70dcd762e223762d4f2ad85 Mon Sep 17 00:00:00 2001 From: Tim Golden Date: Fri, 6 Aug 2010 13:14:33 +0000 Subject: [PATCH] Issue #3210: Ensure stdio handles are closed if CreateProcess fails --- Lib/test/test_subprocess.py | 20 ++++++++++++++++++++ PC/_subprocess.c | 19 +++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index adfb1bfb521..15ca19753f0 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -533,6 +533,26 @@ class ProcessTestCase(BaseTestCase): if c.exception.errno != 2: # ignore "no such file" raise c.exception + def test_handles_closed_on_exception(self): + # If CreateProcess exits with an error, ensure the + # duplicate output handles are released + ifhandle, ifname = mkstemp() + ofhandle, ofname = mkstemp() + efhandle, efname = mkstemp() + try: + subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle, + stderr=efhandle) + except OSError: + os.close(ifhandle) + os.remove(ifname) + os.close(ofhandle) + os.remove(ofname) + os.close(efhandle) + os.remove(efname) + self.assertFalse(os.path.exists(ifname)) + self.assertFalse(os.path.exists(ofname)) + self.assertFalse(os.path.exists(efname)) + # context manager class _SuppressCoreFiles(object): diff --git a/PC/_subprocess.c b/PC/_subprocess.c index 6780382e6ea..41a24982fe3 100644 --- a/PC/_subprocess.c +++ b/PC/_subprocess.c @@ -425,6 +425,7 @@ sp_CreateProcess(PyObject* self, PyObject* args) PyObject* env_mapping; char* current_directory; PyObject* startup_info; + DWORD error; if (! PyArg_ParseTuple(args, "zzOOiiOzO:CreateProcess", &application_name, @@ -474,8 +475,22 @@ sp_CreateProcess(PyObject* self, PyObject* args) Py_XDECREF(environment); - if (! result) - return PyErr_SetFromWindowsErr(GetLastError()); + if (! result) { + error = GetLastError(); + if(si.hStdInput != INVALID_HANDLE_VALUE) { + CloseHandle(si.hStdInput); + si.hStdInput = INVALID_HANDLE_VALUE; + } + if(si.hStdOutput != INVALID_HANDLE_VALUE) { + CloseHandle(si.hStdOutput); + si.hStdOutput = INVALID_HANDLE_VALUE; + } + if(si.hStdError != INVALID_HANDLE_VALUE) { + CloseHandle(si.hStdError); + si.hStdError = INVALID_HANDLE_VALUE; + } + return PyErr_SetFromWindowsErr(error); + } return Py_BuildValue("NNii", sp_handle_new(pi.hProcess),