Fix race condition in saving lock

Two consecutive calls to "date +%s" can return different values. Call "date" only once and reuse the result.
This commit is contained in:
v9v 2019-07-09 10:46:56 +02:00 committed by GitHub
parent 800488ca6f
commit 3917404668
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -37,10 +37,11 @@ fetch_and_run_tmux_resurrect_save_script() {
main() { main() {
# Sometimes tmux starts multiple saves in parallel. We want only one # Sometimes tmux starts multiple saves in parallel. We want only one
# save to be running, otherwise we can get corrupted saved state. # save to be running, otherwise we can get corrupted saved state.
# The following implements a lock that auto-expires after 100...200s.
local lockdir_prefix="/tmp/tmux-continuum-$(current_tmux_server_pid)-lock-" local lockdir_prefix="/tmp/tmux-continuum-$(current_tmux_server_pid)-lock-"
local lockdir1="${lockdir_prefix}$[ `date +%s` / 100 ]" # The following implements a lock that auto-expires after 100...200s.
local lockdir2="${lockdir_prefix}$[ `date +%s` / 100 + 1]" local lock_generation=$[ `date +%s` / 100 ]
local lockdir1="${lockdir_prefix}${lock_generation}"
local lockdir2="${lockdir_prefix}$[ $lock_generation + 1 ]"
if mkdir "$lockdir1"; then if mkdir "$lockdir1"; then
trap "rmdir "$lockdir1"" EXIT trap "rmdir "$lockdir1"" EXIT
if mkdir "$lockdir2"; then if mkdir "$lockdir2"; then