Skip to content

[Support] Prevent leaking unique lock files #130984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 13, 2025

Conversation

jansvoboda11
Copy link
Contributor

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.

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.
@llvmbot
Copy link
Member

llvmbot commented Mar 12, 2025

@llvm/pr-subscribers-llvm-support

Author: Jan Svoboda (jansvoboda11)

Changes

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 --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;
     }

Copy link
Contributor

@Bigcheese Bigcheese left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, but a note on one of the comments.

@jansvoboda11 jansvoboda11 merged commit d0c8695 into llvm:main Mar 13, 2025
6 of 10 checks passed
@jansvoboda11 jansvoboda11 deleted the lock-file-manager-remove-unique branch March 13, 2025 23:15
frederik-h pushed a commit to frederik-h/llvm-project that referenced this pull request Mar 18, 2025
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.
jansvoboda11 added a commit to swiftlang/llvm-project that referenced this pull request Mar 18, 2025
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.

(cherry picked from commit d0c8695)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants