You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: Running Claude Code in a container crashes apps on the host system with "too many open files" #1097
It will start consuming file descriptors as it iterates through files. Quite quickly you will have tens or hundreds of thousands of file descriptors in use when the project contains e.g. a node_modules folder. Then you will quite quickly reach the Mac OS file system limit and applications inside or outside the container will start crashing.
Expected behavior
It closes file descriptors after they have been used
It does not seem to be related to Claude Code as you can notice the same effect by launching the container as claude-contained -s which only starts a shell and running find will also consume all file descriptors and cause apps to crash
This is a very clear issue, thank you for figuring this out. I was planning on using a container for Claude Code and I'm sure I would have run into this myself and lost my mind trying to figure out the root cause!
Knowing what little I know about Swift, I'm sure there is a way to memory optimize the file descriptors so that this issue is resolved. Probably a fun meta issue that Claude Code could try resolving itself.
This partly seems like Virtualization.framework's virtiofs device behavior (an fd is held open after doing vfs lookups; this is intentional), and could be partly rlimit related in the guest as well. Will share some updates after digging in the next few days
Reacted by Ronit Sabhaya, Artur Signell and Ryosuke Komatsu
I ran into this by doing Nix builds in a container. The workaround I found was increasing limits on the host system. Mine were about 100k and I increased by about 10x:
I have a feeling it does hold them open forever. I just removed an idle container that had been running for a long time, and my kern.num_files number dropped by like 20k! Can you confirm if that's the case @dcantah?
Sorry for radio silence, been slammed with other things..
The device holds open FDs right around until the value of kern.maxfilesperproc, and then tries to purge a portion of them. Just to be certain @thomasjm, were you seeing the system limits being reached as well?
Yes, like others reported I saw processes both inside and outside the containers fail because they couldn't allocate a file handle.
I think this was because I was launching multiple containers though. It would only take 2 to exhaust the system limit I think, since the kern.maxfilesperproc is usually 50% of the kern.maxfiles.
With some clanker-assisted debugging, it looks like this caused a kernel panic for me - twice.
'Assisted' summary -
Use case: pi-tin runs multiple Apple containers, each hosting a/multiple coding agents with a/multiple project directories bind-mounted — essentially the scenario in the OP, several times over.
"Apps start crashing" undersells the severity — when the system-wide file table fills, the failure escalates to a full host panic. The chain, from my crash reports:
Host processes start dying with SIGBUS as memory-mapped page-ins fail. One captured report (cfprefsd) states the cause explicitly:
EXC_BAD_ACCESS (SIGBUS), subtype: FS pagein error: 23 Too many open files in system
Fallout while the table is full: the login keychain becomes unreadable (macOS offers to reset it), mds_stores and other daemons crash, and shutdown stalls.
When launchd itself takes the SIGBUS, the kernel panics and the machine reboots:
macOS 26.5.2 (25F84), MacBook Pro (MacBookPro18,2 / M1 Max, 64 GB) — the default limits scale with RAM (kern.maxfiles=491520 on my machine), so lower-memory machines would seemingly hit this even earlier.
Currently working around it with the sysctl limit increases suggested above.
Workaround tip for anyone applying the sysctl workaround: based on the purge behaviour described above, each container VM holds fds up to roughly kern.maxfilesperproc before purging — so that value is effectively the per-container cap, and the system-wide limit needs to fit your expected number of concurrent containers underneath it:
kern.maxfiles ≥ N_containers × kern.maxfilesperproc + host headroom (~25%)
kern.maxvnodes ≥ kern.maxfiles (each held fd pins a vnode)
Leaving kern.maxfilesperproc at its default (245,760 on my machine) seems preferable to raising it — nothing else needs more, and raising it just lets each VM hoard more before purging. For example, to tolerate up to 8 saturated containers on my machine:
(These are caps, not allocations — memory is only consumed as files are actually held, roughly 1–2 KB per pinned vnode, so a fully saturated 2M table would wire a few GB. Worth keeping N modest on lower-RAM machines.)
Activity
Artur- commentedon Jan 26, 2026
It does not seem to be related to Claude Code as you can notice the same effect by launching the container as
claude-contained -swhich only starts a shell and runningfindwill also consume all file descriptors and cause apps to crashAdd test case for container performance issue
Artur- commentedon Jan 27, 2026
Here is a super simple test case: https://github.com/Artur-/test-apple-container
It creates a folder structure, a container that only runs
findand a test script for running 3 containers in parallel.In a few seconds, they fail and potential apps start crashing on the host system also
Artur- commentedon Jan 27, 2026
Tried to reproduce by using
dockerinstead ofcontainerbut then the scripts complete successfully (after quite some time)crimson-knight commentedon Jan 27, 2026
This is a very clear issue, thank you for figuring this out. I was planning on using a container for Claude Code and I'm sure I would have run into this myself and lost my mind trying to figure out the root cause!
Knowing what little I know about Swift, I'm sure there is a way to memory optimize the file descriptors so that this issue is resolved. Probably a fun meta issue that Claude Code could try resolving itself.
emarc commentedon Jan 27, 2026
I tried the test case provided (https://github.com/Artur-/test-apple-container), and can confirm it reproduces the problem (and is quick and easy to try).
Tested on
container CLI version 0.8.0(on Tahoe 26.2)Hopefully this problem can be addressed, as it's a bit of a showstopper for anyone trying to work on real-life codebases.
Fix file descriptor leak in DirectoryWatcher (apple#1097)
dcantah commentedon Jan 29, 2026
This partly seems like Virtualization.framework's virtiofs device behavior (an fd is held open after doing vfs lookups; this is intentional), and could be partly rlimit related in the guest as well. Will share some updates after digging in the next few days
leonewt0n commentedon Jan 29, 2026
does adding this to ~/.zshrc and restarting your terminal fix it?
CLI: Add support for rlimits (apple#1129)
dcantah commentedon Feb 3, 2026
The rlimit PR does not fix this, so reopening. It's just somewhat adjacent
Squashed commit of the following:
Squashed commit of the following:
thomasjm commentedon Mar 30, 2026
I ran into this by doing Nix builds in a container. The workaround I found was increasing limits on the host system. Mine were about 100k and I increased by about 10x:
Note that increasing
kern.maxvnodeswas necessary in addition to themaxfiles*ones.I haven't seen such high file descriptor usage when doing similar workloads on Linux, so this made me wonder if file descriptors are being leaked?
How long is it held open? If the answer is forever, that sounds like a problem.
thomasjm commentedon Mar 31, 2026
I have a feeling it does hold them open forever. I just removed an idle container that had been running for a long time, and my
kern.num_filesnumber dropped by like 20k! Can you confirm if that's the case @dcantah?dcantah commentedon Apr 1, 2026
Sorry for radio silence, been slammed with other things..
The device holds open FDs right around until the value of
kern.maxfilesperproc, and then tries to purge a portion of them. Just to be certain @thomasjm, were you seeing the system limits being reached as well?thomasjm commentedon Apr 1, 2026
Yes, like others reported I saw processes both inside and outside the containers fail because they couldn't allocate a file handle.
I think this was because I was launching multiple containers though. It would only take 2 to exhaust the system limit I think, since the
kern.maxfilesperprocis usually 50% of thekern.maxfiles.Artur- commentedon Jun 28, 2026
Was there ever any progress on this issue?
dave-tn commentedon Jul 24, 2026
With some clanker-assisted debugging, it looks like this caused a kernel panic for me - twice.
'Assisted' summary -
Use case: pi-tin runs multiple Apple containers, each hosting a/multiple coding agents with a/multiple project directories bind-mounted — essentially the scenario in the OP, several times over.
"Apps start crashing" undersells the severity — when the system-wide file table fills, the failure escalates to a full host panic. The chain, from my crash reports:
Host processes start dying with SIGBUS as memory-mapped page-ins fail. One captured report (
cfprefsd) states the cause explicitly:Fallout while the table is full: the login keychain becomes unreadable (macOS offers to reset it),
mds_storesand other daemons crash, and shutdown stalls.When launchd itself takes the SIGBUS, the kernel panics and the machine reboots:
Environment:
kern.maxfiles=491520on my machine), so lower-memory machines would seemingly hit this even earlier.Currently working around it with the
sysctllimit increases suggested above.Workaround tip for anyone applying the
sysctlworkaround: based on the purge behaviour described above, each container VM holds fds up to roughlykern.maxfilesperprocbefore purging — so that value is effectively the per-container cap, and the system-wide limit needs to fit your expected number of concurrent containers underneath it:Leaving
kern.maxfilesperprocat its default (245,760 on my machine) seems preferable to raising it — nothing else needs more, and raising it just lets each VM hoard more before purging. For example, to tolerate up to 8 saturated containers on my machine:(These are caps, not allocations — memory is only consumed as files are actually held, roughly 1–2 KB per pinned vnode, so a fully saturated 2M table would wire a few GB. Worth keeping N modest on lower-RAM machines.)