Issue #3210: Ensure stdio handles are closed if CreateProcess fails
diff --git a/PC/_subprocess.c b/PC/_subprocess.c
index 5132a5e..2a3207b 100644
--- a/PC/_subprocess.c
+++ b/PC/_subprocess.c
@@ -429,6 +429,7 @@
     PyObject* env_mapping;
     Py_UNICODE* current_directory;
     PyObject* startup_info;
+    DWORD error;
 
     if (! PyArg_ParseTuple(args, "ZZOOiiOZO:CreateProcess",
                            &application_name,
@@ -478,8 +479,22 @@
 
     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),