mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-26 12:53:13 +00:00
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:
parent
e3c538a991
commit
6eb1e31070
1 changed files with 6 additions and 1 deletions
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue