mirror of
https://github.com/tmux-plugins/tmux-continuum.git
synced 2024-11-14 06:08:47 +00:00
Merge pull request #61 from v9v/fix-autosave-race
Fix race condition in autosave
This commit is contained in:
commit
9f3cbc7e12
@ -34,8 +34,26 @@ fetch_and_run_tmux_resurrect_save_script() {
|
||||
fi
|
||||
}
|
||||
|
||||
acquire_lock() {
|
||||
# Sometimes tmux starts multiple saves in parallel. We want only one
|
||||
# save to be running, otherwise we can get corrupted saved state.
|
||||
local lockdir_prefix="/tmp/tmux-continuum-$(current_tmux_server_pid)-lock-"
|
||||
# The following implements a lock that auto-expires after 100...200s.
|
||||
local lock_generation=$((`date +%s` / 100))
|
||||
local lockdir1="${lockdir_prefix}${lock_generation}"
|
||||
local lockdir2="${lockdir_prefix}$(($lock_generation + 1))"
|
||||
if mkdir "$lockdir1"; then
|
||||
trap "rmdir "$lockdir1"" EXIT
|
||||
if mkdir "$lockdir2"; then
|
||||
trap "rmdir "$lockdir1" "$lockdir2"" EXIT
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
return 1 # Someone else has the lock.
|
||||
}
|
||||
|
||||
main() {
|
||||
if supported_tmux_version_ok && auto_save_not_disabled && enough_time_since_last_run_passed; then
|
||||
if supported_tmux_version_ok && auto_save_not_disabled && enough_time_since_last_run_passed && acquire_lock; then
|
||||
fetch_and_run_tmux_resurrect_save_script
|
||||
fi
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user