The symptom: everything breaks at once
When one project fails, you look at that project. When every project fails at the same moment with errors about opening files, the problem is almost never in the code. It is in the machine. That was the first useful clue.
My setup runs several AI coding agents. Some of them start MCP servers — small helper programs that give the agent extra tools — automatically, in the background. You do not see them in the Dock, and you do not think about them until one misbehaves.
The wrong turn: ulimit -n
"Too many open files" sends most people, including me, straight to ulimit -n. On my machine it returned 1,048,576. That is the per-process limit, and it was nowhere near being hit by any one build.
The real limit was a different one. macOS also has a system-wide cap on open files shared by every process:
kern.maxfiles— the system-wide maximum (122,880 on my machine).kern.num_files— how many are open right now (68,642 when the failures started).
One process with a very high per-process limit can quietly fill most of the system table. Every other program then fails to open files, even though none of them is doing anything wrong.
Finding the culprit in four commands
- Is the system table nearly full?
sysctl kern.maxfiles kern.num_files - Which process holds the most files?
lsof | awk '{print $2}' | sort | uniq -c | sort -rn | head -5 - Who started it?
ps -o ppid,command -p <pid>, then follow the parent upward. - What kind of files are leaking?
lsof -p <pid> | awk '{print $5}' | sort | uniq -c
The answers were unambiguous. One child process held 61,446 file descriptors; 61,431 of them were regular files. The same file appeared over and over — one file was open 120 times. That is the signature of a leak: files opened and never closed.
Following the parent chain led to a code-indexing MCP server. The Codex desktop app had launched it from my agent configuration, and it was indexing a large folder under my Documents directory. The other instances of the same server were fine, with a dozen to thirty-odd open files each. Only the one indexing that large path had run away.
The fix, and what it does not fix
Killing that one indexing child process was enough. Its parent exited with it, and the coding agent itself kept running. kern.num_files dropped from 68,642 to 7,194, and builds and tests worked again immediately.
That is a stop-gap, not a cure. The agent will start the server again the next time it calls one of its tools, and it may leak again. I have not yet verified a permanent fix. The three options I am weighing:
- Disable that MCP server in the agent's configuration when I am not using it.
- Point it at a much smaller folder instead of a whole Documents tree.
- Upgrade or reinstall the tool and check whether a newer version still leaks.
I am describing what I observed with the version I had installed. I have not traced the leak inside the tool's source, so I am not claiming where exactly it comes from.
Two different problems on the same morning
The same day, my 16 GB machine had also run out of memory earlier, which crashed an agent session. It was tempting to blame the build failures on that too. They were unrelated: memory was fine by then, and the file table was the bottleneck. Measuring the specific resource — not the one that failed last time — kept me from fixing the wrong thing.
What I changed in how I work
- When everything fails at once, check the machine before the code. Shared limits produce failures that look like unrelated bugs.
- Know which limit you are reading.
ulimit -nis per process.kern.num_filesagainstkern.maxfilesis the whole system. - Remember what your agents start. MCP servers launched by coding agents are real processes with real resource use, running whether you are looking or not.
- Keep indexing scopes small. A tool told to index an entire home folder has a lot of files to mishandle.
- Write the diagnosis down. I saved the four commands as a note my agents can search, so the next time this happens the first step is measuring, not guessing.
The broader lesson is the same one I keep relearning with AI tooling: the agent is not just the chat window. It is every process it starts on your machine.