Fix race condition in autosave

Fixes the case where multiple tmux sessions call auto-save at the same time, which occasionally results in multiple instances of save_all() running in parallel and causing issues like #3 and tmux-plugins/tmux-resurrect#294.

The sequence in main() is:
1. Check enough_time_since_last_run_passed
2. Save
3. Update last_save_timestamp.

The race here is:
* process A finishes step 1 and is busy with step 2. The timestamp is not updated yet.
* process B comes to step 1, sees the old timestamp and proceeds to step 2, too.
This commit is contained in:
v9v 2019-07-04 19:35:47 +02:00
parent 90f4a00c41
commit 721890d22f

View File

@ -35,8 +35,11 @@ fetch_and_run_tmux_resurrect_save_script() {
} }
main() { main() {
(
flock -n 101 || return # The code below is not thread-safe.
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; then
fetch_and_run_tmux_resurrect_save_script fetch_and_run_tmux_resurrect_save_script
fi fi
) 101>/tmp/tmux-continuum-autosave.lockfile
} }
main main