-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[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
[Support] Prevent leaking unique lock files #130984
Conversation
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.
@llvm/pr-subscribers-llvm-support Author: Jan Svoboda (jansvoboda11) ChangesPrior 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 Full diff: https://github.com/llvm/llvm-project/pull/130984.diff 1 Files Affected:
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;
}
|
There was a problem hiding this 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.
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. (cherry picked from commit d0c8695)
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 tosys::fs::remove()
that is now redundant and another that was redundant even before this patch.