summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoel Rosdahl <joel@rosdahl.net>2023-01-05 11:07:06 +0100
committerJoel Rosdahl <joel@rosdahl.net>2023-01-15 21:33:41 +0100
commit00b9a854bc20f5c27937682d266bf0b23142163a (patch)
tree2f9a31714776226bd2e4f6aded152d201ade5d8c /src
parent26f8e94b86df915a33152f35697b5446442efb1b (diff)
downloadccache-00b9a854bc20f5c27937682d266bf0b23142163a.tar.gz
enhance: Make it possible for LockFile::try_acquire to break the lock
If a long-lived lock is stale and has no alive file, LockFile::try_acquire will never succeed to acquire the lock. Fix this by creating the alive file for all lock types and making LockFile::try_acquire exit when lock activity is seen instead of immediately after failing to acquire the lock. Another advantage is that a stale lock can now always be broken right away if the alive file exists.
Diffstat (limited to 'src')
-rw-r--r--src/util/LockFile.cpp23
1 files changed, 12 insertions, 11 deletions
diff --git a/src/util/LockFile.cpp b/src/util/LockFile.cpp
index 2e5bef32..a9deab30 100644
--- a/src/util/LockFile.cpp
+++ b/src/util/LockFile.cpp
@@ -1,4 +1,4 @@
-// Copyright (C) 2020-2022 Joel Rosdahl and other contributors
+// Copyright (C) 2020-2023 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
@@ -153,8 +153,8 @@ LockFile::release()
#ifndef _WIN32
if (m_lock_manager) {
m_lock_manager->deregister_alive_file(m_alive_file);
- Util::unlink_tmp(m_alive_file);
}
+ Util::unlink_tmp(m_alive_file);
Util::unlink_tmp(m_lock_file);
#else
CloseHandle(m_handle);
@@ -191,11 +191,12 @@ LockFile::acquire(const bool blocking)
if (acquired()) {
LOG("Acquired {}", m_lock_file);
#ifndef _WIN32
+ LOG("Creating {}", m_alive_file);
+ const auto result = util::write_file(m_alive_file, "");
+ if (!result) {
+ LOG("Failed to write {}: {}", m_alive_file, result.error());
+ }
if (m_lock_manager) {
- const auto result = util::write_file(m_alive_file, "");
- if (!result) {
- LOG("Failed to write {}: {}", m_alive_file, result.error());
- }
m_lock_manager->register_alive_file(m_alive_file);
}
#endif
@@ -281,8 +282,11 @@ LockFile::do_acquire(const bool blocking)
}
const auto last_lock_update = get_last_lock_update();
- if (last_lock_update) {
- last_seen_activity = std::max(last_seen_activity, *last_lock_update);
+ if (last_lock_update && *last_lock_update > last_seen_activity) {
+ if (!blocking) {
+ return false;
+ }
+ last_seen_activity = *last_lock_update;
}
const util::Duration inactive_duration =
@@ -293,9 +297,6 @@ LockFile::do_acquire(const bool blocking)
m_lock_file,
inactive_duration.sec(),
inactive_duration.nsec() / 1'000'000);
- if (!blocking) {
- return false;
- }
} else if (content == initial_content) {
// The lock seems to be stale -- break it and try again.
LOG("Breaking {} since it has been inactive for {}.{:03} seconds",