Skip to content

Commit 4c71655

Browse files
committed
Moved pipe handles in Process execute to close in function
Pipe handles were opened in the main execute function, and closed in the thread worker functions. Handles are now local (instead of member variables), and closed in the same scope as they are created. Signed-off-by: Dave Brotherstone <davegb@pobox.com>
1 parent a585348 commit 4c71655

2 files changed

Lines changed: 16 additions & 20 deletions

File tree

PythonScript/src/ProcessExecute.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ const char *ProcessExecute::STREAM_NAME_STDOUT = "OUT";
1616
const char *ProcessExecute::STREAM_NAME_STDERR = "ERR";
1717

1818
ProcessExecute::ProcessExecute()
19-
: m_hStdOutReadPipe (NULL),
20-
m_hStdOutWritePipe (NULL),
21-
m_hStdErrReadPipe (NULL),
22-
m_hStdErrWritePipe (NULL)
2319
{
2420
}
2521

@@ -56,6 +52,8 @@ long ProcessExecute::execute(const TCHAR *commandLine, boost::python::object pyS
5652
PipeReaderArgs stderrReaderArgs;
5753
// Only used if spooling, but we need to delete it later.
5854
TCHAR tmpFilename[MAX_PATH] = {0};
55+
HANDLE hStdOutReadPipe, hStdOutWritePipe;
56+
HANDLE hStdErrReadPipe, hStdErrWritePipe;
5957

6058
Py_BEGIN_ALLOW_THREADS
6159
try
@@ -73,12 +71,12 @@ long ProcessExecute::execute(const TCHAR *commandLine, boost::python::object pyS
7371
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
7472
sa.bInheritHandle = TRUE;
7573

76-
if (!::CreatePipe(&m_hStdOutReadPipe, &m_hStdOutWritePipe, &sa, DEFAULT_PIPE_SIZE))
74+
if (!::CreatePipe(&hStdOutReadPipe, &hStdOutWritePipe, &sa, DEFAULT_PIPE_SIZE))
7775
{
7876
throw process_start_exception("Error creating pipe for stdout");
7977
}
8078

81-
if (!::CreatePipe(&m_hStdErrReadPipe, &m_hStdErrWritePipe, &sa, DEFAULT_PIPE_SIZE))
79+
if (!::CreatePipe(&hStdErrReadPipe, &hStdErrWritePipe, &sa, DEFAULT_PIPE_SIZE))
8280
{
8381
throw process_start_exception("Error creating pipe for stderr");
8482
}
@@ -88,17 +86,17 @@ long ProcessExecute::execute(const TCHAR *commandLine, boost::python::object pyS
8886

8987

9088
stdoutReaderArgs.processExecute = this;
91-
stdoutReaderArgs.hPipeRead = m_hStdOutReadPipe;
92-
stdoutReaderArgs.hPipeWrite = m_hStdOutWritePipe;
89+
stdoutReaderArgs.hPipeRead = hStdOutReadPipe;
90+
stdoutReaderArgs.hPipeWrite = hStdOutWritePipe;
9391
stdoutReaderArgs.pythonFile = pyStdout;
9492
stdoutReaderArgs.stopEvent = stopEvent;
9593
stdoutReaderArgs.completedEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
9694
stdoutReaderArgs.streamName = STREAM_NAME_STDOUT;
9795
stdoutReaderArgs.toFile = false;
9896

9997
stderrReaderArgs.processExecute = this;
100-
stderrReaderArgs.hPipeRead = m_hStdErrReadPipe;
101-
stderrReaderArgs.hPipeWrite = m_hStdErrWritePipe;
98+
stderrReaderArgs.hPipeRead = hStdErrReadPipe;
99+
stderrReaderArgs.hPipeWrite = hStdErrWritePipe;
102100
stderrReaderArgs.pythonFile = pyStderr;
103101
stderrReaderArgs.stopEvent = stopEvent;
104102
stderrReaderArgs.completedEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
@@ -167,17 +165,17 @@ long ProcessExecute::execute(const TCHAR *commandLine, boost::python::object pyS
167165
STARTUPINFO si;
168166

169167

170-
::SetHandleInformation(m_hStdOutReadPipe, HANDLE_FLAG_INHERIT, 0);
171-
::SetHandleInformation(m_hStdErrReadPipe, HANDLE_FLAG_INHERIT, 0);
168+
::SetHandleInformation(hStdOutReadPipe, HANDLE_FLAG_INHERIT, 0);
169+
::SetHandleInformation(hStdErrReadPipe, HANDLE_FLAG_INHERIT, 0);
172170

173171
// initialize STARTUPINFO struct
174172
::ZeroMemory( &si, sizeof(STARTUPINFO) );
175173
si.cb = sizeof(STARTUPINFO);
176174
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
177175
si.wShowWindow = SW_HIDE;
178176
si.hStdInput = NULL;
179-
si.hStdOutput = m_hStdOutWritePipe;
180-
si.hStdError = m_hStdErrWritePipe;
177+
si.hStdOutput = hStdOutWritePipe;
178+
si.hStdError = hStdErrWritePipe;
181179

182180
::ZeroMemory( &pi, sizeof(PROCESS_INFORMATION) );
183181

@@ -224,6 +222,10 @@ long ProcessExecute::execute(const TCHAR *commandLine, boost::python::object pyS
224222
CloseHandle(hStdoutThread);
225223
CloseHandle(hStderrThread);
226224
CloseHandle(stopEvent);
225+
CloseHandle(hStdOutReadPipe);
226+
CloseHandle(hStdOutWritePipe);
227+
CloseHandle(hStdErrReadPipe);
228+
CloseHandle(hStdErrWritePipe);
227229

228230
if (processStartSuccess)
229231
{
@@ -317,8 +319,6 @@ DWORD WINAPI ProcessExecute::pipeReader(void *args)
317319
}
318320
}
319321

320-
CloseHandle(pipeReaderArgs->hPipeRead);
321-
CloseHandle(pipeReaderArgs->hPipeWrite);
322322
SetEvent(pipeReaderArgs->completedEvent);
323323

324324
return 0;

PythonScript/src/ProcessExecute.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ class ProcessExecute
2424
void writeToFile(PipeReaderArgs *pipeReaderArgs, int bytesRead, char *buffer);
2525
void spoolFile(std::fstream* file, boost::python::object pyStdout, boost::python::object pyStderr);
2626

27-
HANDLE m_hStdOutReadPipe;
28-
HANDLE m_hStdOutWritePipe;
29-
HANDLE m_hStdErrReadPipe;
30-
HANDLE m_hStdErrWritePipe;
3127
};
3228

3329
struct PipeReaderArgs

0 commit comments

Comments
 (0)