Skip to content

bin/fm-wake-grant.sh discards a failed lock acquisition: release/deactivate delete branch-eligibility state without the lock and exit 0 once fm_lock_acquire_wait becomes partial (reproduced with PR #3593) #3602

Closed

Description

Summary

bin/fm-wake-grant.sh calls fm_lock_acquire_wait bare at four sites and never checks the result. That is safe on main today only because fm_lock_acquire_wait is total — it loops until it holds the lock and can only return 0:

# bin/fm-wake-lib.sh (main, d3fcdfa)
fm_lock_acquire_wait() {
  local lockdir=$1
  while ! fm_lock_try_acquire "$lockdir"; do
    sleep 0.1
  done
}

The moment that function becomes partial, two of those four sites delete branch-eligibility state without holding the lock and exit 0. PR #3593 makes it partial and does not touch bin/fm-wake-grant.sh, so the failure is reachable there today.

bin/fm-wake-grant.sh runs under set -u with no set -e (line 2), and the calls are bare statements, not in any condition — so a non-zero status is simply discarded.

Where

# bin/fm-wake-grant.sh:104-108   (release)
    [ "$#" -eq 2 ] || exit 2
    fm_lock_acquire_wait "$FM_WAKE_QUEUE_LOCK"
    LOCK_HELD=true
    owner_matches '' "$generation" || exit 1
    rm -f -- "$BRANCH_ROWS" || exit 1
# bin/fm-wake-grant.sh:112-117   (deactivate)
    [ "$#" -eq 3 ] || exit 2
    fm_lock_acquire_wait "$FM_WAKE_QUEUE_LOCK"
    LOCK_HELD=true
    owner_matches "$pid" "$generation" || exit 1
    rm -f -- "$BRANCH_ROWS" "$BRANCH_OWNER" || exit 1

owner_matches is a read-only validation performed before the rm; it provides no mutual exclusion. $FM_WAKE_QUEUE_LOCK is the same lock fm_wake_append and fm_wake_queued_keys take, so a concurrent holder can be mid-append when the delete lands.

Reproduction

On a genuinely full filesystem mkdir/mktemp -d fail while unlink still succeeds. That is what decouples the two: fm_lock_owner_dir cannot create its owner directory, but the subsequent rm -f works fine.

The script below creates a 2 MB HFS+ image, fills it, holds $FM_WAKE_QUEUE_LOCK in a separate live process, then runs fm-wake-grant.sh deactivate. No shims, stubs, or PATH interposition — the only injected condition is a real out-of-space volume.

repro.sh
#!/usr/bin/env bash
set -u
ROOT=${1:?usage: repro.sh <firstmate-checkout>}
LABEL=${2:-TREE}
IMG=$(mktemp -u "${TMPDIR:-/tmp}/fmenospc.XXXXXX").dmg
hdiutil create -size 2m -fs HFS+ -volname FMENOSPC -quiet "$IMG" || exit 1
MNT=$(mktemp -d "${TMPDIR:-/tmp}/fmmnt.XXXXXX")
hdiutil attach -nobrowse -quiet -mountpoint "$MNT" "$IMG" || { echo "mount failed"; exit 1; }
cleanup(){ kill ${OWNER_PID:-} ${HOLDER:-} 2>/dev/null; hdiutil detach -quiet "$MNT" 2>/dev/null; rm -f "$IMG"; rmdir "$MNT" 2>/dev/null; }
trap cleanup EXIT

export FM_STATE_OVERRIDE="$MNT/state"; mkdir -p "$FM_STATE_OVERRIDE"
export FM_LOCK_STALE_AFTER=3600            # keep the holder's lock fresh; no stealing

sleep 120 & OWNER_PID=$!
IDENT=$(cd "$ROOT" && . bin/fm-wake-lib.sh >/dev/null 2>&1; fm_pid_identity "$OWNER_PID")
printf 'fm-branch-eligible-owner-v1\n%s\n%s\n%s\n' "$OWNER_PID" "$IDENT" gen1 > "$FM_STATE_OVERRIDE/.branch-eligible-owner"
chmod 0600 "$FM_STATE_OVERRIDE/.branch-eligible-owner"
echo 7 > "$FM_STATE_OVERRIDE/.branch-eligible-rows"

# a second live process holds the wake-queue lock for the whole run
( cd "$ROOT" && . bin/fm-wake-lib.sh >/dev/null 2>&1
  fm_lock_acquire_wait "$FM_WAKE_QUEUE_LOCK" && { touch "$FM_STATE_OVERRIDE/.held"; sleep 120; } ) &
HOLDER=$!
for _ in $(seq 1 50); do [ -e "$FM_STATE_OVERRIDE/.held" ] && break; sleep 0.1; done
echo "[$LABEL] lock held by another process: $([ -L "$FM_STATE_OVERRIDE/.wake-queue.lock" ] && echo YES || echo NO)"

dd if=/dev/zero of="$MNT/fill" bs=64k >/dev/null 2>&1
i=0; while [ $i -lt 4000 ]; do mkdir "$MNT/d$i" 2>/dev/null || break; i=$((i+1)); done
PROBE=$(mktemp -d "$FM_STATE_OVERRIDE/probe.XXXXXX" 2>&1) && { echo "fault not injected"; rmdir "$PROBE"; exit 3; }
echo "[$LABEL] mktemp -d fails: $PROBE"
echo "[$LABEL] rm still works: $(touch "$FM_STATE_OVERRIDE/.p" 2>/dev/null; rm -f "$FM_STATE_OVERRIDE/.p" && echo yes || echo no)"

"$ROOT/bin/fm-wake-grant.sh" deactivate "$OWNER_PID" gen1 & GRANT=$!
RC=timeout
for _ in $(seq 1 150); do kill -0 $GRANT 2>/dev/null || { wait $GRANT; RC=$?; break; }; sleep 0.1; done
if [ "$RC" = timeout ]; then kill -9 $GRANT 2>/dev/null; echo "[$LABEL] BLOCKED (15s) - fail-closed"
else echo "[$LABEL] RETURNED exit=$RC"; fi
echo "[$LABEL] .branch-eligible-rows  $([ -e "$FM_STATE_OVERRIDE/.branch-eligible-rows" ] && echo present || echo '*** DELETED WITHOUT THE LOCK ***')"
echo "[$LABEL] .branch-eligible-owner $([ -e "$FM_STATE_OVERRIDE/.branch-eligible-owner" ] && echo present || echo '*** DELETED WITHOUT THE LOCK ***')"
echo "[$LABEL] lock still held by the other process: $([ -L "$FM_STATE_OVERRIDE/.wake-queue.lock" ] && echo YES || echo NO)"

With PR #3593 applied

[PR] lock held by another process: YES
[PR] mktemp -d fails: mktemp: mkdtemp failed on .../state/probe.qlftLF: No space left on device
[PR] rm still works: yes
[PR] --- fm-wake-grant.sh deactivate 43268 gen1 ---
[PR] RETURNED exit=0
[PR] .branch-eligible-rows  *** DELETED WITHOUT THE LOCK ***
[PR] .branch-eligible-owner *** DELETED WITHOUT THE LOCK ***
[PR] lock still held by the other process: YES

Same harness, same conditions, on main (d3fcdfa)

[MAIN] lock held by another process: YES
[MAIN] rm still works: yes
[MAIN] --- fm-wake-grant.sh deactivate 96915 gen1 ---
[MAIN] BLOCKED (15s) - fail-closed
[MAIN] .branch-eligible-rows  present
[MAIN] .branch-eligible-owner present
[MAIN] lock still held by the other process: YES

Why it matters

The prior behaviour was an indefinite block, which is not good either — but it is fail-closed, and AGENTS.md states the intent that scripts stop safely when a guard cannot complete. The new behaviour deletes shared state unserialized and then reports success, and the caller cannot tell: deactivateEligibleRowsOwner in .pi/extensions/lib/fm-branch-dispatch.ts is return runGrantScript(state, grantScript, ["deactivate", ...]) === 0, so it is told the deactivate succeeded.

The lock itself is not stolen — fm_lock_release is pid-guarded, so the LOCK_HELD=true cleanup is a no-op. The damage is the unserialized deletion plus the false exit 0.

Scope — what is not affected

I checked all four call sites and one adjacent report, and most of them are fine:

  • bin/fm-wake-grant.sh:60 (activate) and :76 (publish) are self-defended. Each is preceded by TMP=$(mktemp "$STATE/...") || exit 1, which fails first under the same condition. Under ENOSPC both exit 1 before reaching the lock. They are latent only — worth guarding for consistency, but not reproducible.
  • bin/fm-home-summary-refresh.sh:114 is not affected. Line 117's mktemp ... || { home_summary_fail; return 1; } runs before any mutation, so it fails safe.
  • Wrapper-final calls (fm_procevent_source_lock_acquire, fm_pf_registry_lock_acquire, extension_lifecycle_lock_acquire) propagate the status to callers that check it.
  • PR fix(bin): bound lock stealing to one .steal level and refuse uncreatable locks #2796, the other in-flight fix in this area, leaves fm_lock_acquire_wait unchanged and does not have this problem.

Suggested direction

Guard the two reachable sites the way PR #3593 already guards the callers inside the file it touches (bin/fm-wake-lib.sh:1772 and :1801, both || return 1) — for example fm_lock_acquire_wait "$FM_WAKE_QUEUE_LOCK" || exit 1 at bin/fm-wake-grant.sh:105 and :114. More generally, a contract change from total to partial on a function with ~100 call sites may be worth a mechanical sweep rather than a hand audit.

Platform

macOS 26.5.1, arm64, stock bash 3.2.57(1)-release. main at d3fcdfa; PR #3593 applied as a working-tree diff onto that same base. Reproduced on a real HFS+ image; not tried on Linux, where a full ext4 should behave the same but the hdiutil setup differs.

I am an outside contributor and have not run the full suite; this is a targeted reproduction only.

Activity

  1. added
    ready-for-pr
    on Sep 3, 2026
  2. kunchenguid commented on Sep 3, 2026

    @kunchenguid
    Owner

    Speaking as Kun's firstmate:

    Finding: Confirmed on main 3d2a08b. bin/fm-wake-grant.sh runs under set -u (no set -e) and calls fm_lock_acquire_wait bare at activate/publish/release/deactivate. On main the helper is still total (infinite wait). Open PR #3593 makes it partial (return 2 when the owner dir cannot be created) and guards callers inside bin/fm-wake-lib.sh, but does not touch fm-wake-grant.sh. Under ENOSPC with #3593 applied, release/deactivate discard the failed acquire, delete .branch-eligible-* without the lock, and exit 0 — reproduced as described. Activate/publish stay latent-safe via prior mktemp || exit 1.

    Contract-class (intended fix): restore — restore fail-closed lock acquisition at the release/deactivate (and preferably all four) sites, e.g. fm_lock_acquire_wait … || exit 1, matching the || return 1 pattern #3593 already applies elsewhere.

    VISION: aligns — "Scripts stop safely and report when the world surprises them"; unserialized deletion of shared wake/eligibility state plus a false success breaks durability and captain peace of mind. Authority/consent unchanged.

    Labeled ready-for-pr. Do not ping Firstmate/captain from this stamp.

  3. added
    bug
    on Sep 24, 2026
  4. devin-ai-integration commented on Sep 24, 2026

    @devin-ai-integration
    No description provided.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugready-for-pr

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions