Skip to content

Commit f05b2bd

Browse files
compnerdkeith
authored andcommitted
common: minor cleanup for Windows RunSubProcess
Close the main thread handle earlier as we do not need it. This avoids the duplicated cleanup on the error path. Move variable declaration closer to initial use.
1 parent ece44d7 commit f05b2bd

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

tools/common/process.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ std::string GetCommandLine(const std::vector<std::string> &arguments) {
178178

179179
int RunSubProcess(const std::vector<std::string> &args,
180180
std::ostream *stderr_stream, bool stdout_to_stderr) {
181-
PROCESS_INFORMATION piProcess = {0};
182181
std::error_code ec;
183182
std::unique_ptr<WindowsIORedirector> redirector =
184183
WindowsIORedirector::Create(stdout_to_stderr, ec);
@@ -188,6 +187,7 @@ int RunSubProcess(const std::vector<std::string> &args,
188187
return 254;
189188
}
190189

190+
PROCESS_INFORMATION piProcess = {0};
191191
if (!CreateProcessA(NULL, GetCommandLine(args).data(), nullptr, nullptr, TRUE,
192192
0, nullptr, nullptr, &redirector->siStartInfo,
193193
&piProcess)) {
@@ -196,13 +196,15 @@ int RunSubProcess(const std::vector<std::string> &args,
196196
<< ")\n";
197197
return dwLastError;
198198
}
199+
200+
CloseHandle(piProcess.hThread);
201+
199202
redirector->ConsumeAllSubprocessOutput(stderr_stream);
200203

201204
if (WaitForSingleObject(piProcess.hProcess, INFINITE) == WAIT_FAILED) {
202205
DWORD dwLastError = GetLastError();
203206
(*stderr_stream) << "wait for process failure (error " << dwLastError
204207
<< ")\n";
205-
CloseHandle(piProcess.hThread);
206208
CloseHandle(piProcess.hProcess);
207209
return dwLastError;
208210
}
@@ -212,12 +214,10 @@ int RunSubProcess(const std::vector<std::string> &args,
212214
DWORD dwLastError = GetLastError();
213215
(*stderr_stream) << "unable to get exit code (error " << dwLastError
214216
<< ")\n";
215-
CloseHandle(piProcess.hThread);
216217
CloseHandle(piProcess.hProcess);
217218
return dwLastError;
218219
}
219220

220-
CloseHandle(piProcess.hThread);
221221
CloseHandle(piProcess.hProcess);
222222
return dwExitCode;
223223
}

0 commit comments

Comments
 (0)