Repository navigation
Conversation
…n land inside them The watcher's remote secondmate liveness probe and pending-reply observation called fm-on.sh with no hard time limit, and fm-on.sh set no ConnectTimeout, so a peer that stopped answering (after host sleep, or accepting TCP but never sending a banner) could hold the poll well past a minute. Both calls were also captured with $(...), and bash 3.2 defers a fatal TERM while a command substitution is being read, so evicting a holder stuck there failed after the 5 s wait and the re-arm reported "inspect or stop". - fm-on.sh adds ConnectTimeout (FM_SSH_CONNECT_TIMEOUT, default 15) and BatchMode=yes, validated like FM_SSH_ALIVE_*. - fm-timeout-lib.sh gains fm_run_timed_capture: fm_run_timed in the background with stdout captured through a file and an interruptible wait. - Liveness state/route reads are bounded by FM_SECONDMATE_PROBE_TIMEOUT (default 45); hitting the bound is unknown state with the route preserved, never relaunchable. - Pending-reply remote observe is bounded by FM_PENDING_REPLY_OBSERVE_TIMEOUT (default 20); hitting the bound observes unknown. - A holder that has its TERM but has not exited yet is refused with its own message instead of "inspect or stop".
|
The current PR head is 0648c4f The same remote-probe and eviction behaviour is integrated locally on branch fm/fm-pi-wake-connection-finish and was proven on three routes: a local isolated home, a TCP peer that accepts but never answers, and the real Tailscale SSH route named supercomputer. Relevant recorded commands and results from the evidence:
The integrated branch is available at https://github.com/mehulbhagwani/firstmate/tree/fm/fm-pi-wake-connection-finish |
|
Suggested regression test for the timeout implementation, captured from the integrated fork branch (not proposed as a competing PR): test_remote_poll_probe_timeout_preserves_route() {
local w out started elapsed
w=$(make_remote_probe_world probe-timeout)
started=$(date +%s)
out=$(probe_remote "$w" poll FM_SECONDMATE_PROBE_TIMEOUT=1 FM_FAKE_SLEEP=5)
elapsed=$(( $(date +%s) - started ))
[ "$out" = 'skipped|unknown|0|||remote endpoint probe timed out after 1s on lab-host; route preserved' ] \
|| fail "a bounded remote probe must preserve the route after timeout, got: $out"
[ "$elapsed" -lt 4 ] || fail "a timed-out remote probe exceeded its bound: ${elapsed}s"
pass "poll probe: a silent remote peer is bounded and never relaunchable"
}On the integrated fork branch, |
Intent
Beheben der zwei Watcher-Schwaechen aus dem Untersuchungsbericht data/watcher-remote-hang/report.md (Abschnitte 8.3 und 8.4), Variante (b): klein bauen und upstream an kunchenguid/firstmate beitragen.
Befund (Kurzfassung): Die Watcher-"Haenger" dieser Home waren macOS-Systemschlaf; zwei echte Schwaechen verschaerfen das Symptom: (1) Remote-Aufrufe im Watcher-Zyklus (Secondmate-Liveness-Probe, Pending-Reply-observe, optional Remote-crew-state) haben kein hartes Zeitlimit, und bin/fm-on.sh setzt kein ConnectTimeout/BatchMode - nach dem Aufwachen haengt der Zyklus bis ~1 min, bei echten Netzproblemen theoretisch viel laenger. (2) Die Abloesung eines festgefahrenen Watchers (evict_stalled_holder) wartet nach TERM nur 5 s; bash 3.2 stellt TERM zurueck, solange der Watcher in einer Kommandosubstitution $(...) steckt (so ist der Remote-Probe aufgerufen), daher scheitert die Abloesung sichtbar und der Arm meldet "inspect or stop".
Inhalt der referenzierten Berichtsabschnitte (Substanz des Auftrags):
Messdaten des Berichts: Ueber sieben Tage fielen 33 von 33 Watcher-Ereignissen mit Beacon-Alter >= 300 s in macOS-Schlafphasen (Wachanteil hoechstens 79 s). Ein Minimaltest mit /bin/bash 3.2.57 zeigte: TERM waehrend x=$(sleep 8) wirkt erst nach Rueckkehr der Substitution (t=8 s statt t=1 s), waehrend
sleep 8im Vordergrund undsleep 8 & wait $!das TERM sofort wirken lassen. Im beobachteten Fall kam das Ablöse-TERM an, wirkte aber erst ~20 s spaeter; der Arm hatte nach 5 s schon "inspect or stop" gemeldet. Ein lebender Server mit voller Remote-Job-Queue kann einen einzelnen Probe bis ~12,5 min festhalten, und vor der Authentifizierung greift ServerAlive gar nicht (ssh -G zeigt connecttimeout none).8.3 Harte Zeitlimits fuer Remote-Aufrufe im Zyklus: (1) bin/fm-on.sh bekommt ConnectTimeout (FM_SSH_CONNECT_TIMEOUT, Default 15 s; deckt TCP-Connect und Banner/KEX ab, wo ServerAlive nicht greift) und BatchMode=yes (keine interaktive Rueckfrage in einem Hintergrundprozess), mit Validierung wie die vorhandenen FM_SSH_ALIVE_*-Werte. (2) Der Liveness-Probe (state, und route im Session-Start-Modus) laeuft mit hartem Zeitlimit (FM_SECONDMATE_PROBE_TIMEOUT, Default 45 s); eine Zeitueberschreitung ist wie ssh-Exit 255 "unbekannt, Route bleibt", niemals "tot" oder relaunch-berechtigt. (3) Pending-Reply-observe ebenso mit Zeitlimit (FM_PENDING_REPLY_OBSERVE_TIMEOUT, Default 20 s); Zeitueberschreitung ergibt "unknown". Das Zeitlimit laeuft ueber fm_run_timed, nicht ueber fm_exec_timed (das ist unter bash 3.2 wegen $BASHPID derzeit kaputt, Upstream-Issue #5847). Optional laut Bericht: (4) dasselbe fuer die Remote-state-Abfrage in fm-crew-state.sh und (5) ein Zyklus-Budget fuer Remote-Arbeit.
8.4 Abloesung korrigieren: (1) Im Watcher die Remote-Aufrufe nicht als $(...) ausfuehren, sondern im Hintergrund mit Ausgabe in eine Datei und wait, damit ein TERM sofort wirkt (bei nativem TERM-Verhalten beendet bash sofort und laeuft ueber den EXIT-Trap). (2) Optional laenger nach TERM warten. (3) Die Meldung nach bereits gesendetem TERM unterscheiden, statt dieselbe "inspect or stop"-Meldung wie ohne TERM zu drucken.
Schlafbewusstes Beacon-Alter (Berichtsabschnitt 8.2) gehoert nicht dazu; das deckt der offene Upstream-PR #5768 ab.
What Changed
bin/fm-on.shnow passesBatchMode=yesandConnectTimeoutto SSH.ConnectTimeoutcomes fromFM_SSH_CONNECT_TIMEOUT, defaults to 15 s, and is validated like the existingFM_SSH_ALIVE_*values. A host that accepts the connection but never answers, or a login that would need a prompt, now fails quickly instead of hanging.fm_run_timed_capturehelper inbin/fm-timeout-lib.shruns a command throughfm_run_timedin the background and puts its stdout inFM_TIMED_CAPTURE. Because the watcher blocks inwaitinstead of inside$(...), a TERM takes effect at once, including under bash 3.2. The secondmate liveness probe uses it for its remotestate/routecalls, bounded byFM_SECONDMATE_PROBE_TIMEOUT(default 45 s). If the bound is hit, the state is treated as unknown and the route is kept, never dead. Pending-replyobservealso uses it, bounded byFM_PENDING_REPLY_OBSERVE_TIMEOUT(default 20 s); hitting that bound givesunknown.fm-watch.shrecords which pid it sent TERM to while evicting a stalled holder. If that same pid still holds the lock after the short wait, the arm now prints "sent TERM to stalled pid … but it has not exited yet" instead of the generic "inspect or stop" message.docs/configuration.md,docs/remote-secondmates.mdanddocs/turnend-guard.mddocument the new settings and this behavior. Tests cover each change.Risk Assessment
✅ Low: The fix-round change now gates the sent-TERM refusal on the exact pid that was TERMed. Any other live stale holder falls through to the prior 'inspect or stop' path. The rest of the change is bounded and covered by behavioral tests with no source-grep assertions: remote probe and observe timeouts that preserve the route and report unknown rather than dead, wait-based capture so TERM lands at once, and ConnectTimeout plus BatchMode in fm-on.sh with validation.
Testing
I ran the targeted test files for the changed libraries: fm-on, secondmate-liveness, pending-reply, the watcher-lock eviction cases, and the new fm_run_timed_capture cases. All pass under /bin/bash 3.2. The full fm-timeout-lib file still aborts on the known, already-failing fm_exec_timed case (#5847), which fails the same way on base. I then drove the product live in throwaway marked lab homes with real OpenSSH and a TCP peer that accepts but never answers: - fm-on ConnectTimeout: gives up after about 3.5s with exit 255. Base hangs until an outer 20s alarm kills it. - Effective ssh options:ssh -Gshows batchmode yes and connecttimeout 15 (or the override). Base shows batchmode no and connecttimeout none. - Invalid values: bad FM_SSH_CONNECT_TIMEOUT values are refused before ssh starts. - Watcher-cycle liveness probe: returns unknown with the route preserved after 3s, and the route meta is unchanged. - Stalled watcher: a live watcher stuck inside a real ssh probe is evicted and replaced within 0.5s of the re-arm. On base the same regression test is refused with "inspect or stop". - Refusal messages: a holder that ignores TERM gets the new "sent TERM … not exited yet" refusal. A rival watcher that was never signalled gets the ordinary refusal on HEAD but the wrong "sent TERM" message on the pre-fix commit. The change has no UI, so the artifacts are CLI transcripts. Three scenarios were not driven live: the session-start route read timeout, TERM delivery during fm_run_timed_capture, and the pending-reply observe timeout. The last two have unit-test coverage only.Evidence: Live lab transcript (fm-on connect timeout HEAD vs base, ssh -G options, invalid values, probe timeout, live eviction, sent-TERM refusal)
Source: Live lab transcript (fm-on connect timeout HEAD vs base, ssh -G options, invalid values, probe timeout, live eviction, sent-TERM refusal)
Evidence: Live lab driver script
Source: Live lab driver script
Evidence: Rival-after-eviction adversarial run on HEAD (its pre-fix section is invalid due to a /var path mismatch; see the corrected pre-fix file)
Source: Rival-after-eviction adversarial run on HEAD (its pre-fix section is invalid due to a /var path mismatch; see the corrected pre-fix file)
Evidence: Rival-after-eviction on pre-review-fix 12e3ddc (shows the wrong 'sent TERM' message to a rival never signalled)
Source: Rival-after-eviction on pre-review-fix 12e3ddc (shows the wrong 'sent TERM' message to a rival never signalled)
Evidence: Rival-after-eviction driver script
Source: Rival-after-eviction driver script
Evidence: Base fba81cb fails the stalled-in-remote-probe eviction regression
Source: Base fba81cb fails the stalled-in-remote-probe eviction regression
not ok - a holder stalled in its remote probe was not replaced (holder=57546): watcher: lock held by live pid 57546 but heartbeat is stale for 843835158s (>1s); inspect or stop that watcher before re-arming.Evidence: Watcher-lock eviction cases on HEAD
Source: Watcher-lock eviction cases on HEAD
Evidence: fm_run_timed_capture cases under /bin/bash 3.2
Source: fm_run_timed_capture cases under /bin/bash 3.2
Evidence: fm-on / liveness / pending-reply test logs
Source: fm-on / liveness / pending-reply test logs
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
🔧 **Review** - 1 issue found → auto-fixed ✅
bin/fm-watch.sh:2397- The new "sent TERM to stalled pid ... but it has not exited yet" refusal is gated only on EVICT_TERM_SENT, which stays 1 after a successful eviction. Here is a sequence where it fires wrongly. Arm X TERMs stalled holder A, A exits, evict_stalled_holder succeeds (EVICTED_PID=A), and the loop continues. Meanwhile rival arm B takes the lock. B only touches .last-watcher-beat at line 2641, after its startup work, so X re-reads a live holder B with a beacon that is still stale. X skips eviction because EVICTED_PID is set, then prints "watcher: sent TERM to stalled pid B ... but it has not exited yet". X never signalled B, and B is a healthy new watcher. Two arms racing like this is plausible after a machine wakes from sleep, which is the scenario this change targets. Fix: record the pid that was TERMed (e.g. EVICT_TERM_PID=$pid inside evict_stalled_holder) and print this message only when it equals FM_LOCK_HELD_PID and EVICTED_PID is empty. Otherwise keep the old path, or treat B as an already-running watcher.🔧 Fix applied.
✅ Re-checked - no issues remain.
bin/fm-secondmate-liveness-lib.sh:158- In full (bootstrap) mode the probe first runs fm_remote_readiness_ensure: up to threefm-on.sh ... fm-remote-doctor.shcalls as plain $(...) substitutions with no FM_SECONDMATE_PROBE_TIMEOUT bound. Live, against a silent peer with FM_SSH_CONNECT_TIMEOUT=120, the full probe took about 120s. It is still bounded by the new ConnectTimeout (default 15s per call). The watcher cycle uses poll mode only, so this is outside the stated intent, which covers the watcher cycle.tests/fm-timeout-lib.test.sh:52- On macOS bash 3.2 the first case (fm_exec_timed status passthrough) fails and aborts the file before the new fm_run_timed_capture cases run. This is the known upstream fm-timeout-lib.sh: bare $BASHPID breaks fm-spawn on stock macOS Bash 3.2 #5847 ($BASHPID) and it fails the same way on base fba81cb. I ran the new capture cases on their own and they pass./bin/bash live-lab-drive.sh <worktree> <base>(evidence dir): real ssh against a silent peer. Coversfm-on.shConnectTimeout (HEAD vs base), the effective options fromssh -G(BatchMode/ConnectTimeout), invalid FM_SSH_CONNECT_TIMEOUT values, the liveness probe with FM_SECONDMATE_PROBE_TIMEOUT=3, a live watcher stalled in a real ssh probe evicted by a re-arm, and the sent-TERM refusal for a holder that ignores TERM/bin/bash live-lab-rival-after-evict.shon HEAD and on 12e3ddc (pre-review-fix): arm X evicts holder A, rival B takes the lock, and X's refusal message is checkedbash tests/fm-watcher-lock.test.shrestricted to test_live_stale_watch_lock_is_actionable, test_live_stalled_watch_lock_is_replaced_past_hard_bound, test_stalled_holder_inside_remote_probe_is_replaced and test_holder_still_finishing_after_term_gets_its_own_refusal (HEAD)the same test_stalled_holder_inside_remote_probe_is_replaced against a base fba81cb checkout (reproduces the reported failure)bash tests/fm-on.test.shbash tests/fm-secondmate-liveness.test.shbash tests/fm-pending-reply.test.shbash tests/fm-timeout-lib.test.sh(aborts on the known #5847 case, which also fails on base), then the four new fm_run_timed_capture cases run on their own under /bin/bash 3.2✅ **Document** - passed
✅ No issues found.
🔧 Fix applied.
1 warning still open:
✅ **Push** - passed
✅ No issues found.
Pre-existing failures (unrelated to this change)
On macOS with the system
/bin/bash3.2.57 these tests fail identically on basefba81cb2and on this branch, run one at a time:tests/fm-watcher-lock.test.shtest_watch_restart_rejects_reused_pid: "restart did not surface recovery after replacing a reused-pid lock".tests/fm-watch-arm.test.shtest_rearm_resurfaces_durable_queue_and_remote_open_decision: "re-arm stayed live instead of surfacing durable wakes and the still-open remote decision".tests/fm-timeout-lib.test.shfirst case, thefm_exec_timedstatus pass-through under bash 3.2 (fm-timeout-lib.sh: bare $BASHPID breaks fm-spawn on stock macOS Bash 3.2 #5847). The newfm_run_timed_capturecases in that file pass when run on their own.This change does not touch any of those code paths, and it deliberately uses
fm_run_timed, notfm_exec_timed.