Skip to content

Commit 9f3cbc7

Browse files
authored
Merge pull request #61 from v9v/fix-autosave-race
Fix race condition in autosave
2 parents 100498d + ff63f86 commit 9f3cbc7

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

scripts/continuum_save.sh

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,26 @@ fetch_and_run_tmux_resurrect_save_script() {
3434
fi
3535
}
3636

37+
acquire_lock() {
38+
# Sometimes tmux starts multiple saves in parallel. We want only one
39+
# save to be running, otherwise we can get corrupted saved state.
40+
local lockdir_prefix="/tmp/tmux-continuum-$(current_tmux_server_pid)-lock-"
41+
# The following implements a lock that auto-expires after 100...200s.
42+
local lock_generation=$((`date +%s` / 100))
43+
local lockdir1="${lockdir_prefix}${lock_generation}"
44+
local lockdir2="${lockdir_prefix}$(($lock_generation + 1))"
45+
if mkdir "$lockdir1"; then
46+
trap "rmdir "$lockdir1"" EXIT
47+
if mkdir "$lockdir2"; then
48+
trap "rmdir "$lockdir1" "$lockdir2"" EXIT
49+
return 0
50+
fi
51+
fi
52+
return 1 # Someone else has the lock.
53+
}
54+
3755
main() {
38-
if supported_tmux_version_ok && auto_save_not_disabled && enough_time_since_last_run_passed; then
56+
if supported_tmux_version_ok && auto_save_not_disabled && enough_time_since_last_run_passed && acquire_lock; then
3957
fetch_and_run_tmux_resurrect_save_script
4058
fi
4159
}

0 commit comments

Comments
 (0)