Add some safety to history_file_lock

Use an atomic to avoid TSAN complaints, and ensure that we don't
leave the file locked if we've decided to give up on locking.
This commit is contained in:
ridiculousfish 2017-02-06 10:00:34 -08:00
parent e3c538a991
commit 6eb1e31070

View file

@ -119,7 +119,7 @@ class time_profiler_t {
/// Lock the history file. /// Lock the history file.
/// Returns true on success, false on failure. /// Returns true on success, false on failure.
static bool history_file_lock(int fd, int lock_type) { static bool history_file_lock(int fd, int lock_type) {
static bool do_locking = true; static std::atomic<bool> do_locking(true);
if (!do_locking) return false; if (!do_locking) return false;
double start_time = timef(); double start_time = timef();
@ -127,6 +127,11 @@ static bool history_file_lock(int fd, int lock_type) {
double duration = timef() - start_time; double duration = timef() - start_time;
if (duration > 0.25) { if (duration > 0.25) {
debug(1, _(L"Locking the history file took too long (%.3f seconds)."), duration); debug(1, _(L"Locking the history file took too long (%.3f seconds)."), duration);
// we've decided to stop doing any locking behavior
// but make sure we don't leave the file locked!
if (retval == 0 && lock_type != LOCK_UN) {
flock(fd, LOCK_UN);
}
do_locking = false; do_locking = false;
return false; return false;
} }