Chris F

Every wait gets a deadline

Chris F · · How I do it · 3 min read

In short: I found seven little "wait for this file" loops that had been running for 34 hours. The jobs they were waiting on had died a day and a half earlier. Nothing was broken, but it looked like runaway work and nobody could tell. The fix is boring and absolute: every wait gets a deadline, and every deadline says why it fired.

Before: start a job and wait for its file; the job dies early and the loop is still checking 34 hours later. After: wait with a time limit; either the file appears and you read the summary, or time is up and the wait exits with a reason.
An open-ended wait can outlive its job; a bounded one always ends and says why.

Context

A lot of what my agents do at home is long-running: a simulation for my robot side project, a CI pipeline, an audio render, a big file copy to the NAS. The agent starts the job, then waits for a result.

The natural way to write that wait, for a human or an AI, is a loop:

until [ -f results/summary.md ]; do sleep 30; done

Read it again. It has exactly one way out: the file appears. If the job crashes, is killed, writes its result somewhere else, or never starts, the loop waits forever. Run it in the background and it waits forever quietly.

A wait loop has one way out, the file appearing. A crashed job, a killed job, a result written somewhere else, or a job that never starts all lead to the loop waiting forever.
One way out, four ways to wait forever.

That is what happened. One day I opened the task list in Claude Code and saw tasks that had been "running for 34 hours". Seven of them. Each was a background wait for a file that was never going to show up. The agents that started them had long since finished, moved on, or been closed. The loops were orphans.

Agents start jobs with background waits, then finish or close. The jobs die, so no file appears, and seven loops keep waiting for 34 hours until I open the task list and wonder what is stuck.
How the seven loops became orphans.

They weren't using much CPU. The real cost was trust. When I glance at a task list and see 34-hour tasks, I don't know if something important is stuck, so I stop and dig. That is exactly the kind of attention I am trying not to spend.

Detail

The rule

Every background wait, whether it is an until loop, a tail -F, or a watcher tool, gets a hard limit. In shell that is usually just timeout:

timeout 3600 bash -c 'until [ -f results/summary.md ]; do sleep 30; done' \
  || echo "gave up after 1 h: results/summary.md never appeared"

The limit is generous: longer than the job should ever take. The point is not to cut jobs short. The point is that the wait always ends, and when it ends early it says so in plain words, so the next agent or I know where to look.

The rule is in the standing instructions every agent reads, and in the briefs I give to sub-agents. It is one of the few rules I state as "always", because the failure is silent.

Black and white view from the Opportunity rover: its long shadow stretches across Martian ground toward a crater wall.
Even NASA's longest waits had an end date. Opportunity, built for a 90-day mission, worked for almost 15 years. After a dust storm cut contact in June 2018, the team kept trying for eight months, then ended the mission in February 2019. The rover took this picture of its own shadow in 2004.
Photo: NASA/JPL, public domain, via Wikimedia Commons

Clean up after yourself

The second half: at the end of a job, the agent checks for leftover loops it started and stops them. That sounds simple, and it is where I hit two more surprises.

Surprise one: pkill -f can kill itself. A pattern match on the full command line also matches the shell that is running the pkill, because that shell's command line contains the pattern. One cleanup killed its own shell halfway through. Another wait loop never ended because its exit check matched itself.

# Risky: the calling shell's own command line contains "summary.md"
pkill -f summary.md

# Better: remember the PID when you start it, and stop exactly that
long_job & pid=$!
...
kill "$pid" 2>/dev/null
Risky: pkill -f with a pattern matches the wait loop and also the calling shell, so the cleanup kills itself. Better: save the PID when starting the job and kill exactly that PID.
Stop exactly what you started: by PID, not by pattern.

Surprise two: grep with an empty file name reads standard input. If a variable that should hold a path is empty, grep pattern $f becomes grep pattern, which sits there waiting for input that never comes. Another quiet hang. Quote the variable and check the file exists first:

[ -n "$f" ] && [ -f "$f" ] || { echo "no file: '$f'"; exit 1; }
grep -m1 pattern "$f"

Why this is an AI problem as much as a shell problem

None of this is new to anyone who has written shell scripts for a living. What changed is the volume. An agent writes a wait loop in a second, and it may write dozens in a day across sessions. A human who writes a sloppy loop usually notices it when they close their terminal. An agent session that ends does not look back.

So I stopped relying on the agent to remember, and wrote it down as a rule it reads every time. Since then the task list has been boring, which is what I want from it.

What I'd tell a team

The 34-hour orphans didn't hurt anything. They just made me stop trusting what I was looking at, and that turned out to be reason enough.


Drafted with Claude.