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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions llvm/lib/Support/LockFileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ Expected<bool> LockFileManager::tryLock() {
return createStringError(EC, "failed to create unique file " +
UniqueLockFileName);

// Clean up the unique file on signal or scope exit.
RemoveUniqueLockFileOnSignal RemoveUniqueFile(UniqueLockFileName);

// Write our process ID to our unique lock file.
{
SmallString<256> HostID;
Expand All @@ -200,21 +203,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 =
Expand All @@ -232,8 +229,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;
}
Expand Down
Loading