DEV Community

Hitesh Sisara
Hitesh Sisara

Posted on Fully Autonomous

How a Claude Code session froze my Mac with a 288 GB text file

My Mac suddenly became unresponsive this morning. The disk was almost full, and one text file in /tmp had grown to 288 GB in about 12 minutes.

I run a lot of Claude Code sessions at once, so the first question was which one, if any, was writing it.

Finding the writer

lsof lists the processes that have a file open:

lsof /tmp/qa/review-all.txt
Enter fullscreen mode Exit fullscreen mode

It showed a cat and a grep. Walking up the parent chain with ps -o pid,ppid,command -p <pid> led from both of them to shells started by the same Claude Code session.

The loop

That session was running a design review over a set of pages. Each review wrote a small text file of findings, and the last step combined them into one file:

for f in /tmp/qa/review-*.txt; do
  echo "=== $(basename "$f")"
  cat "$f"
done > /tmp/qa/review-all.txt
Enter fullscreen mode Exit fullscreen mode

The output file is review-all.txt, and that name also matches review-*.txt.

The redirect at the end applies to the whole loop, so the shell creates review-all.txt before it expands the glob. The new, empty output file lands in the list of inputs. When the loop reaches it, cat starts reading the file it is appending to. Every block it reads is written back onto the end, so the end of the file moves away as fast as cat reads, and cat never gets there.

The one-line version has the same trap, with a delay:

cat /tmp/qa/review-*.txt > /tmp/qa/review-all.txt
Enter fullscreen mode Exit fullscreen mode

Here the glob is expanded before the redirect creates the file, so the first run is fine. On the second run the old review-all.txt is one of the inputs, and the loop starts. Agents rerun commands all the time.

Why Linux would have stopped it

I ran the same loop on a Linux machine with GNU coreutils and got this:

cat: review-all.txt: input file is output file
Enter fullscreen mode Exit fullscreen mode

GNU cat checks whether an input is the same file as its output and skips it. The cat that ships with macOS comes from BSD and has no such check, so it keeps going until a write fails. Mine stopped on its own after 12 minutes, probably when a write failed for lack of space. By then the file held 288 GB, about 400 MB for every second it ran, and the disk was 96% full.

How the agent made it worse

The command ran into the agent's 10-minute command timeout, so the tool moved it to the background and the agent went on with other work. Two more full reads of the file followed:

  • The original command ended with a grep -c over the combined file to count the clean pages. That grep had to read all 288 GB.
  • A few minutes later the agent checked progress with cat review-all.txt | wc -c, which reads every byte again just to count them.

Both were still running when I found the file. Neither changed it, but together they kept the disk busy reading hundreds of gigabytes. A person in a hurry could have written every one of these commands.

Cleaning up

Stop anything that still has the file open before you delete it. Deleting a file only removes its name; the space comes back when the last process holding it closes.

lsof /tmp/qa/review-all.txt
Enter fullscreen mode Exit fullscreen mode
kill <pid>
Enter fullscreen mode Exit fullscreen mode
rm /tmp/qa/review-all.txt
Enter fullscreen mode Exit fullscreen mode

Free space went from 37 GiB back to 306 GiB.

Avoiding it

  • Write the combined file somewhere the glob can't match, such as another folder or another extension (> /tmp/qa/combined.md). Or skip it explicitly at the top of the loop: [ "$f" = /tmp/qa/review-all.txt ] && continue.
  • To see how big a file is, use ls -l or stat. They read metadata. cat file | wc -c reads the whole file. This is worth putting in the instructions you give your agents.
  • Put a ceiling on file size in the shells your agents run. ulimit -f kills a process with SIGXFSZ when a file it writes passes the limit, and a generous limit still stops a runaway file long before the disk fills. The units differ between shells: zsh counts 512-byte blocks and bash counts 1 KB blocks, so ulimit -f 200 means 100 KB in zsh and 200 KB in bash.

Top comments (1)

Collapse
 
acemeridian profile image
Ace Meridian •

I ran your loop on Linux to see how far "Linux would have stopped it" goes. It holds for cat (here Ubuntu's uutils cat 0.10.0 has the same check as GNU) and for GNU grep 3.12. It doesn't hold for most other things an agent might put in the loop body. Three 64 KiB review files, the output inside the glob, ulimit -f 20480 and timeout 5 around each run (stderr trimmed):

cat "$f"               rc=0    0.02s size=196691    err=cat: review-all.txt: input file is output file
sed "s/^/- /" "$f"     rc=153  3.39s size=20971520  err=File size limit exceeded
awk 1 "$f"             rc=153  3.53s size=20971520  err=File size limit exceeded
grep -v "^$" "$f"      rc=0    0.01s size=196694    err=grep: review-all.txt: input file is also the output
tail -n +1 "$f"        rc=153  3.36s size=20971520  err=File size limit exceeded
Enter fullscreen mode Exit fullscreen mode

So the cat check is a property of cat, not of Linux. An agent that "tidies" the combine step to sed 's/^/- /' gets your Mac behaviour back. tail -n +1 loops even with one-line review files. sed and awk only loop once the inputs are bigger than their read buffer, so a small test fixture won't show it. Two runs gave the same numbers.

Your ulimit -f advice works as you describe: 20480 in bash stopped at exactly 20 MiB, exit 153 (SIGXFSZ).

One catch with the skip line: [ "$f" = /tmp/qa/review-all.txt ] only matches if the glob is spelled the same way. With for f in ./review-*.txt, the absolute-path test never matched and the loop ran to the cap. Comparing the files themselves works however the path is written:

[ "$f" -ef /tmp/qa/review-all.txt ] && continue
Enter fullscreen mode Exit fullscreen mode

That stopped cleanly at 218 KB in the same setup. Did the agent write the original loop itself, or was it a step from a skill/prompt you gave it?