Boternal
Boternal Field Notes · Issue #4 · 2026-11-28

The machine that got switched off mid-job

Several agents share one cloud VM, and each is told to stop it when done. Without a lease, "done" is a guess, and we guessed wrong in both directions.

Welcome back to Boternal Field Notes. Every issue is one thing that broke in a real system we run with AI agents, how we found it, and what we changed.

This one is about a cloud machine shared by several agents, and the question each of them has to answer before switching it off: is anyone else still using this?

The symptom: the box disappears, or never goes away

We run long-running data jobs on a shared cloud VM. It bills by the hour, so our agents follow a standing rule: never leave the machine running with nothing to do. When an agent's job finishes, the agent stops the machine.

That rule broke in two opposite ways.

The machine vanished under live work. An agent was partway through a multi-step task when its next ssh timed out, and the cloud API said the machine was stopped. Another time, an agent was running a read-only 43.7-million-row count when a different agent session stopped the machine in the middle of it.

The machine stayed on with nothing to do. In the worst case, a batch job finished at 05:17 UTC and the machine sat idle until 12:23, about seven hours, because the agent meant to stop it had gone quiet.

What the incidents had in common

What happened Result
A 30-minute lease, outlived by the real work Stopped while in use
Work covered only by a "don't auto-stop" flag file Stopped by another session
Manual leases of 12 h and 24 h on jobs that ended early Would have pinned it on into the next day
A finished job and a dormant agent Idle for ~7 hours

Every failure was some process deciding whether the machine was busy, and deciding wrong.

The cause: "busy" was being guessed

Our first idea of busy was CPU load: if the load is high, someone's working, so leave the machine alone. It sounds reasonable, and it's wrong both ways.

High CPU doesn't mean someone else is working. A job that ends with "stop the machine if it's idle" checks the load right after its own work finishes. The five-minute load average takes about nine minutes to decay, so the job sees its own leftover load and decides someone else is busy. Ours logged exactly that, with no other job running:

BUSY: CPU busy (load1=9.38 load5=16.94 ncpu=32
      per-core=0.529 threshold=0.100)

The first workaround was a 12 × 60-second retry loop that outwaited the decay. It worked, but it burned up to 12 minutes of a 32-core machine on every run.

Low CPU doesn't mean nothing is running. A process in uninterruptible sleep (the D state, waiting on disk) uses roughly 0% CPU. One of our jobs sat in that state for 8 to 12 minutes after logging that its output was written, while the kernel flushed the files. By CPU it was idle. Kill it then and you lose the write.

CPU can't tell a stuck job from a quiet one, or your own leftover load from someone else's work. So we stopped asking it.

The fix: a lease is the only "busy" signal

Every job on the machine now holds a lease: a small record in a registry on the host saying who is using the machine, for what, and until when. Every automated stop checks the registry and nothing else:

That flips what a lease means. It isn't just coordination; it's what keeps the machine alive. A job with no lease will be powered off underneath it.

Agents never take a lease by hand for a long job. They launch it through a wrapper that takes the lease, heartbeats it, and releases it on exit, even on failure or Ctrl+C:

with_lease.sh <job-id> -- bash -lc '<command>'

The details took several incidents to get right:

Heartbeat, so the lease can't lapse mid-job. The wrapper renews at TTL/2, so a 30-minute TTL protects a six-hour job as well as an eight-hour one. An early version discarded a failed renewal, ended the heartbeat loop and reported success. The lease then went stale while the job was still running. Failed beats are now logged loudly.

Size the TTL to the whole job when nothing heartbeats it. A lease taken by hand has no heartbeat. The default 1,800 seconds is meant for a quick pull. One agent used it for work that included several checks of about 2.5 minutes each plus edit-and-test cycles. The lease ran out and the watchdog stopped the machine mid-task. For hand-held leases the rule is now 3,600 seconds, on the assumption the job will outrun your estimate, and one lease for the whole session, not one per command. Release between commands and nothing holds the machine while the agent is thinking.

Under the wrapper, the TTL is the orphan window. With heartbeats, the TTL no longer bounds how long the lease lives. It bounds how long a dead wrapper's lease survives. Our 12 h and 24 h manual leases weren't insurance. They set how long the machine could sit idle and billing with every stop refused. Under the wrapper we keep the default and never raise it.

Release checks the owner. A lease records who took it, and a release from a different identity is refused, so one agent's cleanup can't drop another agent's protection. The lease also records the job's PID. If that process is gone, the release goes through without --force, so recovery never means overriding the guard.

SIGTERM goes to the wrapper, never SIGKILL

At first the wrapper ran its job in the foreground. Bash defers its traps while a foreground command runs, so when the job wedged in D state, SIGTERM to the wrapper did nothing. The only way out was SIGKILL, which skips the trap entirely, so the lease was orphaned for its full TTL.

Now the wrapper puts the job in its own process group and waits on it. SIGTERM is handled at once and forwarded to the whole group. After a 30-second grace period the lease is released whether or not the job died, and any survivor is reported. SIGKILL is now the only way to orphan a lease, so we never send it. We deliberately did not add "drop the lease when the job looks stalled": near-zero CPU can't tell a wedge from a quiet job, and guessing invites the watchdog to power off the machine mid-write.

There's a catch in the other direction. We once sent SIGTERM to a wrapper's PID. The lease was released and the wrapper exited, but its job and 32 worker processes kept running, reparented to PID 1, and kept making progress with no lease at all. The log showed only released lease; the warning meant for exactly this never printed. That's the dangerous orphan: not a stale lease blocking a stop, but a live job any stop would kill. So signal the group and count survivors:

kill -TERM -<pgid>      # the minus sign means the whole group
ps -eo pid,pgid,args | awk '$2==<pgid>' | wc -l   # expect 0

Why it took so long to see

The dormant agent that never stopped the machine

Now the seven idle hours. The agent was counting on its own session to stop the machine when the job finished. But our agent sessions pause after about 5 to 15 minutes without user activity, and a background task completing doesn't wake a paused session. The job finished, the completion went to a session that wasn't listening, and nothing stopped the machine.

A waiter that lives in the agent's session is only as reliable as the agent being awake. So we moved the stop into the job itself. Every unattended job now ends with a guarded self-stop:

# idle = no active lease except my own; CPU ignored
if host_is_idle --no-cpu --ignore-lease "$JOB"; then
  sudo shutdown -h now    # last one out
fi

The job always tries, even when it knows another agent's job is running. If the other job is still going, its lease refuses the stop and nothing is lost. If the other job finished first, this one turns off the lights. "Last one out stops the machine" only works if every job tries, and the watchdog backstops any job that never gets that far.

One loose end: a self-stop strands its own lease, because the release trap doesn't survive shutdown. The next stop sees it and refuses with exit 4, which reads exactly like another agent's job. That's what the PID check is for.

The part that matters for agents

A human operator remembers having started a job on a shared machine. An agent remembers only what it's told, and may not be awake when the job ends. Every failure here was an agent making a sensible-sounding guess. CPU looks busy, so leave it on. A 24-hour lease is safer than a short one. SSH failed, so it's the network. I checked the result by hand, so I'm done.

That last one bit us on its own. A background waiter polled for a "done" file over ssh and treated every ssh failure as "not yet." The agent checked the file by hand, reported the result, and stopped the machine without cancelling the waiter, which then spun against a dead host for 3 hours 40 minutes. Cleanup is now one sequence: release the lease, cancel the waiters, stop the machine.

As in issue #1, the fix shipped in layers:

  1. The mechanism. A lease registry, a wrapper that heartbeats and releases, a guarded stop, an idle watchdog, and jobs that stop the machine themselves.
  2. No silent success. --no-cpu on every automated stop, exit 5 instead of a reassuring exit 0 when a release can't find its lease, and a loud line for every failed heartbeat.
  3. Standing rules the agents load every session. Lease every host command that runs longer than a few seconds, read-only counts included. A "don't auto-stop" flag file is not a lease: it blocks the watchdog, not another session's stop. Anything you relaunch outside the original wrapper needs its own lease. And never SIGKILL a wrapper.

If you only take one thing

Don't let anything that can switch a machine off decide "busy" from CPU. Give every job a lease, and make every stop check only the lease.

Next issue: the brief is the cost lever. Most of our agents' tokens went to re-reading context, and here's what we changed.

Reply to this email if you've hit something like this. We read every reply.


Get the next one by email: subscribe to Boternal Field Notes.