Skip to content

Commit ac8a767

Browse files
authored
fix: resurface queued wakes from handling successors (kunchenguid#7466)
* fix(bin): resurface wakes appended while a handling successor is parked The supervision host parks on the handling successor it starts for a wake its engine handled, and a handling successor never resurfaced recovery, so a row another script appended during the park (a captain inbox note from the Basecamp integration) sat queued until an unrelated close - four hours in the observed case. A handling successor now records the newest queued sequence at its start and closes once with the recovery wake while a newer row is still queued; a newer row already consumed only advances that mark, so the predecessor's delivered episode is still never re-announced. Follow-ups, not built here: - A row appended in the short window between a handled close and its successor's start (about a second) is below the successor's mark; if the engine handles that close without claiming the row, the row waits for the next close. Closing that window needs the host to treat rows left queued after a handled attended turn as main's, which is a separate host change. - After a machine reboot the primary session does not restart on its own, so queued wakes wait until it is resumed by hand. Either auto-resume the primary session after boot, or alert the owner when the primary session is down while wakes are queued. * no-mistakes(review): Close handling-successor startup wake race * no-mistakes(test): Stabilize parked-host inbox wake regression timeout * no-mistakes(document): Clarify handling-successor wake continuity guarantees * no-mistakes(ci): Stabilized the flaky remote secondmate lifecycle test by extending the auto-relaunch watcher deadline from ~30 to ~60 seconds. The CI runner exceeded the old deadline by about 3 seconds; the changed production branch was not reachable in this fixture. `bash -n` and `git diff --check` pass. The full test was attempted but exceeded the 600-second local execution limit before reaching this case * no-mistakes(ci): Fixed flaky concurrent Herdr recovery testing by retrying only documented session-lock contention after both competitors finish. Cleanup now handles fixture-owned read-only hooks. Targeted E2E passed; bash syntax and diff checks passed. Shellcheck reported only existing SC1091 source-following notices * no-mistakes(test): Preserve successor wake boundaries across handoffs * no-mistakes(test): Revert successor boundaries and drain teardown fixture wake * no-mistakes(test): Notify main when downtime handback publication fails * no-mistakes(test): Revert downtime handback notification changes * no-mistakes(document): Correct stale supervision wake routing documentation * fix(bin): stop the supervision host deadlocking and re-announcing handed-back closes Two defects in this branch's handling-successor changes surfaced as the "Behavior portable serial 2" shard hanging at its 30-minute limit. A TERM that lands while the host settles a handled turn's successor handoff left the host holding the wake-queue and recovery-marker locks; its exit cleanup then waited forever on the queue lock it still owned. The cleanup now releases those locks if this process owns them before releasing its grant. When an accepted close turns main-only at its turn, the host hands it back and leaves the successor running, but republished the episode as pending downtime, so that successor re-announced the same close and exited. The handback now republishes the episode as already announced; a later durable append still opens a pending episode the successor recovers. * no-mistakes(test): Park on successor after handling settlement failure * no-mistakes(test): Use tab-delimited handled-settlement failure logs --------- Co-authored-by: Caleb LeNoir <calebl@users.noreply.github.com>
1 parent 6999991 commit ac8a767

9 files changed

Lines changed: 276 additions & 22 deletions

‎bin/fm-supervision-host.sh‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,11 @@
7777
# releases the branch's leases and grant, and counts the wake handled only
7878
# when that turn exited cleanly, recorded a durable report
7979
# (bin/fm-branch-report.sh), and left none of its granted rows in the wake
80-
# queue. A handled wake with only routine outcomes never wakes
81-
# main, and neither does any handled wake while away: captain outcomes wait in
80+
# queue. Before parking after a handled turn, it settles the successor handoff;
81+
# docs/watcher-continuity.md "Durable queue and turn-end backstop" owns how
82+
# that transition makes rows still queued for main recoverable. A handled wake
83+
# with only routine outcomes never wakes main, and neither does any handled
84+
# wake while away: captain outcomes wait in
8285
# the outcome store for the return drain's BRANCH OUTCOMES section. A handled
8386
# attended wake that recorded a captain outcome exits with one "supervision-host: branch-outcome:"
8487
# line naming its store rows, without the close it handled; main drains, where
@@ -416,6 +419,11 @@ stop_engine_turn() {
416419
cleanup() {
417420
local rc=$? f
418421
trap - EXIT HUP TERM INT
422+
# A signal can land inside the handled-turn settle, which holds the queue and
423+
# marker locks; the grant release below needs the queue lock, so drop any of
424+
# them this process still owns first (fm_lock_release ignores foreign owners).
425+
fm_lock_release "$STATE/.watcher-down.lock" 2>/dev/null || true
426+
fm_lock_release "$FM_WAKE_QUEUE_LOCK" 2>/dev/null || true
419427
if [ "$ENGINE_RUNNING" -eq 1 ]; then
420428
stop_engine_turn
421429
fi
@@ -1151,9 +1159,11 @@ while :; do
11511159
detach_successor
11521160
# Main handles this close after all, so hand back the downtime the handoff
11531161
# above consumed: the re-arm owner delivers the close only while the
1154-
# recovery marker reads downtime (leave_successor_for_main).
1162+
# recovery marker reads downtime (leave_successor_for_main). It is handed
1163+
# back as already announced, so the successor left running does not
1164+
# resurface the episode this very close delivers.
11551165
if [ -n "$SUCCESSOR_GENERATION" ] \
1156-
&& ! fm_recovery_marker_publish "$STATE/.watcher-down" downtime >/dev/null 2>&1; then
1166+
&& ! fm_recovery_marker_handback "$STATE/.watcher-down" >/dev/null 2>&1; then
11571167
log_line "pass-through downtime-unrestored $(printf '%s\n' "$REASON" | head -n 1)"
11581168
exit 1
11591169
fi
@@ -1193,6 +1203,9 @@ while :; do
11931203
fi
11941204

11951205
# Handled: park on the successor.
1206+
if ! fm_recovery_marker_handling_settled "$STATE/.watcher-down"; then
1207+
log_line "handling-settle-failed turn=$LAST_TURN successor=$SUCCESSOR_PID"
1208+
fi
11961209
ARM_PID=$SUCCESSOR_PID
11971210
ARM_OUT=$SUCCESSOR_OUT
11981211
SUCCESSOR_PID=

‎bin/fm-wake-lib.sh‎

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -718,12 +718,14 @@ _fm_recovery_marker_write_locked() {
718718
}
719719

720720
# Apply the downtime republication states owned by docs/watcher-continuity.md
721-
# while preserving an outstanding generation-bound acknowledgement.
721+
# while preserving an outstanding generation-bound acknowledgement. A handback
722+
# source republishes the current episode as already announced: the close it
723+
# hands to main is that episode's delivery.
722724
_fm_recovery_marker_publish() {
723725
local marker=$1 kind=${2:-downtime} bound=${3:-} source=${4:-watcher}
724726
local lock saved_token generation='' status=pending previous_append_token=''
725727
case "$kind" in handling|downtime) ;; *) return 1 ;; esac
726-
case "$source" in watcher|append) ;; *) return 1 ;; esac
728+
case "$source" in watcher|append|handback) ;; *) return 1 ;; esac
727729
if [ "$source" = append ]; then
728730
FM_WAKE_APPEND_RECOVERY_PREVIOUS_TOKEN=
729731
FM_WAKE_APPEND_RECOVERY_PUBLISHED_TOKEN=
@@ -757,14 +759,15 @@ _fm_recovery_marker_publish() {
757759
status=pending
758760
;;
759761
announced:downtime:*)
760-
if [ "$source" = watcher ]; then
762+
if [ "$source" != append ]; then
761763
generation=${FM_RECOVERY_MARKER_TOKEN##*:}
762764
status=announced
763765
fi
764766
;;
765767
esac
766768
fi
767769
FM_RECOVERY_MARKER_TOKEN=$saved_token
770+
[ "$source" != handback ] || status=announced
768771
fi
769772
if ! _fm_recovery_marker_write_locked "$marker" "$kind" "$generation" "$status"; then
770773
fm_lock_release "$lock"
@@ -861,6 +864,36 @@ fm_recovery_marker_snapshot() {
861864
fm_lock_release "$lock"
862865
}
863866

867+
_fm_recovery_marker_handling_settled() {
868+
local marker=$1 lock line
869+
lock="${marker}.lock"
870+
fm_lock_acquire_wait "$FM_WAKE_QUEUE_LOCK" || return 1
871+
if ! fm_lock_acquire_wait "$lock"; then
872+
fm_lock_release "$FM_WAKE_QUEUE_LOCK"
873+
return 1
874+
fi
875+
if [ -s "$FM_WAKE_QUEUE" ]; then
876+
if ! fm_recovery_marker_read "$marker"; then
877+
fm_lock_release "$lock"
878+
fm_lock_release "$FM_WAKE_QUEUE_LOCK"
879+
return 1
880+
fi
881+
line=$FM_RECOVERY_MARKER_TOKEN
882+
case "$line" in
883+
pending:handling:*|announced:handling:*)
884+
if ! _fm_recovery_marker_write_locked "$marker" downtime "${line##*:}"; then
885+
fm_lock_release "$lock"
886+
fm_lock_release "$FM_WAKE_QUEUE_LOCK"
887+
return 1
888+
fi
889+
;;
890+
pending:downtime:*|announced:downtime:*|acked:*) ;;
891+
esac
892+
fi
893+
fm_lock_release "$lock"
894+
fm_lock_release "$FM_WAKE_QUEUE_LOCK"
895+
}
896+
864897
_fm_recovery_marker_ack() {
865898
local marker=$1 expected_generation=$2 lock tmp line
866899
[ -n "$expected_generation" ] || return 2
@@ -1062,6 +1095,12 @@ fm_recovery_transition() {
10621095
reopen-announced)
10631096
_fm_recovery_marker_reopen_announced "$marker"
10641097
;;
1098+
handling-settled)
1099+
_fm_recovery_marker_handling_settled "$marker"
1100+
;;
1101+
handback)
1102+
_fm_recovery_marker_publish "$marker" downtime "" handback
1103+
;;
10651104
release-lock)
10661105
[ -n "$target" ] || return 1
10671106
_fm_recovery_marker_publish "$marker" "${value:-downtime}" "$bound" || return 1
@@ -1095,10 +1134,18 @@ fm_recovery_marker_publish() {
10951134
fm_recovery_transition "$1" publish "${2:-downtime}"
10961135
}
10971136

1137+
fm_recovery_marker_handback() {
1138+
fm_recovery_transition "$1" handback
1139+
}
1140+
10981141
fm_recovery_marker_ack() {
10991142
fm_recovery_transition "$1" acknowledge "$2"
11001143
}
11011144

1145+
fm_recovery_marker_handling_settled() {
1146+
fm_recovery_transition "$1" handling-settled
1147+
}
1148+
11021149
fm_recovery_marker_begin_handling() {
11031150
_fm_recovery_marker_begin_handling "$1" "${2:-}"
11041151
}

‎bin/fm-watch.sh‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3172,6 +3172,13 @@ done
31723172
if [ -n "$EVICTED_PID" ]; then
31733173
echo "watcher: replaced stalled pid $EVICTED_PID (beacon ${EVICTED_BEAT_AGE}s past hard bound ${WATCHER_STALL_BOUND}s)"
31743174
fi
3175+
handling_successor_has_queued_row() {
3176+
local queued=1
3177+
fm_lock_acquire_wait "$FM_WAKE_QUEUE_LOCK" || return 1
3178+
[ ! -s "$FM_WAKE_QUEUE" ] || queued=0
3179+
fm_lock_release "$FM_WAKE_QUEUE_LOCK"
3180+
return "$queued"
3181+
}
31753182
WATCHER_RECOVERY_PENDING=0
31763183
if [ -n "${FM_LOCK_RECOVERED_PID:-}" ]; then
31773184
WATCHER_RECOVERY_PENDING=1
@@ -3348,9 +3355,17 @@ rerecord_device_shifted_pr_poll() { # <id>
33483355
resurface_after_downtime() {
33493356
# Handling successors already have a predecessor-delivered wake on the way.
33503357
# Re-announcing from this cycle is what turned a lost handshake into an
3351-
# unbounded recovery loop; stay in the poll loop and supervise instead.
3358+
# unbounded recovery loop. A handled host turn explicitly settles its
3359+
# handoff after consuming the predecessor's rows, making any row left for
3360+
# main a recoverable downtime episode.
33523361
if [ "${FM_WATCH_HANDLING_SUCCESSOR:-0}" = 1 ]; then
3353-
return 0
3362+
if ! fm_recovery_marker_arm_check "$WATCHER_DOWNTIME_MARKER"; then
3363+
echo "watcher: recovery state could not be consumed safely" >&2
3364+
exit 1
3365+
fi
3366+
[ "$FM_RECOVERY_MARKER_ACTION" = recover ] || return 0
3367+
handling_successor_has_queued_row || return 0
3368+
wake "check: rearm-resurface"
33543369
fi
33553370
if [ "$WATCHER_RECOVERY_PENDING" -ne 1 ]; then
33563371
if ! fm_recovery_marker_arm_check "$WATCHER_DOWNTIME_MARKER"; then

‎docs/supervision-host.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,14 @@ A close the engine takes is handled as in [One wake](#one-wake), with the dialog
126126
A handled wake with only routine outcomes never reaches main.
127127
A handled wake that recorded a captain outcome while the captain is still attended exits with one `supervision-host: branch-outcome:` line naming its store rows, without the close it handled; see [Captain outcomes](#captain-outcomes).
128128
A turn that fails hands its close to main with one `supervision-host:` line, as away.
129-
Main-only rows that share the queue with the branch's rows stay queued for main, which is woken for each on its own triggering close, as on Pi.
129+
Main-only rows that share the queue with the branch's rows stay queued for main.
130+
The [handling-successor recovery contract](watcher-continuity.md#durable-queue-and-turn-end-backstop) guarantees that a main-only row appended after the predecessor closes, including before the successor initializes or while the host is parked, reaches main as a pass-through.
130131
The engine turn runs beside a captain who is present, so its guarded actions take the task leases that keep it and main off the same task.
131132

132133
### Away
133134

134135
Every close goes to the engine; captain outcomes remain in the store until the return drain presents them (see [Captain outcomes](#captain-outcomes)).
135-
Every turn that starts attended meets the attended rule again at its start, and the offer's scan is the scope the turn claims: a close accepted away whose turn starts attended, because the captain returned in between, or an attended close whose task turned main-only (a decision appeared) while the successor started, reaches main unchanged and leaves that successor cycle running, with the handoff that turn had confirmed handed back to downtime.
136+
Every turn that starts attended meets the attended rule again at its start, and the offer's scan is the scope the turn claims: a close accepted away whose turn starts attended, because the captain returned in between, or an attended close whose task turned main-only (a decision appeared) while the successor started, reaches main unchanged and leaves that successor cycle running, with the handoff that turn had confirmed handed back to downtime as already announced, so that successor does not announce the same close again.
136137
A captain who leaves while an attended turn runs turns its captain outcomes into away outcomes: they wait for the return too.
137138

138139
### Quiet mode
@@ -167,7 +168,8 @@ On each actionable close the engine takes, the host runs these steps:
167168
3. It runs one bounded engine turn with the branch prompt and the wake message carrying, attended, the dialog mirror and, away, the record's read-back.
168169
The engine drains, handles, reports through `bin/fm-branch-report.sh`, and acknowledges, exactly as the Pi branch does.
169170
4. It releases the branch's leases and grant, whether or not the wake was handled.
170-
5. It parks on the successor only for a handled wake.
171+
5. For a handled wake, it settles the handling-successor recovery transition and then parks on that successor.
172+
If settlement fails, it logs `handling-settle-failed` and still parks, leaving a later main-only pass-through to restore the handoff or deliver the restore failure to main.
171173
A main-only pass-through is not a park: the host exits after leaving that cycle running, as [Attended](#attended) describes.
172174

173175
The host counts the wake handled only when all three hold:

‎docs/watcher-continuity.md‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,12 @@ The bounded turn-end guard enforces recovery at Stop when no watcher is live and
193193
So a finished, hung, or identity-mismatched claim cannot suppress that recovery ([`turnend-guard.md`](turnend-guard.md#harness-integrations) owns that boundary).
194194

195195
The recovery-episode contract below owns once-per-generation announcement.
196-
A handling successor does not re-announce.
196+
A handling successor does not re-announce the predecessor-delivered wake while its handling handoff remains open.
197197
It enters its poll loop immediately and keeps scanning signals, stale panes, and checks.
198+
After the supervision host finishes a handled turn, it settles the successor handoff under the durable-queue and recovery-marker locks.
199+
Any row left for main then becomes a recoverable downtime episode, including one appended after the predecessor closed but before the successor initialized or the host confirmed its handoff.
200+
The successor closes once with the recovery wake only while that row is still queued; this is how a captain inbox note reaches main during handling-successor startup or while the host is parked on the handled wake's successor.
201+
When the host instead hands the accepted close back to main and leaves that successor running, it republishes the episode as announced downtime, because the close it hands back is that episode's delivery; the successor therefore does not announce it again, while a later durable append still opens a pending episode it recovers.
198202

199203
### Manual recovery and other harnesses
200204

@@ -230,7 +234,7 @@ Its ordinary arm check still announces a never-announced generation, including o
230234
### Generation reuse
231235

232236
An ordinary watcher close attempts to publish downtime, and every durable queue append publishes it.
233-
A handling successor closing to resurface recovery preserves the existing marker instead.
237+
A watcher closing to resurface the recovery it found at its start preserves the existing marker instead, while a handling successor resurfacing a later row publishes downtime like any close.
234238
If EXIT cleanup cannot acquire the downtime-marker lock within its bound, it retains the stale singleton for the next arm to publish the missing downtime before clearing that lock (see [Grace, beacon, and stop signals](#grace-beacon-and-stop-signals)).
235239
A downtime republication of a pending episode reuses its generation.
236240
A watcher close leaves an announced downtime episode announced, while a successful durable append opens a fresh pending generation so a live watcher can recover the new work.
@@ -468,6 +472,7 @@ They also prove that a legacy or handoff-phase watcher marker from an absent rep
468472

469473
- The once-per-generation announcement bound with the real Pi extension against a refused handling handshake.
470474
- A handling successor that must surface a real crew event instead of going blind.
475+
- A handling successor that resurfaces a row appended between its predecessor's close and its own startup handoff.
471476

472477
`tests/fm-watch-triage.test.sh` proves TERM stops a watcher blocked inside a poll's pane capture and still releases its lock and records an acknowledgeable stop.
473478
It also exercises a single TERM with a live foreign downtime-marker lock holder, retained stale singleton and subsequent arm-style recovery, including decimal `08` and zero `00` cleanup bounds.

‎tests/fm-backend-herdr-presentation-e2e.test.sh‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,15 @@ finish_concurrent_expected_abort() { # <id> <status> <stdout> <stderr>
443443
fi
444444
}
445445

446+
finish_concurrent_recovery() { # <id> <home> <status> <stdout> <stderr>
447+
local id=$1 home=$2 status=$3 out=$4 err=$5
448+
[ "$status" -ne 0 ] || return 0
449+
grep -F "presentation recovery could not acquire its session lock" "$err" >/dev/null 2>&1 \
450+
|| fail "concurrent recovery $id failed unexpectedly: $(cat "$err")"
451+
spawn_task "$id" "$home" "$RECOVERY_PROJECT_DIR" > "$out" 2> "$err" \
452+
|| fail "concurrent recovery $id retry failed after session-lock contention cleared: $(cat "$err")"
453+
}
454+
446455
spawn_secondmate_task() {
447456
local id=$1 home=$2
448457
FM_GATE_REFUSE_BYPASS=1 FM_SPAWN_NO_GUARD=1 FM_HOME="$HOME_DIR" FM_ROOT_OVERRIDE="$ROOT" \
@@ -1334,8 +1343,12 @@ spawn_task "$PRIMARY_WAVE_ID" "$HOME_DIR" "$RECOVERY_PROJECT_DIR" > "$TMP_ROOT/p
13341343
PRIMARY_WAVE_PID=$!
13351344
spawn_task "$BRAVO_WAVE_ID" "$SECOND_HOME_B" "$RECOVERY_PROJECT_DIR" > "$TMP_ROOT/bravo-wave-resume.out" 2> "$TMP_ROOT/bravo-wave-resume.err" &
13361345
BRAVO_WAVE_PID=$!
1337-
wait "$PRIMARY_WAVE_PID" || fail "concurrent primary recovery failed: $(cat "$TMP_ROOT/primary-wave-resume.err")"
1338-
wait "$BRAVO_WAVE_PID" || fail "concurrent secondmate recovery failed: $(cat "$TMP_ROOT/bravo-wave-resume.err")"
1346+
if wait "$PRIMARY_WAVE_PID"; then PRIMARY_WAVE_STATUS=0; else PRIMARY_WAVE_STATUS=$?; fi
1347+
if wait "$BRAVO_WAVE_PID"; then BRAVO_WAVE_STATUS=0; else BRAVO_WAVE_STATUS=$?; fi
1348+
finish_concurrent_recovery "$PRIMARY_WAVE_ID" "$HOME_DIR" "$PRIMARY_WAVE_STATUS" \
1349+
"$TMP_ROOT/primary-wave-resume.out" "$TMP_ROOT/primary-wave-resume.err"
1350+
finish_concurrent_recovery "$BRAVO_WAVE_ID" "$SECOND_HOME_B" "$BRAVO_WAVE_STATUS" \
1351+
"$TMP_ROOT/bravo-wave-resume.out" "$TMP_ROOT/bravo-wave-resume.err"
13391352
PRIMARY_WAVE_NEW_WT=$(remember_meta_worktree "$PRIMARY_WAVE_META")
13401353
BRAVO_WAVE_NEW_WT=$(remember_meta_worktree "$BRAVO_WAVE_META")
13411354
PRIMARY_WAVE_NEW_PANE=$(grep '^herdr_pane_id=' "$PRIMARY_WAVE_META" | cut -d= -f2-)

‎tests/fm-remote-secondmate-lifecycle-e2e.test.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,7 @@ FM_STATE_OVERRIDE="$WATCH_STATE" FM_SECONDMATE_LIVENESS_SECS=1 FM_POLL=1 \
13101310
> "$TMP_ROOT/watch-liveness.out" 2> "$TMP_ROOT/watch-liveness.err" &
13111311
watch_pid=$!
13121312
watch_wait=0
1313-
while kill -0 "$watch_pid" 2>/dev/null && [ "$watch_wait" -lt 1500 ]; do
1313+
while kill -0 "$watch_pid" 2>/dev/null && [ "$watch_wait" -lt 3000 ]; do
13141314
sleep 0.02
13151315
watch_wait=$((watch_wait + 1))
13161316
done

0 commit comments

Comments
 (0)