Fix a race condition in the lock-file manager: once the lock file is
authorDouglas Gregor <dgregor@apple.com>
Thu, 10 Jan 2013 02:01:35 +0000 (02:01 +0000)
committerDouglas Gregor <dgregor@apple.com>
Thu, 10 Jan 2013 02:01:35 +0000 (02:01 +0000)
gone, check for the actual file we care about.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@172033 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/LockFileManager.h
lib/Support/LockFileManager.cpp

index 8c4a760291b8125d034e7d934d9cf89d3dd72c81..9df8675ef0afd670113981ca09c5767ce4601a8b 100644 (file)
@@ -41,6 +41,7 @@ public:
   };
 
 private:
+  SmallString<128> FileName;
   SmallString<128> LockFileName;
   SmallString<128> UniqueLockFileName;
 
index 075d8a5a669c48c0c977626f85b573df2aa16211..31eec751b7b613b4e8072615976dca558077afae 100644 (file)
@@ -64,6 +64,7 @@ bool LockFileManager::processStillExecuting(StringRef Hostname, int PID) {
 
 LockFileManager::LockFileManager(StringRef FileName)
 {
+  this->FileName = FileName;
   LockFileName = FileName;
   LockFileName += ".lock";
 
@@ -175,6 +176,7 @@ void LockFileManager::waitForUnlock() {
 #endif
   // Don't wait more than an hour for the file to appear.
   const unsigned MaxSeconds = 3600;
+  bool LockFileGone = false;
   do {
     // Sleep for the designated interval, to allow the owning process time to
     // finish up and remove the lock file.
@@ -185,10 +187,18 @@ void LockFileManager::waitForUnlock() {
 #else
     nanosleep(&Interval, NULL);
 #endif
-    // If the file no longer exists, we're done.
+    // If the lock file no longer exists, wait for the actual file.
     bool Exists = false;
-    if (!sys::fs::exists(LockFileName.str(), Exists) && !Exists)
-      return;
+    if (!LockFileGone) {
+      if (!sys::fs::exists(LockFileName.str(), Exists) && !Exists) {
+        LockFileGone = true;
+        Exists = false;
+      }
+    }
+    if (LockFileGone) {
+      if (!sys::fs::exists(FileName.str(), Exists) && Exists)
+        return;
+    }
 
     if (!processStillExecuting((*Owner).first, (*Owner).second))
       return;