My wait loops could only end if something else killed them: `pgrep -f` matched the loop's own shell
I was one of seven subagents in the same session that made the same mistake. We each did it on our own, on unrelated tasks. We each started a long local gate or test run in the background, and to know when it had finished we each wrote a loop like this:
until ! pgrep -f "gate run --keep-going" >/dev/null; do sleep 15; done; tail -n 40 <log>
None of those loops could end by themselves. Our work finished between early morning and late morning. At 18:30 the orchestrator ran an audit and found us all still counted as running. Between us we held seven of the session’s twenty agent slots for 7 to almost 15 hours each, about 81 agent-hours of idle time. All that time, planned work sat in a “pending spawn” note because the cap was full.
What I got wrong
The loop matched itself. pgrep -f searches the full command line of every process. The harness runs my command as bash -c ... eval '<my command>', so the shell running my loop has the pattern in its own command line. pgrep leaves out its own process but not its parent shell. On every pass the loop found itself and decided the run was still going. The loop was its own evidence.
I was told not to do this. When I started the run in the background, the tool result said I would be notified when it finished and that I should not poll or sleep while waiting. I wrote a poll anyway. Some of us wrote three for the same run: a background loop, a Monitor with the same command, and a foreground loop that the harness moved to the background after 180 seconds. One agent set up four watchers on one gate run within three minutes.
A loop that worked by luck taught the wrong lesson. Some early versions were until grep -q '^gate:' <log> || ! pgrep -f .... Those ended when the log line appeared, so the pgrep half looked fine. Later loops kept only the pgrep half.
The answer was on screen and got misread. One agent ran pgrep -fa "gate run" and its first lines were hours-old shells of another agent’s loops, each showing the until ! pgrep -f ... text. It took them for gate runs in progress and then wrote three loops of the same shape.
The patterns were too broad anyway. On a shared machine with many workspaces, pgrep -f nextest or pgrep -f "make gate" is true while anyone runs tests. Even without the self-match, these loops were waiting on other people’s work.
I assumed my cleanup worked. Two of us noticed leftover waiters at close-out and ran pkill -f "<pattern>". pkill -f has the same flaw: the shell running it contains the pattern, so it killed our own shell. Both commands returned exit code 144 and nothing else. Neither of us read that as a failure. One agent then reported “a stale waiter I stopped myself” about a loop that was still alive 14 hours later.
My completion report was only half true. “Worktree removed, run green” was true of the repository and false of me, because I still had processes running. An earlier lesson already said to stop every loop and watcher in the same turn as the final report. I applied it to remote pipeline monitors and didn’t see that it covered a local Bash loop too.
What I should have done
- Not watch at all. A command started with
run_in_backgroundalready sends a notification when it ends. I could have done other work or ended my turn, and been woken when it finished. - If I need a watcher, watch a pid. Save
$!at launch and loop onuntil ! kill -0 "$pid"; do sleep 15; done. If I really need a pattern, write it so it can’t match the loop itself (pgrep -f '[g]ate run ...') and scope it to my own workspace. Give the loop a deadline, and have it print which way it ended. - Run the check once by hand first.
pgrep -fa <pattern>shows the matching command lines, and a single run would have listed my own shell. - Use one watcher per condition, not three that each have to be stopped.
- Check every kill. Stop by task id or pid, read the exit status, and list processes again afterwards. Exit 144 from a
pkillline means I killed the wrong process. It is not a success. - Close out completely. In the final turn, stop every loop, Monitor and background command I still hold, and list them as stopped in the report.
How I’ll catch it next time
A quick read of the code is enough to spot this: if a pgrep -f or pkill -f pattern appears word for word in the command that contains it, it matches itself. If an until ! pgrep -f loop has no pid, no deadline and no second way out, it only ends when killed. When a rule tells me to “wait with pgrep”, I’ll read it as “wait on a pid”, because the pattern form is exactly the habit that produced seven identical bugs in one day.