Skip to content

test: re-check reply listener ownership while awaiting capture - #6241

Open
guanchengh-lgtm wants to merge 1 commit into
kunchenguid:mainfrom
guanchengh-lgtm:fm/fm-remote-reply-flake
Open

guanchengh-lgtm wants to merge 1 commit into
kunchenguid:mainfrom
guanchengh-lgtm:fm/fm-remote-reply-flake

Conversation

@guanchengh-lgtm

@guanchengh-lgtm guanchengh-lgtm commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Intent

Context: the firstmate CI job "Behavior portable serial 4" failed on an open pull request that does not touch the test, with "not ok - the NUL-bearing line was not captured" in tests/fm-remote-reply.test.sh, after the 40-second await_reply_result wait (800 polls of 0.05 s). The same test failed on an unrelated upstream branch (CI run 36703475299, "not ok - the header-collision line was not captured"). Two local runs pass. A failed job cannot be re-run from here because that needs admin rights on the repository, so the flake blocks every firstmate pull request that hits it.

What Changed

  • In tests/fm-remote-reply.test.sh, await_reply_result no longer checks reply ownership once before the wait. It now re-reads reply_owner every 20th poll (about once a second) inside the existing 800-poll loop. It starts fm-procevent.sh start only when no listener owns the source and no starter launched by this wait is still running. A listener that reads as live and then exits as superseded no longer leaves the source without a listener for the whole 40-second wait.
  • When the wait expires, the helper now prints a diagnostic to stderr with the result file name, the current owner, and the PID of the listener this wait launched (or none), then returns 1 as before.

Risk Assessment

✅ Low: The change edits only one test helper. It replaces a single ownership sample with a re-check about once per second, guarded by a live-starter check. This closes the traced race where a manual handle re-arm supersedes a listener that still reads as live, and I found no path where it masks a real failure or double-launches a listener.

Testing

I ran the changed test file end to end once. Then I ran 5 concurrent copies of both the base and the changed version on a loaded 28-CPU host. All 11 runs passed. The natural flake did not reproduce at base, so I built probes from the real test prefix (generations 1-3) that replay the reported race against the real fm-procevent runner. In the forced race, base waited 142s and never captured the line, while the change captured it in 7s. When the listener was killed mid-wait, base failed after 136s and the change recovered in 7s. A healthy listener was not launched twice. When capture was impossible, the wait timed out with the new diagnostic and launched at most once a second. The probes emulate the race window - they do not hit it naturally. Natural reproduction of the flake at base is untested. No visual surface applies, because the change is to a shell test only.

  • Live validation: ✅ go - 5 of 6 scenarios driven live against the product
Scenario Result Live Evidence
Run the changed fm-remote-reply test end to end: every generation is captured and the test prints ALL TESTS PASSED ✅ pass live bash tests/fm-remote-reply.test.sh rc=0; also 5 of 5 concurrent copies exit=0 (stress-head-*.log)
Reported race: the listener is gone but the first ownership sample reads live. The wait must launch a listener and capture the line ✅ pass live race-probe-head.log: captured after 7s. Base (race-probe-base.log): NOT captured after 142s. The race window is injected, not natural
Adversarial: the listener dies mid-wait after this wait launched it. The wait must relaunch and capture a later line ✅ pass live mode-killed-head.log: rc=0 after 7s, 2 starts. mode-killed-base.log: rc=1 after 136s, 1 start
Adversarial: a healthy listener must not get a second listener from the periodic ownership re-read ✅ pass live mode-steady-head.log: rc=0, 1 start, 1 distinct claim pid across a 5s wait with 4 periodic re-reads
Failure path: no capture is possible. The wait must stay bounded and print the timeout diagnostic ✅ pass live mode-timeout-head.log: rc=1, 40 launches over 800 polls (at most one per second), stderr 'await_reply_result: remote-reply-ios.4.result was not applied within the wait (owner=none, listener launched b…
Natural reproduction of the flake at base under host load ⏸️ untested no The flake is a rare timing race. It did not fire in 5 loaded base runs (all exit=0, as were the 5 changed runs), so the run did not establish a natural reproduction at base. Only the injected-race pro…
Evidence: Base vs change comparison across all probes and the stress run

Source: Base vs change comparison across all probes and the stress run

fm-remote-reply await_reply_result: base 65c75b0d vs change 93ae5875

== Injected race: listener gone, first ownership sample reads 'live' ==
base:   PROBE-RESULT: NOT captured after 142s
change: PROBE-RESULT: captured after 7s

== Listener killed mid-wait, line arrives later ==
base:   PROBE-RC: 1 after 136s PROBE-STARTS: 1 
change: PROBE-RC: 0 after 7s PROBE-STARTS: 2 

== Healthy listener, line arrives after 4s (no double launch) ==
change: PROBE-RC: 0 after 5s PROBE-STARTS: 1 PROBE-DISTINCT-CLAIM-PIDS: 1 

== Capture impossible (reads fail): timeout diagnostic and launch cadence ==
PROBE-RC: 1 after 146s
PROBE-STARTS: 40
PROBE-STDERR: await_reply_result: remote-reply-ios.4.result was not applied within the wait (owner=none, listener launched by this wait=37955)

== 5 concurrent full-test copies each, loaded 28-cpu host ==
base: 5 of 5 exit=0
head: 5 of 5 exit=0
Evidence: Injected race, base: not captured after 142s

Source: Injected race, base: not captured after 142s

ok - a blocking non-destructive remote delta reaches durable process-event capture
ok - a captured delta is applied, acknowledged, and re-armed without a handler
ok - ingest appends one validated line, fetches its document, and advances the cursor
ok - replayed capture has one deduplicated append and one durable handling identity
ok - later generations cannot invalidate an unacknowledged ingested result
PROBE-RESULT: NOT captured after 142s
exit=1
Evidence: Injected race, change: captured after 7s

Source: Injected race, change: captured after 7s

ok - a blocking non-destructive remote delta reaches durable process-event capture
ok - a captured delta is applied, acknowledged, and re-armed without a handler
ok - ingest appends one validated line, fetches its document, and advances the cursor
ok - replayed capture has one deduplicated append and one durable handling identity
ok - later generations cannot invalidate an unacknowledged ingested result
PROBE-RESULT: captured after 7s
exit=0
Evidence: Listener killed mid-wait, base fails

Source: Listener killed mid-wait, base fails

ok - a blocking non-destructive remote delta reaches durable process-event capture
ok - a captured delta is applied, acknowledged, and re-armed without a handler
ok - ingest appends one validated line, fetches its document, and advances the cursor
ok - replayed capture has one deduplicated append and one durable handling identity
ok - later generations cannot invalidate an unacknowledged ingested result
PROBE-MODE: killed
PROBE-RC: 1 after 136s
PROBE-STARTS: 1
PROBE-DISTINCT-CLAIM-PIDS: 1
PROBE-STDERR: 
exit=1
Evidence: Listener killed mid-wait, change recovers

Source: Listener killed mid-wait, change recovers

ok - a blocking non-destructive remote delta reaches durable process-event capture
ok - a captured delta is applied, acknowledged, and re-armed without a handler
ok - ingest appends one validated line, fetches its document, and advances the cursor
ok - replayed capture has one deduplicated append and one durable handling identity
ok - later generations cannot invalidate an unacknowledged ingested result
PROBE-MODE: killed
PROBE-RC: 0 after 7s
PROBE-STARTS: 2
PROBE-DISTINCT-CLAIM-PIDS: 2
PROBE-STDERR: 
exit=0
Evidence: Healthy listener, no double launch

Source: Healthy listener, no double launch

ok - a blocking non-destructive remote delta reaches durable process-event capture
ok - a captured delta is applied, acknowledged, and re-armed without a handler
ok - ingest appends one validated line, fetches its document, and advances the cursor
ok - replayed capture has one deduplicated append and one durable handling identity
ok - later generations cannot invalidate an unacknowledged ingested result
PROBE-MODE: steady
PROBE-RC: 0 after 5s
PROBE-STARTS: 1
PROBE-DISTINCT-CLAIM-PIDS: 1
PROBE-STDERR: 
exit=0
Evidence: Timeout diagnostic and bounded relaunch cadence

Source: Timeout diagnostic and bounded relaunch cadence

ok - a blocking non-destructive remote delta reaches durable process-event capture
ok - a captured delta is applied, acknowledged, and re-armed without a handler
ok - ingest appends one validated line, fetches its document, and advances the cursor
ok - replayed capture has one deduplicated append and one durable handling identity
ok - later generations cannot invalidate an unacknowledged ingested result
PROBE-MODE: timeout
PROBE-RC: 1 after 146s
PROBE-STARTS: 40
PROBE-DISTINCT-CLAIM-PIDS: 40
PROBE-STDERR: await_reply_result: remote-reply-ios.4.result was not applied within the wait (owner=none, listener launched by this wait=37955)
exit=1
Evidence: Probe generators and stress driver

Source: Probe generators and stress driver

#!/usr/bin/env bash
# Builds race-probe-{base,head}.sh from the test file at each revision.
set -eu
W=$1 EV=$2
for pair in "base:65c75b0dab02f2bd6c293cf21a20f712dfac16bd" "head:93ae5875dd9f4206eed3a1b43370763b86c55c1e"; do
  name=${pair%%:*} rev=${pair#*:}
  out="$EV/race-probe-$name.sh"
  git -C "$W" show "${rev}:tests/fm-remote-reply.test.sh" > "$out.full"
  cut=$(grep -n '^pass "later generations cannot invalidate an unacknowledged ingested result"' "$out.full" | cut -d: -f1)
  head -n "$cut" "$out.full" \
    | sed -e 's#^\. "\$(dirname "\${BASH_SOURCE\[0\]}")/lib.sh"#. "'"$W"'/tests/lib.sh"#' \
          -e 's#^ROOT=.*#ROOT='"$W"'#' > "$out"
  cat >> "$out" <<'EOF'

# ---- injected race (probe only) -------------------------------------------
# The listener is gone, but the FIRST ownership sample still reads "live": the
# window in which a listener has adopted the previous registration and is about
# to exit as superseded. Later samples tell the truth.
stop_reply_listener || fail "probe: listener did not stop"
RACE_FLAG="$TMP_ROOT/race-first-sample-done"
eval "real_$(declare -f reply_owner)"
reply_owner() {
  if [ ! -e "$RACE_FLAG" ]; then : > "$RACE_FLAG"; printf 'live\n'; return 0; fi
  real_reply_owner
}
printf 'working [key=race-probe]: injected race generation\n' >> "$REMOTE/state/parent-replies.status"
T0=$(date +%s)
if await_reply_result "$PARENT/state/procevent-inbox/$SID.4.result"; then
  echo "PROBE-RESULT: captured after $(( $(date +%s) - T0 ))s"
  exit 0
else
  echo "PROBE-RESULT: NOT captured after $(( $(date +%s) - T0 ))s"
  exit 1
fi
EOF
  rm -f "$out.full"
  chmod +x "$out"
done

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

⚠️ **Review** - 1 warning
  • ⚠️ tests/fm-remote-reply.test.sh:121 - Simplification: the new timeout diagnostic is not required by the intent. The intent requires the flaky wait to stop failing. It does not ask for extra failure output. The added printf ... &gt;&amp;2 at the end of await_reply_result also costs one more fm-procevent.sh list spawn on every timeout. The re-sampling loop already covers the flake, including the launch-then-superseded case. The narrower form is to drop the trailing printf and its reply_owner call, and keep the loop only. The added stderr text is harmless, so the remedy is a scope decision for the author.
✅ **Test** - passed

✅ No issues found.

  • Live validation: ✅ go - 5 of 6 scenarios driven live against the product
Scenario Result Live Evidence
Run the changed fm-remote-reply test end to end: every generation is captured and the test prints ALL TESTS PASSED ✅ pass live bash tests/fm-remote-reply.test.sh rc=0; also 5 of 5 concurrent copies exit=0 (stress-head-*.log)
Reported race: the listener is gone but the first ownership sample reads live. The wait must launch a listener and capture the line ✅ pass live race-probe-head.log: captured after 7s. Base (race-probe-base.log): NOT captured after 142s. The race window is injected, not natural
Adversarial: the listener dies mid-wait after this wait launched it. The wait must relaunch and capture a later line ✅ pass live mode-killed-head.log: rc=0 after 7s, 2 starts. mode-killed-base.log: rc=1 after 136s, 1 start
Adversarial: a healthy listener must not get a second listener from the periodic ownership re-read ✅ pass live mode-steady-head.log: rc=0, 1 start, 1 distinct claim pid across a 5s wait with 4 periodic re-reads
Failure path: no capture is possible. The wait must stay bounded and print the timeout diagnostic ✅ pass live mode-timeout-head.log: rc=1, 40 launches over 800 polls (at most one per second), stderr 'await_reply_result: remote-reply-ios.4.result was not applied within the wait (owner=none, listener launched b…
Natural reproduction of the flake at base under host load ⏸️ untested no The flake is a rare timing race. It did not fire in 5 loaded base runs (all exit=0, as were the 5 changed runs), so the run did not establish a natural reproduction at base. Only the injected-race pro…
  • bash tests/fm-remote-reply.test.sh at 93ae5875 (rc=0, ALL TESTS PASSED)
  • bash evidence/stress.sh &lt;worktree&gt; &lt;evidence&gt; 5: 5 concurrent full copies of the base test and 5 of the changed test (10 of 10 exit=0)
  • race-probe-base.sh and race-probe-head.sh: real test prefix, listener stopped, first reply_owner sample forced to live, then await_reply_result for generation 4
  • mode-probe-{base,head}.sh with PROBE_MODE=steady (healthy listener, line arrives after 4s)
  • mode-probe-{base,head}.sh with PROBE_MODE=killed (listener killed at 2.5s, line arrives at 6s)
  • mode-probe-head.sh with PROBE_MODE=timeout (FM_REMOTE_REPLY_FAIL_READ=1, so no capture is possible)
⚠️ **Document** - 1 info
  • ℹ️ tests/fm-remote-secondmate-lifecycle-e2e.test.sh:296 - This file has its own copy of await_reply_result and reply_owner. It still uses the old one-sample ownership check that this change replaced in tests/fm-remote-reply.test.sh. It can fail in the same way. A follow-up could move one shared helper into tests/lib.sh. This is a test change, so the document phase did not edit it.
✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

await_reply_result in tests/fm-remote-reply.test.sh sampled listener
ownership once. The test's own manual `handle` re-arms the source, and a
listener that had already adopted the previous registration then exits as
superseded at its next launch check. A sample taken before that exit read
`live`, so the helper started nothing and the whole 40-second wait ran
with no listener.

The helper now re-reads ownership on a bounded cadence (every 20 polls)
and starts a listener when none owns the source, with at most one launch
in flight per wait. The 800-poll bound is unchanged, so a capture that
never arrives still fails. On that failure the helper prints the owner
state and whether the wait launched a listener.

Evidence:
- 13 of 378 completed CI runs failed in this test since
  2026-09-27T23:30Z (3.4%): 8 "the NUL-bearing line was not captured",
  5 "the header-collision line was not captured". Each shows a constant
  41-second gap after the previous ok line.
- With a delay of 0.5 s or more injected before the listener's launch
  check in a scratch clone: unpatched 0 of 8 runs pass, patched 12 of 12.
- The failure was not reproduced at natural timing locally, so the link
  to the CI failures is an inference from the assertion text, the
  constant gap, and the injected-delay result.

Acceptance check after merge: 0 failures of this test in the next 150
completed ci.yml runs. At the previous 3.4% rate the chance of 0 in 150
by luck is 0.5%. Any failure of this test in that window reopens the
diagnosis.

Test code only. No product change.
@greptile-apps

greptile-apps Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 4/5

[Low risk] Test script refactors reply listener polling logic.

The PR appears safe to merge, though a deterministic regression test would protect the flake fix.

Reviews (1) · Last reviewed commit: "fix(tests): re-read reply listener owner..."

Comment on lines +114 to +117
if [ $((poll % 20)) -eq 0 ] && ! kill -0 "$starter" 2>/dev/null \
&& [ "$(reply_owner)" != live ]; then
remote_env "$ROOT/bin/fm-procevent.sh" start "$SID" >/dev/null 2>&1 &
starter=$!

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Retry path lacks regression coverage The new re-check fixes a rare case where a listener disappears after the first ownership sample, but the committed tests do not force that transition. Ordinary runs can therefore pass even if a later change removes the retry. A deterministic test of that transition would protect this fix.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@dimasmjunior

Copy link
Copy Markdown

Another data point for this one. #6970 hit the same failure in "Behavior portable serial 4": not ok - the header-collision line was not captured at tests/fm-remote-reply.test.sh:374 (job). That PR doesn't touch the remote-reply code, and the identical failure on its previous head passed when the job was re-run. The wait in await_reply_result checks for a live listener only once before polling, so a listener that exits during the 40-second window is never restarted. That matches what this PR fixes. Merging it would unblock #6970 too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants