SSH "accept : too many open files" on OS X when using Burp

EDIT 19.04.2013 10:17 ---
WARNING! This did break the Tor Browser Bundle on my machine. The error was "Couldn't set maximum number of file descriptors: Invalid argument"

For as long as I can remember, when using SSH as a forward proxy to proxy Burp Suite through an upstream server I have gotten a "accept : too many open files" error in my Mac OS X Terminal after a couple of hours of using Burp's Proxy and/or Scanner.

When searching Google the first solution I came across was to set the 'ulimit' to something higher, as far as I can tell 'ulimit' sets user system limits such as how many open files a user is allowed to have open at once.

On OS X when attempting to set this limit to 'unlimited' I always got an error, "Neither the hard nor soft limit for "maxfiles" can be unlimited. Please use a numeric parameter for both.", or when setting the ulimit to something higher than the default (256) the error (accept : too many open files) would still not go away or at least not for long. The only thing I found that would get rid of the error was to kill my ssh session and spawn a new one.

After further reading, some forums and blogs suggested updating openssh, I did this and the issue persisted. I thought the issue may have been openssl, so I updated that, the issue persisted.

I also tweeted about the issue where the suggestion of adjusting the ulimit resurfaced, but I just couldn't get ulimit to fix the issue.

Finally, I came across this post on where the user suggests to issue the following commands:

echo 'kern.maxfiles=20480' | sudo tee -a /etc/sysctl.conf
echo -e 'limit maxfiles 8192 20480\nlimit maxproc 1000 2000' | sudo tee -a /etc/launchd.conf
echo 'ulimit -n 4096' | sudo tee -a /etc/profile

As far as I can tell, the above seems to set a kernal limit, system limit and also the user's ulimit. After a system restart, the error has now gone!

The user also adds some additional notes:

You will need to restart for these changes to take effect.
AFAIK you can no longer set limits to 'unlimited' under OS X
launchctl maxfiles are bounded by sysctl maxfiles, and therefore cannot exceed them
sysctl seems to inherit kern.maxfilesperproc from launchctl maxfiles
ulimit seems to inherit it's 'open files' value from launchctl by default
you can set a custom ulimit within /etc/profile, or ~/.profile ; while this isn't required I've provided an example
Be cautious when setting any of these values to a very high number when compared with their default - the features exist stability/security. I've taken these example numbers that I believe to be reasonable, written on other websites.

I'm sure I am not the only one who has come across this before, so hopefully it is useful to other Burp Suite user's who use SSH as a forwarding proxy on Mac OS X. It worked for me so should hopefully work for you too.