Fix a race condition in getting the process exit code on Win32.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77953 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/System/Unix/Program.inc b/lib/System/Unix/Program.inc
index 3c5a54c..3a562e4 100644
--- a/lib/System/Unix/Program.inc
+++ b/lib/System/Unix/Program.inc
@@ -35,6 +35,10 @@
namespace llvm {
using namespace sys;
+Program::Program() : Pid_(0) {}
+
+Program::~Program() {}
+
// This function just uses the PATH environment variable to find the program.
Path
Program::FindProgramByName(const std::string& progName) {
diff --git a/lib/System/Win32/Program.inc b/lib/System/Win32/Program.inc
index 963946a..005f631 100644
--- a/lib/System/Win32/Program.inc
+++ b/lib/System/Win32/Program.inc
@@ -25,6 +25,16 @@
namespace llvm {
using namespace sys;
+Program::Program() : Pid_(0), Data(0) {}
+
+Program::~Program() {
+ if (Data) {
+ HANDLE hProcess = (HANDLE) Data;
+ CloseHandle(hProcess);
+ Data = 0;
+ }
+}
+
// This function just uses the PATH environment variable to find the program.
Path
Program::FindProgramByName(const std::string& progName) {
@@ -122,6 +132,12 @@
const Path** redirects,
unsigned memoryLimit,
std::string* ErrMsg) {
+ if (Data) {
+ HANDLE hProcess = (HANDLE) Data;
+ CloseHandle(Data);
+ Data = 0;
+ }
+
if (!path.canExecute()) {
if (ErrMsg)
*ErrMsg = "program not executable";
@@ -250,9 +266,9 @@
return false;
}
Pid_ = pi.dwProcessId;
-
+ Data = pi.hProcess;
+
// Make sure these get closed no matter what.
- AutoHandle hProcess(pi.hProcess);
AutoHandle hThread(pi.hThread);
// Assign the process to a job if a memory limit is defined.
@@ -286,13 +302,13 @@
int
Program::Wait(unsigned secondsToWait,
std::string* ErrMsg) {
- if (Pid_ == 0) {
+ if (Data == 0) {
MakeErrMsg(ErrMsg, "Process not started!");
return -1;
}
- AutoHandle hProcess = OpenProcess(SYNCHRONIZE, FALSE, Pid_);
-
+ HANDLE hProcess = (HANDLE) Data;
+
// Wait for the process to terminate.
DWORD millisecondsToWait = INFINITE;
if (secondsToWait > 0)