Skip to content

[Bug]: Running Claude Code in a container crashes apps on the host system with "too many open files" #1097

Open

Description

@Artur-

I have done the following

  • I have searched the existing issues
  • If possible, I've reproduced the issue using the 'main' branch of this project

Steps to reproduce

https://github.com/emarc/claude-contained implements a containerized version of Claude Code (and other AI tools). It works by sharing the current folder with Claude Code running in a container.

Run that on any folder with many files (tried with https://github.com/vaadin/flow) and ask Claude to do just about anything related to files (e.g. "What files are there").

Current behavior

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

Environment

- OS: macOS Tahoe 26.2 (25C56)
- Xcode: 
- Container: container CLI version 0.8.0 (build: release, commit: a18df81)

Relevant log output

N/A

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

  1. added theissue type on Jan 26, 2026
  2. Artur- commented on Jan 26, 2026

    @Artur-
    Author

    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

  3. Artur- commented on Jan 27, 2026

    @Artur-
    Author

    Here is a super simple test case: https://github.com/Artur-/test-apple-container

    It creates a folder structure, a container that only runs find and 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

  4. Artur- commented on Jan 27, 2026

    @Artur-
    Author

    Tried to reproduce by using docker instead of container but then the scripts complete successfully (after quite some time)

  5. crimson-knight commented on Jan 27, 2026

    @crimson-knight

    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.

  6. emarc commented on Jan 27, 2026

    @emarc

    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.

  7. dcantah commented on Jan 29, 2026

    @dcantah
    Contributor

    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

  8. leonewt0n commented on Jan 29, 2026

    @leonewt0n

    does adding this to ~/.zshrc and restarting your terminal fix it?

    ulimit -n 64000
    
  9. added a commit that references this issue on Feb 2, 2026
    7dfe27d
  10. dcantah commented on Feb 3, 2026

    @dcantah
    Contributor

    The rlimit PR does not fix this, so reopening. It's just somewhat adjacent

  11. reopened this on Feb 3, 2026
  12. added a commit that references this issue on Feb 4, 2026
    8b79ec5
  13. added a commit that references this issue on Feb 18, 2026
    61c0378
  14. thomasjm commented on Mar 30, 2026

    @thomasjm

    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:

    /usr/sbin/sysctl \
      -w \
      kern.maxfiles=1048576 \
      kern.maxfilesperproc=524288 \
      kern.maxvnodes=1048576

    Note that increasing kern.maxvnodes was necessary in addition to the maxfiles* 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?

    This partly seems like Virtualization.framework's virtiofs device behavior (an fd is held open after doing vfs lookups; this is intentional)

    How long is it held open? If the answer is forever, that sounds like a problem.

  15. thomasjm commented on Mar 31, 2026

    @thomasjm

    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?

  16. dcantah commented on Apr 1, 2026

    @dcantah
    Contributor

    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?

  17. thomasjm commented on Apr 1, 2026

    @thomasjm

    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.

  18. Artur- commented on Jun 28, 2026

    @Artur-
    Author

    Was there ever any progress on this issue?

  19. dave-tn commented on Jul 24, 2026

    @dave-tn

    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:

    1. 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
      
    2. 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.

    3. When launchd itself takes the SIGBUS, the kernel panics and the machine reboots:

      panic(cpu 2 caller 0xfffffe0041d6e4b8): initproc exited -- exit reason namespace 2 subcode 0xa description: none
      OS version: 25F84
      Panicked task: 1523 pages, 9 threads: pid 1: launchd
      

    Environment:

    • container 1.1.0 (Homebrew)
    • 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:

    sudo sysctl -w kern.maxfiles=2097152 kern.maxfilesperproc=245760 kern.maxvnodes=2097152
    

    (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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions