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
43 changes: 31 additions & 12 deletions bin/fm-wake-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ fm_recovery_marker_reopen_announced() {
}

fm_lock_try_acquire() {
local lockdir=$1 pid steal cur rc steal_owner primary_owner current
local lockdir=$1 allow_steal=${2:-yes} pid steal cur rc steal_owner primary_owner current sblock_pid
FM_LOCK_HELD_PID=
FM_LOCK_OWNER_DIR=
FM_LOCK_RECOVERED_PID=
Expand Down Expand Up @@ -950,27 +950,33 @@ fm_lock_try_acquire() {
fi

steal="$lockdir.steal"
if ! fm_lock_try_acquire "$steal"; then
FM_LOCK_HELD_PID=$(cat "$lockdir/pid" 2>/dev/null || true)
FM_LOCK_OWNER_DIR=
return 1
steal_owner=
if [ "$allow_steal" = yes ]; then
# The steal mutex serializes primary-lock recovery. It is acquired WITHOUT
# its own steal-mutex recovery (allow_steal=no): recursing here let a stale
# .steal lock grow an unbounded .steal.steal... chain until ENAMETOOLONG.
if ! fm_lock_try_acquire "$steal" no; then
FM_LOCK_HELD_PID=$(cat "$lockdir/pid" 2>/dev/null || true)
FM_LOCK_OWNER_DIR=
return 1
fi
steal_owner=${FM_LOCK_OWNER_DIR:-}
fi
steal_owner=${FM_LOCK_OWNER_DIR:-}

cur=$(cat "$lockdir/pid" 2>/dev/null || true)
if fm_pid_alive "$cur"; then
fm_lock_release "$steal"
[ -n "$steal_owner" ] && fm_lock_release "$steal"
FM_LOCK_HELD_PID=$cur
FM_LOCK_OWNER_DIR=
return 1
fi
if fm_lock_mid_acquire_is_fresh "$lockdir" "$cur"; then
fm_lock_release "$steal"
[ -n "$steal_owner" ] && fm_lock_release "$steal"
FM_LOCK_HELD_PID=$cur
FM_LOCK_OWNER_DIR=
return 1
fi
if ! fm_lock_points_to_owner "$steal" "$steal_owner"; then
if [ -n "$steal_owner" ] && ! fm_lock_points_to_owner "$steal" "$steal_owner"; then
fm_lock_release "$steal"
FM_LOCK_HELD_PID=$(cat "$lockdir/pid" 2>/dev/null || true)
FM_LOCK_OWNER_DIR=
Expand All @@ -983,19 +989,32 @@ fm_lock_try_acquire() {
fi
cur=$(cat "$lockdir/pid" 2>/dev/null || true)
if ! fm_lock_recheck_stale_owner "$lockdir" "$primary_owner" "$cur"; then
fm_lock_release "$steal"
[ -n "$steal_owner" ] && fm_lock_release "$steal"
FM_LOCK_HELD_PID=$(cat "$lockdir/pid" 2>/dev/null || true)
FM_LOCK_OWNER_DIR=
return 1
fi

if [ "$lockdir" = "$STATE/.watch.lock" ] \
&& ! _fm_recovery_marker_publish "$STATE/.watcher-down" downtime; then
fm_lock_release "$steal"
[ -n "$steal_owner" ] && fm_lock_release "$steal"
FM_LOCK_HELD_PID=$cur
FM_LOCK_OWNER_DIR=
return 1
fi
if [ "$allow_steal" = no ]; then
# One bounded level only: a stale serialization mutex left behind by an
# interrupted or long-dead recovery is reclaimed here directly, after the
# same dead-owner and mid-acquire-freshness checks, and we never recurse
# into its own steal lock.
if [ -e "$steal" ] || [ -L "$steal" ]; then
sblock_pid=$(cat "$steal/pid" 2>/dev/null || true)
if ! fm_pid_alive "$sblock_pid" \
&& ! fm_lock_mid_acquire_is_fresh "$steal" "$sblock_pid"; then
fm_lock_remove_path "$steal" || true
fi
fi
fi
fm_lock_remove_path "$lockdir" || true
rc=1
if fm_lock_try_create "$lockdir" "$steal_owner"; then
Expand All @@ -1008,7 +1027,7 @@ fm_lock_try_acquire() {
FM_LOCK_HELD_PID=$(cat "$lockdir/pid" 2>/dev/null || true)
FM_LOCK_OWNER_DIR=
fi
fm_lock_release "$steal"
[ -n "$steal_owner" ] && fm_lock_release "$steal"
return "$rc"
}

Expand Down
32 changes: 32 additions & 0 deletions tests/fm-wake-queue.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1689,6 +1689,37 @@ test_self_held_lock_reclaims_instead_of_deadlocking() {
pass "an abandoned same-process lock hold is reclaimed; a parent's live hold is not"
}

# A stale primary lock whose abandoned .steal serialization mutex (and its own
# stale .steal.steal) is still in place must be recovered WITHOUT recursing
# down an ever-growing .steal.steal... chain: the steal-mutex acquisition
# path reclaimed recursively until "File name too long".
test_stale_steal_chain_recovers_without_unbounded_suffixes() {
local dir state rc
dir=$(make_case stale-steal-chain)
state="$dir/state"
rc=0
mkdir -p "$state"
dir="$dir" FM_STATE_OVERRIDE="$state" bash -c '
. "$1"
lock="$2/.fixture.lock"
dead=999999
mkdir -p "$dir/primary" && echo "$dead" > "$dir/primary/pid"
ln -s "$dir/primary" "$lock"
for suffix in ".steal" ".steal.steal"; do
mkdir -p "$lock$suffix-owner" && echo "$dead" > "$lock$suffix-owner/pid"
ln -s "$lock$suffix-owner" "$lock$suffix"
touch -t 202001010000 "$lock$suffix" "$lock$suffix-owner"
done
touch -t 202001010000 "$dir/primary" "$lock"
fm_lock_try_acquire "$lock" || exit 20
[ -L "$lock" ] || exit 21
[ ! -e "$lock.steal.steal.steal" ] && [ ! -L "$lock.steal.steal.steal" ] || exit 22
fm_lock_release "$lock" || exit 23
' _ "$ROOT/bin/fm-wake-lib.sh" "$state" || rc=$?
[ "${rc:-0}" -eq 0 ] || fail "stale .steal chain was not recovered (rc=$rc)"
pass "stale .steal chain recovers without growing .steal suffixes"
}

test_subshell_lock_ownership_without_bashpid() {
local dir state rc
dir=$(make_case subshell-lock-ownership)
Expand Down Expand Up @@ -2010,6 +2041,7 @@ test_historical_annotation_skips_announced_status() {
}

test_self_held_lock_reclaims_instead_of_deadlocking
test_stale_steal_chain_recovers_without_unbounded_suffixes
test_subshell_lock_ownership_without_bashpid
test_bounded_lock_handoff_after_contention
test_live_presentation_holder_is_deadlined_without_weakening_ack
Expand Down
Loading