[llvm] [Support] Prevent leaking unique lock files (PR #130984)

via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 12 08:54:40 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Jan Svoboda (jansvoboda11)

<details>
<summary>Changes</summary>

Prior to this PR, failing to get the host ID would leave the unique lock file on the file system. This is now fixed by constructing `RemoveUniqueLockFileOnSignal` earlier. This PR also removes one call to `sys::fs::remove()` that is now redundant and another that was redundant even before this patch.

---
Full diff: https://github.com/llvm/llvm-project/pull/130984.diff


1 Files Affected:

- (modified) llvm/lib/Support/LockFileManager.cpp (+5-9) 


``````````diff
diff --git a/llvm/lib/Support/LockFileManager.cpp b/llvm/lib/Support/LockFileManager.cpp
index 7cf9db379974f..430dc1fa25fef 100644
--- a/llvm/lib/Support/LockFileManager.cpp
+++ b/llvm/lib/Support/LockFileManager.cpp
@@ -189,6 +189,10 @@ Expected<bool> LockFileManager::tryLock() {
     return createStringError(EC, "failed to create unique file " +
                                      UniqueLockFileName);
 
+  // Clean up the unique file on signal or scope exit. This also releases the
+  // lock if it's held since the .lock symlink will point to a nonexistent file.
+  RemoveUniqueLockFileOnSignal RemoveUniqueFile(UniqueLockFileName);
+
   // Write our process ID to our unique lock file.
   {
     SmallString<256> HostID;
@@ -200,21 +204,15 @@ Expected<bool> LockFileManager::tryLock() {
     Out.close();
 
     if (Out.has_error()) {
-      // We failed to write out PID, so report the error, remove the
-      // unique lock file, and fail.
+      // We failed to write out PID, so report the error and fail.
       Error Err = createStringError(Out.error(),
                                     "failed to write to " + UniqueLockFileName);
-      sys::fs::remove(UniqueLockFileName);
       // Don't call report_fatal_error.
       Out.clear_error();
       return std::move(Err);
     }
   }
 
-  // Clean up the unique file on signal, which also releases the lock if it is
-  // held since the .lock symlink will point to a nonexistent file.
-  RemoveUniqueLockFileOnSignal RemoveUniqueFile(UniqueLockFileName);
-
   while (true) {
     // Create a link from the lock file name. If this succeeds, we're done.
     std::error_code EC =
@@ -232,8 +230,6 @@ Expected<bool> LockFileManager::tryLock() {
     // Someone else managed to create the lock file first. Read the process ID
     // from the lock file.
     if (auto LockFileOwner = readLockFile(LockFileName)) {
-      // Wipe out our unique lock file (it's useless now)
-      sys::fs::remove(UniqueLockFileName);
       Owner = std::move(*LockFileOwner);
       return false;
     }

``````````

</details>


https://github.com/llvm/llvm-project/pull/130984


More information about the llvm-commits mailing list