Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions scripts/ledger-append.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions verify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading