SWE-AF icon indicating copy to clipboard operation
SWE-AF copied to clipboard

Coder agents wait with a self-matching `pgrep -f` loop, so the wait spins until agent_timeout_seconds and the retry repeats finished work

Open i2020github opened this issue 1 month ago • 0 comments

Summary

Coder agents sometimes wait for a long-running script with a pgrep -f loop. The loop matches its own shell, so it never ends. It spins until agent_timeout_seconds, even though the script finished long before. The timeout then triggers the issue advisor and a retry coder. That coder repeats the work, here a 40-minute real-stack test run, although the first run's result was already in the worktree.

Seen twice in one build (two regression-run issues) on 2026-10-23.

What the agent did

  1. It started the runner as a background Bash command (Claude Code's run_in_background):
    bash scripts/m2-run.sh > /tmp/m2run.log 2>&1; echo exit $? >> /tmp/m2run.log
    
  2. In a second Bash command, it waited for the runner to exit:
    until ! pgrep -f "scripts/m2-run.sh" >/dev/null; do sleep 20; done; tail -15 /tmp/m2run.log
    

The Bash tool runs step 2 as /bin/bash -c '... until ! pgrep -f "scripts/m2-run.sh" ...'. pgrep excludes only itself, not its parent shell. That shell's command line contains the pattern, so pgrep -f always finds a match. The coder's own claude process often matches as well, because its --system-prompt argument can contain the script path.

In one case the agent started step 2 twice, so two identical loops were spinning.

Evidence (one build, max_concurrent_issues: 1, agent_timeout_seconds: 7200)

Issue Runner finished OK Wait loop ended Result
regress-m3 07:14 UTC 08:31 UTC (agent timeout) issue advisor, then evidence committed at 08:32
regress-m2 10:05 UTC ~11:24 UTC (agent timeout) issue advisor, evidence committed at 11:25, then a retry coder started a full re-run of the same 40-min script

At each hang, pgrep -af "scripts/m2-run.sh" listed only the coder's claude process and its own waiting shell(s). No runner process and no containers existed.

Impact

  • About 1h15m of idle wall-clock per occurrence, on top of the run itself.
  • A retry can repeat an expensive real-environment run whose result was already produced.
  • Nothing in the logs says the wait is stuck. From outside it looks like a long test run.

Suggested fixes (any one helps)

  1. Coder/QA prompt guidance. To wait for a background job, poll for a sentinel the job writes, for example the exit N line the agent already appends, or record $! and use kill -0 $PID. Never use pgrep -f <pattern>, which matches the waiting shell itself. Better still, run scripts that finish within the agent's time budget in the foreground.
  2. Timeout recovery. Before the advisor or a retry re-runs work, check the worktree for uncommitted results and finished log files from the timed-out attempt, and offer to commit them instead of redoing the work.
  3. Stall detection (optional). If a coder's only activity for N minutes is the same polling command, surface it as a probable stuck wait rather than waiting for the full agent_timeout_seconds.

Workaround we use

A small watchdog, run outside SWE-AF, ends any waiting shell whose command line contains until ! pgrep -f "scripts/<name>.sh" when no real bash .../scripts/<name>.sh process exists. The agent gets its Bash call back and carries on.

Environment

SWE-AF fork based on upstream 4d1f11c (Merge #152), runtime claude_code, Claude Code 2.1.292, models claude-opus-5-5 (coder) and claude-sonnet-5-5 (git), Ubuntu 26.04, rootless Docker. Related: #159, #162.

i2020github avatar Oct 23 '26 11:10 i2020github