diff --git a/scripts/ledger-append.sh b/scripts/ledger-append.sh index 68d272d..9015d23 100755 --- a/scripts/ledger-append.sh +++ b/scripts/ledger-append.sh @@ -83,8 +83,11 @@ timeout_hit=0 while :; do # Sweep a stale lock (crashed/killed holder) before every attempt. if [ -d "$LOCK_DIR" ]; then - lock_mtime=$(stat -f %m "$LOCK_DIR" 2>/dev/null || stat -c %Y "$LOCK_DIR" 2>/dev/null || echo "") - if [ -n "$lock_mtime" ]; then + # GNU stat first: on Linux, `stat -f` means filesystem status and exits 0 + # with text output, which broke the arithmetic below under `set -u`. BSD + # stat rejects `-c`, so macOS falls through to `-f %m`. + lock_mtime=$(stat -c %Y "$LOCK_DIR" 2>/dev/null || stat -f %m "$LOCK_DIR" 2>/dev/null || echo "") + if [[ "$lock_mtime" =~ ^[0-9]+$ ]]; then lock_age=$(( $(date +%s) - lock_mtime )) if [ "$lock_age" -gt 60 ]; then rmdir "$LOCK_DIR" 2>/dev/null || rm -rf "$LOCK_DIR" 2>/dev/null diff --git a/verify.sh b/verify.sh index 9b25595..8c22117 100755 --- a/verify.sh +++ b/verify.sh @@ -406,6 +406,22 @@ else check "ledger-append.sh concurrency: lock dir removed after run" "$PASS" fi +# A lock dir left by a crashed writer (2020 mtime) must be swept at once on +# both GNU and BSD stat. On Linux, `stat -f` is filesystem status and once +# broke the age arithmetic, so the writer died and the entry was lost. +TMPDIR_STALE=$(mktemp -d) +mkdir -p "$TMPDIR_STALE/.localdev/workflow/.done.md.lock" +touch -t 202001010000 "$TMPDIR_STALE/.localdev/workflow/.done.md.lock" +(cd "$TMPDIR_STALE" && printf '## stale-entry\n' | bash "$SCRIPT_DIR/scripts/ledger-append.sh" "done" 2>/dev/null) +if grep -q '^## stale-entry$' "$TMPDIR_STALE/.localdev/workflow/done.md" 2>/dev/null \ + && ! grep -q 'lock-timeout' "$TMPDIR_STALE/.localdev/workflow/done.md" \ + && [ ! -d "$TMPDIR_STALE/.localdev/workflow/.done.md.lock" ]; then + check "ledger-append.sh: stale lock swept at once, entry appended without timeout" "$PASS" +else + check "ledger-append.sh: stale lock swept at once, entry appended without timeout" "$FAIL" +fi +rm -rf "$TMPDIR_STALE" + (cd "$TMPDIR_LA" && echo hi | bash "$SCRIPT_DIR/scripts/ledger-append.sh" bogus >/dev/null 2>/tmp/ledger_err_$$.txt) LA_BOGUS_EXIT=$? if [ "$LA_BOGUS_EXIT" -ne 0 ] && [ -s "/tmp/ledger_err_$$.txt" ]; then