darkk/redsocks — Issue #19: redsocks enters an inifite loop when RLIMIT_NOFILE is reached and a new client connection is attempted
Author: apoikos
Created: 2012-01-26 09:14:20+00:00
State: open

Hi,

In its main loop, redsocks accept()s new connections and simply retries if accept() returns an error. However, this leads to an infinite loop in the following corner case:

1. If redsocks has reached its open file limit and
2. a new connection is being accept()ed,
3. then accept() fails because it cannot create a new file descriptor for the incoming connection, however
4. the client connection stays in the listening socket's backlog and thus
5. the listening socket appears EV_READ-ready and thus we immediately loop back to 2.

This loop causes 100% CPU utilization and logfile flooding with redsocks[1187]: accept: Too many open files, until a client connection is closed.

There are a number of ways to solve this (e.g. check that accept() does not return EMFILE and throttle), although IMHO the best one would be to perform a getrlimit(RLIMIT_NOFILE) on startup and keep track of how many fd's are currently in use before accept()ing. setrlimit() support would also be nice by the way ;-)

Thanks,
Apollon