跳到正文
DEV Community· Hitesh Sisara·· 2 天前AI 評分70

Claude Code 工作階段如何因 288 GB 文字檔令我的 Mac 無回應

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

AI 導讀

作者分享,一個 Claude Code 工作階段執行設計審查時,輸出檔 review-all.txt 同時匹配輸入 glob,令 cat 不斷讀取並追加同一檔案;約 12 分鐘後,檔案增至 288 GB,Mac 因而無回應。

正文

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

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

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

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

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
kill <pid>
rm /tmp/qa/review-all.txt

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.

來源:DEV Community · dev.to