From 39174046684efe60b3ed656d99291b835b07f60a Mon Sep 17 00:00:00 2001 From: v9v <36771847+v9v@users.noreply.github.com> Date: Tue, 9 Jul 2019 10:46:56 +0200 Subject: [PATCH] Fix race condition in saving lock Two consecutive calls to "date +%s" can return different values. Call "date" only once and reuse the result. --- scripts/continuum_save.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/scripts/continuum_save.sh b/scripts/continuum_save.sh index a952cf1..77dfe7f 100755 --- a/scripts/continuum_save.sh +++ b/scripts/continuum_save.sh @@ -37,10 +37,11 @@ fetch_and_run_tmux_resurrect_save_script() { main() { # Sometimes tmux starts multiple saves in parallel. We want only one # 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 lockdir1="${lockdir_prefix}$[ `date +%s` / 100 ]" - local lockdir2="${lockdir_prefix}$[ `date +%s` / 100 + 1]" + # 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