diff --git a/bin/fm-remote-doctor.sh b/bin/fm-remote-doctor.sh index aad9ce44aa7..cc874c65f13 100755 --- a/bin/fm-remote-doctor.sh +++ b/bin/fm-remote-doctor.sh @@ -244,7 +244,7 @@ remote_job_probe_ok() { mtime=$(fm_remote_job_path_mtime "$ready" 2>/dev/null || true) case "$mtime" in ''|*[!0-9]*) return 1 ;; esac now=$(date +%s) - [ $((now - mtime)) -le 10 ] + [ $((now - mtime)) -le "$FM_REMOTE_JOB_PROBE_FRESHNESS_SECONDS" ] } remote_job_identity_ok() { diff --git a/bin/fm-remote-entrypoint.sh b/bin/fm-remote-entrypoint.sh index 6763e8c955d..53969280fcb 100755 --- a/bin/fm-remote-entrypoint.sh +++ b/bin/fm-remote-entrypoint.sh @@ -22,7 +22,7 @@ set -eu PROTOCOL=1 -DOCTOR_SHA256=7bb13d9fad8455978bf109d4681a3aa3cb170565c8a74be4ec7b520427db14c2 +DOCTOR_SHA256=f0e9c423a1deeb1a6bfcbc5f595e958b9d6fb620606f3804dafdc3d837f088c6 REAL_SOURCE=$(python3 -c 'import os, sys; print(os.path.realpath(sys.argv[1]))' "${BASH_SOURCE[0]}" 2>/dev/null) || REAL_SOURCE=$(realpath "${BASH_SOURCE[0]}" 2>/dev/null) || REAL_SOURCE=${BASH_SOURCE[0]} diff --git a/bin/fm-remote-job-lib.sh b/bin/fm-remote-job-lib.sh index 73bffa54c70..8b794e3187a 100755 --- a/bin/fm-remote-job-lib.sh +++ b/bin/fm-remote-job-lib.sh @@ -57,6 +57,12 @@ FM_REMOTE_JOB_TIMEOUT=${FM_REMOTE_JOB_TIMEOUT:-360} FM_REMOTE_JOB_WAIT_GRACE=${FM_REMOTE_JOB_WAIT_GRACE:-30} FM_REMOTE_JOB_POLL_SECONDS=${FM_REMOTE_JOB_POLL_SECONDS:-0.05} FM_REMOTE_JOB_REAP_SECONDS=${FM_REMOTE_JOB_REAP_SECONDS:-3600} +FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS=${FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS:-3} +FM_REMOTE_JOB_REAP_INTERVAL_SECONDS=${FM_REMOTE_JOB_REAP_INTERVAL_SECONDS:-60} +# The single owner of fm_remote_job_probe's freshness window, so the heartbeat +# interval bound below cannot silently drift out of sync with what a prober +# actually tolerates. +FM_REMOTE_JOB_PROBE_FRESHNESS_SECONDS=10 # shellcheck disable=SC2034 # Shared protocol constant consumed by the worker and sourcing callers. FM_REMOTE_JOB_PREEMPTED_EXIT=76 FM_REMOTE_JOB_OPERATOR_PATH= @@ -93,6 +99,9 @@ fm_remote_job_validate_settings() { case "$FM_REMOTE_JOB_WAIT_GRACE" in ''|*[!0-9]*) return 1 ;; esac [ "$FM_REMOTE_JOB_WAIT_GRACE" -le 300 ] || return 1 case "$FM_REMOTE_JOB_REAP_SECONDS" in ''|*[!0-9]*|0) return 1 ;; esac + case "$FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS" in ''|*[!0-9]*|0) return 1 ;; esac + [ "$FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS" -lt "$FM_REMOTE_JOB_PROBE_FRESHNESS_SECONDS" ] || return 1 + case "$FM_REMOTE_JOB_REAP_INTERVAL_SECONDS" in ''|*[!0-9]*|0) return 1 ;; esac return 0 } @@ -881,7 +890,7 @@ fm_remote_job_probe() { # ; a fresh worker heartbeat or active job mtime=$(fm_remote_job_path_mtime "$ready" 2>/dev/null || true) case "$mtime" in ''|*[!0-9]*) return 1 ;; esac now=$(date +%s) - [ $((now - mtime)) -le 10 ] + [ $((now - mtime)) -le "$FM_REMOTE_JOB_PROBE_FRESHNESS_SECONDS" ] } fm_remote_job_wait_for_probe() { # diff --git a/bin/fm-remote-job-worker.sh b/bin/fm-remote-job-worker.sh index edd123c859e..259d013a277 100755 --- a/bin/fm-remote-job-worker.sh +++ b/bin/fm-remote-job-worker.sh @@ -15,6 +15,15 @@ # have been committed. The library header owns the exact record fields and # lifecycle. # +# Job pickup stays on the fast FM_REMOTE_JOB_POLL_SECONDS tick, while the two +# loop-maintenance duties run on their own cadence so an idle worker does not +# fork a heartbeat write and a full queue rescan every tick: worker.ready is +# refreshed every FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS and stale done +# records are swept every FM_REMOTE_JOB_REAP_INTERVAL_SECONDS. That heartbeat +# interval is validated strictly below FM_REMOTE_JOB_PROBE_FRESHNESS_SECONDS, +# the window the library's probe tolerates, so a healthy worker between writes +# never reads as unready. +# # The worker is abandoned when its configured FM_ROOT stops being a genuine # Firstmate checkout - the state a pruned no-mistakes gate worktree, a returned # pooled worktree, or a removed test fixture root leaves behind. It can never @@ -75,6 +84,11 @@ worker_write_heartbeat() { mv -f -- "$tmp" "$ready" } +worker_refresh_heartbeat() { + worker_write_heartbeat || { worker_error "cannot update worker heartbeat"; exit 1; } + next_heartbeat=$((SECONDS + FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS)) +} + worker_publish_pid() { local pid_file tmp pid_file=$(fm_remote_job_worker_pid_path) @@ -677,7 +691,7 @@ worker_process_once() { # } main() { - local account_home lock_status + local account_home lock_status next_heartbeat next_reap account_home=$(worker_account_home) || { worker_error "cannot resolve account home"; exit 1; } FM_ROOT=$(fm_remote_job_canonical_existing_dir "$FM_ROOT") || { worker_error "configured FM_ROOT is unsafe"; exit 1; } [ -f "$FM_ROOT/AGENTS.md" ] && [ ! -L "$FM_ROOT/AGENTS.md" ] || { worker_error "FM_ROOT is not a Firstmate checkout"; exit 1; } @@ -695,18 +709,29 @@ main() { trap worker_shutdown HUP INT TERM worker_publish_identity "$account_home" || { worker_error "cannot publish worker code identity"; exit 1; } worker_publish_pid || { worker_error "cannot publish worker pid"; exit 1; } + worker_refresh_heartbeat + next_reap=$SECONDS while :; do - worker_write_heartbeat || { worker_error "cannot update worker heartbeat"; exit 1; } - # Checked right after a fresh heartbeat, so the grace window cannot make a - # still-healthy worker read as unready to a concurrent probe. - if worker_code_root_abandoned; then - worker_error "configured FM_ROOT $FM_ROOT no longer exists; stopping the abandoned worker" - exit 0 + if [ "$SECONDS" -ge "$next_heartbeat" ]; then + worker_refresh_heartbeat + fi + if ! fm_remote_job_root_is_live "$FM_ROOT"; then + # A prober's freshness window must not lapse during the abandoned-root + # grace check below, which can block for seconds confirming a transient + # disappearance instead of a real one. + worker_refresh_heartbeat + if worker_code_root_abandoned; then + worker_error "configured FM_ROOT $FM_ROOT no longer exists; stopping the abandoned worker" + exit 0 + fi fi - worker_reap=0 - if [ "$worker_reap" -eq 0 ]; then + if [ "$SECONDS" -ge "$next_reap" ]; then + # The sweep re-prepares the state tree, so refresh first: a deleted state + # root must fail this write and stop the worker for its supervisor rather + # than be silently recreated behind an owner that holds no lock. + worker_refresh_heartbeat fm_remote_job_reap_stale "$account_home" || true - worker_reap=1 + next_reap=$((SECONDS + FM_REMOTE_JOB_REAP_INTERVAL_SECONDS)) fi worker_process_once "$account_home" sleep "$FM_REMOTE_JOB_POLL_SECONDS" diff --git a/tests/fm-remote-job.test.sh b/tests/fm-remote-job.test.sh index 0b6dede4d65..b056ea8b22d 100755 --- a/tests/fm-remote-job.test.sh +++ b/tests/fm-remote-job.test.sh @@ -19,6 +19,8 @@ REAL_GIT=$(command -v git) OTHER_PID= RECOVERY_WORKER_PID= REPEAT_WORKER_PID= +IDLE_WORKER_PID= +TRIPWIRE_WORKER_PID= mkdir -p "$REMOTE_ROOT/bin" "$REMOTE_HOME" "$ACCOUNT_HOME" "$RUNTIME_BIN" # worker.pid records the serving child, not its restart supervisor, so stopping # that pid alone leaves the supervisor to respawn - the leak @@ -27,6 +29,8 @@ cleanup_remote_job_fixture() { [ -z "$OTHER_PID" ] || kill "$OTHER_PID" 2>/dev/null || true [ -z "$RECOVERY_WORKER_PID" ] || kill "$RECOVERY_WORKER_PID" 2>/dev/null || true [ -z "$REPEAT_WORKER_PID" ] || kill "$REPEAT_WORKER_PID" 2>/dev/null || true + [ -z "$IDLE_WORKER_PID" ] || kill "$IDLE_WORKER_PID" 2>/dev/null || true + [ -z "$TRIPWIRE_WORKER_PID" ] || kill "$TRIPWIRE_WORKER_PID" 2>/dev/null || true if [ -f "$STATE_ROOT/worker.pid" ]; then fm_remote_job_stop_worker_tree "$(cat "$STATE_ROOT/worker.pid")" || true fi @@ -691,4 +695,145 @@ wait "$REPEAT_WORKER_PID" 2>/dev/null || true REPEAT_WORKER_PID= pass "a repeatedly signalled shutdown still releases ownership for the next worker" +# Fake mktemp/date/sleep shadow the real ones on PATH to count calls, still +# exec'ing through. Idle mktemp comes only from heartbeat writes, idle date +# only from reap_stale, and idle sleep only from the poll tick itself. +IDLE_HOME="$TMP_ROOT/idle-cadence-account" +IDLE_STATE="$TMP_ROOT/idle-cadence-jobs" +IDLE_BIN="$TMP_ROOT/idle-cadence-bin" +IDLE_MKTEMP_LOG="$TMP_ROOT/idle-cadence-mktemp.log" +IDLE_DATE_LOG="$TMP_ROOT/idle-cadence-date.log" +IDLE_SLEEP_LOG="$TMP_ROOT/idle-cadence-sleep.log" +mkdir -p "$IDLE_HOME" "$IDLE_BIN" +chmod 700 "$IDLE_HOME" +: > "$IDLE_MKTEMP_LOG" +: > "$IDLE_DATE_LOG" +: > "$IDLE_SLEEP_LOG" +REAL_MKTEMP=$(command -v mktemp) +REAL_DATE=$(command -v date) +REAL_SLEEP=$(command -v sleep) +cat > "$IDLE_BIN/mktemp" <> "$IDLE_MKTEMP_LOG" +exec "$REAL_MKTEMP" "\$@" +SH +cat > "$IDLE_BIN/date" <> "$IDLE_DATE_LOG" +exec "$REAL_DATE" "\$@" +SH +cat > "$IDLE_BIN/sleep" <> "$IDLE_SLEEP_LOG" +exec "$REAL_SLEEP" "\$@" +SH +chmod +x "$IDLE_BIN/mktemp" "$IDLE_BIN/date" "$IDLE_BIN/sleep" +HOME="$IDLE_HOME" PATH="$IDLE_BIN:/usr/bin:/bin:/usr/sbin:/sbin" FM_ROOT_OVERRIDE="$REMOTE_ROOT" \ + FM_REMOTE_JOB_STATE_ROOT="$IDLE_STATE" FM_REMOTE_JOB_PLATFORM_OVERRIDE=Linux \ + FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS=2 FM_REMOTE_JOB_REAP_INTERVAL_SECONDS=2 \ + FM_REMOTE_JOB_POLL_SECONDS=0.02 \ + "$REMOTE_ROOT/bin/fm-remote-job-worker.sh" --serve \ + > "$TMP_ROOT/idle-cadence.out" 2> "$TMP_ROOT/idle-cadence.err" & +IDLE_WORKER_PID=$! +for _ in $(seq 1 300); do + [ -f "$IDLE_STATE/worker.ready" ] && break + sleep 0.05 +done +assert_present "$IDLE_STATE/worker.ready" "the idle-cadence worker did not become ready" +# Startup (lock acquisition, identity, pid, the first heartbeat) already forked +# these before this point; only counts from here measure the idle cadence +# itself, not one-time startup cost. +: > "$IDLE_MKTEMP_LOG" +: > "$IDLE_DATE_LOG" +: > "$IDLE_SLEEP_LOG" +sleep 6 +kill -TERM "$IDLE_WORKER_PID" +wait "$IDLE_WORKER_PID" 2>/dev/null || true +IDLE_WORKER_PID= +MKTEMP_CALLS=$(wc -l < "$IDLE_MKTEMP_LOG" | tr -d ' ') +DATE_CALLS=$(wc -l < "$IDLE_DATE_LOG" | tr -d ' ') +# The poll tick is the only per-iteration sleep while idle, so counting it +# measures the cadence as a ratio instead of an absolute fork count that a +# loaded runner (or the regression's own forking) would silently rescale. +TICK_CALLS=$(wc -l < "$IDLE_SLEEP_LOG" | tr -d ' ') +[ "$TICK_CALLS" -ge 20 ] \ + || fail "the idle worker polled only $TICK_CALLS times; the cadence window is too short to measure" +[ "$DATE_CALLS" -ge 2 ] && [ $((DATE_CALLS * 3)) -le "$TICK_CALLS" ] \ + || fail "idle reap forked date $DATE_CALLS times across $TICK_CALLS poll ticks; the dead reap throttle regressed to running every poll tick" +[ "$MKTEMP_CALLS" -ge 2 ] && [ $((MKTEMP_CALLS * 3)) -le "$TICK_CALLS" ] \ + || fail "idle heartbeat forked mktemp $MKTEMP_CALLS times across $TICK_CALLS poll ticks; expected the write gated to its own cadence" +pass "idle heartbeat and reap run on their own cadence, not on every poll tick" + +TRIPWIRE_HOME="$TMP_ROOT/tripwire-home" +TRIPWIRE_STATE="$TMP_ROOT/tripwire-jobs" +mkdir -p "$TRIPWIRE_HOME" +chmod 700 "$TRIPWIRE_HOME" +HOME="$TRIPWIRE_HOME" FM_ROOT_OVERRIDE="$REMOTE_ROOT" \ + FM_REMOTE_JOB_STATE_ROOT="$TRIPWIRE_STATE" FM_REMOTE_JOB_PLATFORM_OVERRIDE=Linux \ + FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS=9 FM_REMOTE_JOB_REAP_INTERVAL_SECONDS=1 \ + FM_REMOTE_JOB_POLL_SECONDS=0.02 \ + "$REMOTE_ROOT/bin/fm-remote-job-worker.sh" --serve \ + > "$TMP_ROOT/tripwire.out" 2> "$TMP_ROOT/tripwire.err" & +TRIPWIRE_WORKER_PID=$! +for _ in $(seq 1 300); do + [ -f "$TRIPWIRE_STATE/worker.ready" ] && break + sleep 0.05 +done +assert_present "$TRIPWIRE_STATE/worker.ready" "the state-root tripwire worker did not become ready" +# The reap sweep re-prepares the state tree, so a worker whose root is deleted +# must fail fast on its heartbeat before that sweep can silently recreate an +# unowned tree and leave two workers serving one queue. +rm -rf -- "$TRIPWIRE_STATE" +for _ in $(seq 1 200); do + kill -0 "$TRIPWIRE_WORKER_PID" 2>/dev/null || break + sleep 0.05 +done +if kill -0 "$TRIPWIRE_WORKER_PID" 2>/dev/null; then + kill -KILL "$TRIPWIRE_WORKER_PID" 2>/dev/null || true + wait "$TRIPWIRE_WORKER_PID" 2>/dev/null || true + TRIPWIRE_WORKER_PID= + fail "the worker kept serving after its state root was deleted" +fi +TRIPWIRE_STATUS=0 +wait "$TRIPWIRE_WORKER_PID" 2>/dev/null || TRIPWIRE_STATUS=$? +TRIPWIRE_WORKER_PID= +[ "$TRIPWIRE_STATUS" -ne 0 ] \ + || fail "a worker whose state root vanished exited cleanly instead of failing for its supervisor to restart" +assert_absent "$TRIPWIRE_STATE" "the reap sweep recreated the deleted worker state root behind an unowned worker" +pass "a deleted state root fails the worker fast instead of being recreated by the reap sweep" + +# A prober reads worker.ready as fresh for FM_REMOTE_JOB_PROBE_FRESHNESS_SECONDS, +# so a heartbeat cadence at or past that window would let a healthy worker read +# as unready between writes. Settings validation refuses it before any state. +for BAD_HEARTBEAT in '' abc 0 "$FM_REMOTE_JOB_PROBE_FRESHNESS_SECONDS" \ + "$((FM_REMOTE_JOB_PROBE_FRESHNESS_SECONDS + 1))"; do + ( FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS=$BAD_HEARTBEAT + fm_remote_job_validate_settings ) \ + && fail "a heartbeat interval of '$BAD_HEARTBEAT' was accepted" +done +( FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS=$((FM_REMOTE_JOB_PROBE_FRESHNESS_SECONDS - 1)) + fm_remote_job_validate_settings ) \ + || fail "the widest heartbeat interval still inside the freshness window was rejected" +for BAD_REAP in '' abc 0; do + ( FM_REMOTE_JOB_REAP_INTERVAL_SECONDS=$BAD_REAP + fm_remote_job_validate_settings ) \ + && fail "a reap interval of '$BAD_REAP' was accepted" +done +BOUNDS_HOME="$TMP_ROOT/bounds-home" +BOUNDS_STATE="$TMP_ROOT/bounds-jobs" +mkdir -p "$BOUNDS_HOME" +chmod 700 "$BOUNDS_HOME" +BOUNDS_STATUS=0 +HOME="$BOUNDS_HOME" FM_ROOT_OVERRIDE="$REMOTE_ROOT" \ + FM_REMOTE_JOB_STATE_ROOT="$BOUNDS_STATE" FM_REMOTE_JOB_PLATFORM_OVERRIDE=Linux \ + FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS="$FM_REMOTE_JOB_PROBE_FRESHNESS_SECONDS" \ + "$REMOTE_ROOT/bin/fm-remote-job-worker.sh" --serve \ + > "$TMP_ROOT/bounds.out" 2> "$TMP_ROOT/bounds.err" || BOUNDS_STATUS=$? +[ "$BOUNDS_STATUS" -ne 0 ] \ + || fail "the worker served with a heartbeat cadence outside the probe freshness window" +grep -q 'bounds or timeout are invalid' "$TMP_ROOT/bounds.err" \ + || fail "the refused worker did not report invalid bounds: $(cat "$TMP_ROOT/bounds.err")" +assert_absent "$BOUNDS_STATE/worker.ready" "a worker with a refused heartbeat cadence still published a heartbeat" +pass "a heartbeat cadence outside the probe freshness window is refused before the worker serves" + echo "ALL TESTS PASSED"