I had new terminal windows failing with
[forkpty: Too many open files]
[Could not create a new process and open a pseudo-tty.]
even though I had kern.tty.ptmx_max: 511
(the default setting for my computer running Catalina with 64 GiB memory). I also had kern.maxfilesperproc: 98304
so I knew it was not that limit, either.
I thought it might be ulimit
since ulimit -n
was 256, but discounted that because lsof -c Terminal | wc
was 315, well over 256. Turns out it was ulimit
but lsof
lists more files than ulimit
limits. I will skip the explanation, but what you want to look at is
lsof -X -c Terminal | wc
The -X
limits lsof
to showing just file descriptors, which what ulimit
limits. That was 257 (the 256 limit, plus a header line in the lsof
output).
So, in addition to the 2 sysctl
settings, I needed to adjust the default ulimit
with launchctl
. That turns out to be harder than you would expect.
The command to use to view the ulimit
soft and hard limits on the number of open files is launchctl limit maxfiles
which on my computer output
maxfiles 256 unlimited
The command to change the limits is launchctl limit maxfiles <softlimit> <hardlimit>
but you cannot set the hard limit to "unlimited" because the kernel converts "unlimited" to 10240. If you leave out the hard limit, it is set to the same as the soft limit, which is even worse. So what I did was use the value from sysctl kern.maxfilesperproc
as the hard limit. I ran
sudo launchctl limit maxfiles 1001 98304
Well, that took care of ulimit
, but unfortunately, it also reset the kernel parameters:
kern.maxfiles: 98304
kern.maxfilesperproc: 1001
which is definitely not what I wanted. I wanted the soft limit to change the default ulimit and the hard limit to change kern.maxfilesperproc
but instead the soft limit changed kern.maxfilesperproc
and the hard limit changed kern.maxfiles
. So I ran it again, restoring the previous values from the kernel
sudo launchctl limit maxfiles 98304 196608
This sets the ulimit
way too high, but I think that is better than setting the kernel limit too low.
These changes also do not persist after reboot. To get them to persist, now that /etc/launchd.conf
no longer works, consider these solutions for persisting a single setting or restoring the launchd.conf behavior.