At 2:27am my MacBook Pro froze. Spinning wheel, nothing clicked, then it rebooted on its own. One of my coding agents was rendering a promo video for AEO Toolkit, our AI search visibility tool.
Its script started a new ffmpeg for every frame and never closed any of them. Two minutes in, 506 of them were running. Swap was at 36.4 of 36.9 GB. The Mac gave up. Below is what the machine recorded, the one line of Python that did it, and what I'd change.
What it looked like
I had nine agent sessions running in Ninebrains, our multi-agent desktop app, at once. One of them was cutting a video for AEO Toolkit. It had just launched its renderer, a 510-line Python script (film.py). The script draws each frame with Pillow, decodes B-roll clips with ffmpeg and pipes the result into a second ffmpeg that encodes the MP4.
| Time | Event |
|---|---|
| 02:22:56 | Agent runs film.py to render pic-v1.mp4 |
| ~02:25:08 | 506 ffmpeg processes, all children of one Python process; swap 36,427 of 36,864 MB used |
| ~02:26:07 | watchdogd makes its last check-in (derived: 93 s before the crash) |
| 02:27:40 | Mac crashes: watchdog timeout, no check-ins in 93 seconds |
| 02:27:41 | Hardware reset (boot faults: wdog) |
| ~02:29 | Back at the login screen; zero ffmpeg, swap empty |
What the machine recorded
About two and a half minutes before the crash I asked a second agent session why everything was slow. It grouped every process by name and added up the memory, and that gave the answer in one screen:
vm.swapusage: total = 36864.00M used = 36427.44M free = 436.56M
872 MB 12 claude
483 MB 506 ffmpeg <- 506 copies
142 MB 12 Notion
parents:
506 24811 (Python film.py)All 506 had the same parent and nearly the same command line: decode a1-switchboard.mp4 from second 0, scale to 1080x1080, write raw RGB frames to stdout. Each one showed under 1 MB resident because its memory had already been pushed to swap. Divide the swap by the process count and you get roughly 70 MB per stuck decoder. That's about right for a decoder holding a 1080p filter graph and frame buffers.
The crash report is short and clear:
panic(cpu 1 caller ...): watchdog timeout: no checkins from watchdogd in 93 seconds
Boot faults: wdog,reset_in_1 timeoutThis isn't a crash in the usual sense. The kernel was healthy enough to notice that a small system daemon hadn't run for a minute and a half, and it chose to reset instead of hang. On a Mac with swap used up, that's how it ends.
The one line
Every shot in the film is a function called once per frame, with a per-shot state dict. Shots that use video footage open their source like this:
def s01(t, d, st):
st.setdefault("src", Src(A("a1-switchboard.mp4"), zoom=(1.0, 1.08)))
...
class Src:
def __init__(self, path, start=0.0, zoom=(1.0, 1.06)):
...
self.p = subprocess.Popen(["ffmpeg", ..., "-i", path, ..., "-"],
stdout=subprocess.PIPE)It reads as "create the source once, then reuse it". It doesn't do that. Python evaluates function arguments before the call, so Src(...) runs, and starts an ffmpeg, on every single frame. setdefault then sees the key already exists and throws the new object away. At 30 frames a second that's up to 30 new decoders a second, for a shot that needs one.
Throwing it away should have been harmless. The object is garbage, its pipe closes, ffmpeg gets a broken pipe and exits. That's not what CPython does. When a Popen whose child is still running gets garbage collected, its __del__ adds it to an internal list (subprocess._active) so the child can be reaped later. That keeps the object alive, and the pipe with it. The ffmpeg fills the pipe buffer with the first slice of a frame, blocks on a write nobody will ever read, and sits there holding its memory.
I confirmed both halves on the same Python, using a harmless stand-in for ffmpeg:
st = {}
for frame in range(5):
st.setdefault("src", Src()) # Src() starts `yes` piped to us
# Src objects kept: 1 child processes alive: 5
# subprocess._active (resurrected Popens): 4Why the previews never caught it
The agent had already rendered dozens of still previews with the same code. Those runs only lasted seconds and rendered a few frames per shot. When the Python process exited, the pipes finally closed and every leaked ffmpeg died with it. The leak only becomes fatal in a full render that runs long enough to pile up hundreds.
The fix, and the guards that should have been there
The code fix is one line per call site. Only build the object when the key is missing.
if "src" not in st:
st["src"] = Src(A("a1-switchboard.mp4"), zoom=(1.0, 1.08))A script with a bug is normal, though. What bothers me is that nothing between the agent and the kernel noticed one process tree growing by four children a second for two minutes. Here's what should have been in place:
- Anything that owns a process closes it: A class that starts a subprocess gets an explicit close() that terminates and waits, and the render loop closes each shot's sources when the shot ends. A dropped Popen is not a stopped process.
- Lint for side effects inside setdefault and dict.get defaults: setdefault(key, SomeConstructor(...)) is almost always a bug when the constructor does I/O. It's easy to grep for, and an agent reviewing its own diff should flag it.
- Renders run with a process budget: Long jobs an agent launches should run under a ulimit on child processes. Then a runaway fan-out fails with an error after a few dozen, instead of taking the machine down after five hundred.
- The agent host watches memory, not just output: An agent waiting on a long command sees nothing until the command finishes. The host can see swap climbing. Past a threshold it should stop the newest heavy process tree and tell the agent why.
- When swap is at 99%, kill first and explain later: The session that diagnosed it found the culprit with about two and a half minutes to spare and reported it instead of ending it. On a machine that close to the edge, the first move is to kill the process tree. Write the explanation after.
Common questions
Does dict.setdefault evaluate its default argument every time?
Yes. setdefault is an ordinary method call, so Python evaluates every argument, including the default, before the call happens. If building the default is cheap, that doesn't matter. If it opens a file, starts a subprocess or makes a network call, it does that work every time, even when the key exists. Use if key not in d: d[key] = make() when the default has side effects.
Why didn't the discarded subprocesses exit on their own?
In CPython, garbage collecting a subprocess.Popen whose child is still running doesn't kill the child. Popen.__del__ puts the object on an internal list so the child can be reaped later, and that keeps its stdout pipe open. A child writing to that pipe fills the buffer and blocks forever, holding its memory. Always terminate and wait on a Popen you no longer need.
What does 'watchdog timeout: no checkins from watchdogd' mean on a Mac?
It's a deliberate crash. macOS expects the watchdogd daemon to check in regularly. If it can't, because the system is starved of memory or otherwise stuck, the kernel resets the machine after a timeout (93 seconds in this case) instead of hanging forever. When you see it, check memory pressure and swap usage first.
Sources
Related guides
The tool that was running
Ninebrains runs coding agents in parallel lanes, each in its own git worktree. It was the tool running when this happened, and the lesson applies to it too. A lane's processes cost the machine something, not only the output they print.
See Ninebrains
