Repository navigation
test: stop the remote secondmate e2e watcher before temp-root cleanup - #5845
kunchenguid merged 3 commits into
Conversation
The remote secondmate lifecycle e2e backgrounded fm-watch.sh through the remote_env shell function, so $! named the function's subshell rather than the watcher. Killing that subshell left the unreachable-leg watcher running, and its one-second liveness probe kept invoking the fake ssh, which rewrites ssh.count in the temp root. When a probe landed while the EXIT trap was removing the root, rm failed with "Directory not empty" after every assertion had passed. Exec the watcher from the backgrounded function so the recorded pid is the watcher itself, and assert the stopped watcher stops probing and writing its state. Cleanup also stops a watcher left running by a failed assertion and removes the root through fm_test_remove_tree, so a run that fails before retirement does not strand the read-only spawn hooks directory. Closes kunchenguid#5836
|
Speaking as Kun's firstmate: Verdict: Whole thread + tip vs main Tip vs main: Only contract-class: restore — concrete existing e2e cleanup path was broken/flaky; tip restores reliable fixture teardown. VISION.md (each rule)
Attestation: MATCH. CI: CLEAN SUCCESS ( |
|
Speaking as Kun's firstmate: this is merged. Thank you @aminry — really appreciate you taking the time on this. |
…kunchenguid#5845) * test: stop the leaked unreachable watcher before remote e2e cleanup The remote secondmate lifecycle e2e backgrounded fm-watch.sh through the remote_env shell function, so $! named the function's subshell rather than the watcher. Killing that subshell left the unreachable-leg watcher running, and its one-second liveness probe kept invoking the fake ssh, which rewrites ssh.count in the temp root. When a probe landed while the EXIT trap was removing the root, rm failed with "Directory not empty" after every assertion had passed. Exec the watcher from the backgrounded function so the recorded pid is the watcher itself, and assert the stopped watcher stops probing and writing its state. Cleanup also stops a watcher left running by a failed assertion and removes the root through fm_test_remove_tree, so a run that fails before retirement does not strand the read-only spawn hooks directory. Closes kunchenguid#5836 * no-mistakes(review): Clear reaped watcher PIDs and restore plain temp-root removal * no-mistakes(review): Let in-flight probe settle before stopped-watcher baseline
…kunchenguid#5845) * test: stop the leaked unreachable watcher before remote e2e cleanup The remote secondmate lifecycle e2e backgrounded fm-watch.sh through the remote_env shell function, so $! named the function's subshell rather than the watcher. Killing that subshell left the unreachable-leg watcher running, and its one-second liveness probe kept invoking the fake ssh, which rewrites ssh.count in the temp root. When a probe landed while the EXIT trap was removing the root, rm failed with "Directory not empty" after every assertion had passed. Exec the watcher from the backgrounded function so the recorded pid is the watcher itself, and assert the stopped watcher stops probing and writing its state. Cleanup also stops a watcher left running by a failed assertion and removes the root through fm_test_remove_tree, so a run that fails before retirement does not strand the read-only spawn hooks directory. Closes kunchenguid#5836 * no-mistakes(review): Clear reaped watcher PIDs and restore plain temp-root removal * no-mistakes(review): Let in-flight probe settle before stopped-watcher baseline
…kunchenguid#5845) * test: stop the leaked unreachable watcher before remote e2e cleanup The remote secondmate lifecycle e2e backgrounded fm-watch.sh through the remote_env shell function, so $! named the function's subshell rather than the watcher. Killing that subshell left the unreachable-leg watcher running, and its one-second liveness probe kept invoking the fake ssh, which rewrites ssh.count in the temp root. When a probe landed while the EXIT trap was removing the root, rm failed with "Directory not empty" after every assertion had passed. Exec the watcher from the backgrounded function so the recorded pid is the watcher itself, and assert the stopped watcher stops probing and writing its state. Cleanup also stops a watcher left running by a failed assertion and removes the root through fm_test_remove_tree, so a run that fails before retirement does not strand the read-only spawn hooks directory. Closes kunchenguid#5836 * no-mistakes(review): Clear reaped watcher PIDs and restore plain temp-root removal * no-mistakes(review): Let in-flight probe settle before stopped-watcher baseline
…kunchenguid#5845) * test: stop the leaked unreachable watcher before remote e2e cleanup The remote secondmate lifecycle e2e backgrounded fm-watch.sh through the remote_env shell function, so $! named the function's subshell rather than the watcher. Killing that subshell left the unreachable-leg watcher running, and its one-second liveness probe kept invoking the fake ssh, which rewrites ssh.count in the temp root. When a probe landed while the EXIT trap was removing the root, rm failed with "Directory not empty" after every assertion had passed. Exec the watcher from the backgrounded function so the recorded pid is the watcher itself, and assert the stopped watcher stops probing and writing its state. Cleanup also stops a watcher left running by a failed assertion and removes the root through fm_test_remove_tree, so a run that fails before retirement does not strand the read-only spawn hooks directory. Closes kunchenguid#5836 * no-mistakes(review): Clear reaped watcher PIDs and restore plain temp-root removal * no-mistakes(review): Let in-flight probe settle before stopped-watcher baseline
Intent
Fix the flaky cleanup in tests/fm-remote-secondmate-lifecycle-e2e.test.sh, filed upstream as #5836.
The test passes every assertion, then exits 1 because its EXIT-trap cleanup
rm -rf -- "$TMP_ROOT"fails with 'Directory not empty': a fixture-owned background process is still writing into the temp root while it is removed. It failed this way in 4 of 217 main CI runs since 2026-09-10 (runs 36211248815, 36196302018, 36105826991, 35139828921) and on PR 4380 (run 36189713738). The issue proposes quiescing the fixture's background writers (stop and wait them) before removing the temp root, keeping the change inside this test's fixture with no behavior change outside tests.What Changed
tests/fm-remote-secondmate-lifecycle-e2e.test.shnow starts bothfm-watch.shlegs throughremote_env exec. That makes$!the watcher process itself rather than a wrapping subshell, sokillreaches the process that writes into the fixture root.cleanupkills and waits for any watcher still recorded inwatch_pidbefore it runsrm -rfon$TMP_ROOT. This covers a watcher leg that a failed assertion cut short. Each leg clearswatch_pidonce its watcher has been reaped.Risk Assessment
✅ Low: The change only touches one test fixture. It makes $! point at the watcher itself by exec-ing it through remote_env, which still passes the prefix env vars through exec. It clears the reaped watcher PIDs, lets an in-flight probe settle before taking the baseline, and stops a watcher that a failed assertion left running before the original rm -rf. The unreachable leg starts no detached home-summary or reconcile children, because home-summary.json is pre-touched and no requests are queued, so the new stopped-watcher checks have no other writer to race against.
Testing
I ran the real e2e test four times under isolated TMPDIRs, each followed by a check for leaked processes and leftover temp roots. The fixed test passed twice with exit 0, no leftover temp root, and zero leaked processes. The injected-failure variant exited 1 as intended with zero leaked processes, which shows the EXIT trap stopped the live watcher. Its temp root was not removed because the read-only spawn hooks dir blocked rm, which is the out-of-scope case the user chose to leave alone. The base copy happened to pass once, which fits the ~2% flake rate. The no-exec variant exited 1, but my log keeps only the last 8 lines, so the failing assertion was not captured and that scenario is untested. No Herdr lab or harness primary was needed because this change is test-only. All temporary test copies and temp dirs were removed.
Evidence: Fixed test run 1 transcript
Source: Fixed test run 1 transcript
Evidence: Fixed test run 2 with post-exit leak check
Source: Fixed test run 2 with post-exit leak check
Evidence: Injected mid-watcher failure: cleanup stops watcher (0 leaked processes)
Source: Injected mid-watcher failure: cleanup stops watcher (0 leaked processes)
Evidence: Base (pre-fix) run with leak check
Source: Base (pre-fix) run with leak check
Evidence: Fixed test with exec reverted (failing assertion truncated)
Source: Fixed test with exec reverted (failing assertion truncated)
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
🔧 **Review** - 2 issues found → auto-fixed (2) ✅
tests/fm-remote-secondmate-lifecycle-e2e.test.sh:38- The new cleanup block kills$watch_pidwhenever it is set, but neither watcher leg clears it after its ownwait(lines ~1265 and ~1311). On a normal passing run,watch_pidstill holds the unreachable watcher's PID, which was already reaped. When the EXIT trap runs, it sends SIGTERM to that stale PID. If the PID has been recycled in the meantime (the suite spawns many processes, and macOS wraps at 99999), the signal hits an unrelated process owned by the same user. Fix: runwatch_pid=''right after each leg's successfulwait. The cleanup block then only fires for a watcher that a failed assertion left running.tests/fm-remote-secondmate-lifecycle-e2e.test.sh:52- The cleanup now callsfm_test_remove_tree(chmod every directory, thenrm -rf) instead ofrm -rf -- "$TMP_ROOT". The stated reason is a read-only spawn hooks dir left by runs that fail before retirement. That is a separate failure mode from the issue's 'Directory not empty' race, which comes from a background writer. The race is fixed byexec-ing the watcher and stopping/waiting it. No requirement in the intent needs this swap. Recommend removing it from this change, or confirm that it is intentionally in scope.🔧 Fix applied.
1 warning still open:
tests/fm-remote-secondmate-lifecycle-e2e.test.sh:1318- The new check that the stopped watcher does no more probing captures its baseline (ssh_after) right afterwaitreturns. That baseline can be off by one. fm-watch.sh runssecondmate_liveness_tick->fm_secondmate_liveness_probein its main shell, and TERM keeps bash's default disposition (watcher_stop_signals). So a SIGTERM that arrives after the watcher forksfake-ssh, but beforefake-sshruns its first two lines (count=$(cat ...),printf ... > $FM_FAKE_SSH_COUNT), kills the watcher and leaves that child running. The child then bumpsssh.countafterssh_afteris read, and the check[ "$(cat "$SSH_COUNT")" = "$ssh_after" ]fails with 'the stopped unreachable watcher kept probing' on an otherwise correct run. The watcher probes once per 1s cycle with FM_POLL=1, and each probe opens a window of a few milliseconds (bash startup plus acatfork), so this adds a new low-rate flake of the same kind the change is meant to remove. Fix: let the leftover child settle before taking the baseline. For example, afterwatch_pid='',sleep 1, then read the baseline count and touch the.stoppedmarker, thensleep 3and compare. A real surviving watcher still keeps probing and fails the check.🔧 Fix applied.
✅ Re-checked - no issues remain.
✅ **Test** - passed
✅ No issues found.
bash tests/fm-remote-secondmate-lifecycle-e2e.test.sh(fixed, run 1, ~6 min): ALL TESTS PASSED, exit 0, no fm-remote-secondmate-e2e temp root leftTMPDIR=<isolated> bash tests/fm-remote-secondmate-lifecycle-e2e.test.sh(fixed, run 2, parallel under load), then 5s laterps -Efor processes carrying the fixture root andlsof TMPDIRBase-commit copy of the test (git show 503ba82:tests/fm-remote-secondmate-lifecycle-e2e.test.sh) run under an isolated TMPDIR with the same leak checksTemporary copy of the fixed test withfailinjected 3s after the unreachable watcher starts, run under an isolated TMPDIR with the same leak checksTemporary copy of the fixed test with onlyexecreverted, to check that the new stopped-watcher guard catches a surviving watcher✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.
Closes #5836