Skip to content

Commit 3b51c45

Browse files
jansvoboda11frederik-h
authored andcommitted
[Support] Prevent leaking unique lock files (llvm#130984)
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.
1 parent 7477aa4 commit 3b51c45

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

llvm/lib/Support/LockFileManager.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ Expected<bool> LockFileManager::tryLock() {
189189
return createStringError(EC, "failed to create unique file " +
190190
UniqueLockFileName);
191191

192+
// Clean up the unique file on signal or scope exit.
193+
RemoveUniqueLockFileOnSignal RemoveUniqueFile(UniqueLockFileName);
194+
192195
// Write our process ID to our unique lock file.
193196
{
194197
SmallString<256> HostID;
@@ -200,21 +203,15 @@ Expected<bool> LockFileManager::tryLock() {
200203
Out.close();
201204

202205
if (Out.has_error()) {
203-
// We failed to write out PID, so report the error, remove the
204-
// unique lock file, and fail.
206+
// We failed to write out PID, so report the error and fail.
205207
Error Err = createStringError(Out.error(),
206208
"failed to write to " + UniqueLockFileName);
207-
sys::fs::remove(UniqueLockFileName);
208209
// Don't call report_fatal_error.
209210
Out.clear_error();
210211
return std::move(Err);
211212
}
212213
}
213214

214-
// Clean up the unique file on signal, which also releases the lock if it is
215-
// held since the .lock symlink will point to a nonexistent file.
216-
RemoveUniqueLockFileOnSignal RemoveUniqueFile(UniqueLockFileName);
217-
218215
while (true) {
219216
// Create a link from the lock file name. If this succeeds, we're done.
220217
std::error_code EC =
@@ -232,8 +229,6 @@ Expected<bool> LockFileManager::tryLock() {
232229
// Someone else managed to create the lock file first. Read the process ID
233230
// from the lock file.
234231
if (auto LockFileOwner = readLockFile(LockFileName)) {
235-
// Wipe out our unique lock file (it's useless now)
236-
sys::fs::remove(UniqueLockFileName);
237232
Owner = std::move(*LockFileOwner);
238233
return false;
239234
}

0 commit comments

Comments
 (0)