Issue #3210: Ensure stdio handles are closed if CreateProcess fails

This commit is contained in:
Tim Golden 2010-08-06 13:14:33 +00:00
parent 3ad323ecaf
commit 90374f5102
2 changed files with 37 additions and 2 deletions

View File

@ -533,6 +533,26 @@ class ProcessTestCase(BaseTestCase):
if c.exception.errno != 2: # ignore "no such file" if c.exception.errno != 2: # ignore "no such file"
raise c.exception 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 # context manager
class _SuppressCoreFiles(object): class _SuppressCoreFiles(object):

View File

@ -425,6 +425,7 @@ sp_CreateProcess(PyObject* self, PyObject* args)
PyObject* env_mapping; PyObject* env_mapping;
char* current_directory; char* current_directory;
PyObject* startup_info; PyObject* startup_info;
DWORD error;
if (! PyArg_ParseTuple(args, "zzOOiiOzO:CreateProcess", if (! PyArg_ParseTuple(args, "zzOOiiOzO:CreateProcess",
&application_name, &application_name,
@ -474,8 +475,22 @@ sp_CreateProcess(PyObject* self, PyObject* args)
Py_XDECREF(environment); Py_XDECREF(environment);
if (! result) if (! result) {
return PyErr_SetFromWindowsErr(GetLastError()); 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", return Py_BuildValue("NNii",
sp_handle_new(pi.hProcess), sp_handle_new(pi.hProcess),