What to do w/accept when too-many-files-open

Marc Lehmann schmorp at schmorp.de
Tue Jan 22 12:28:19 CET 2008

On Mon, Jan 21, 2008 at 10:57:35PM -0800, Eric Brown <yogieric.list at gmail.com> wrote:
> I have put my server socket into non-blocking mode and added a watcher.
> Everything works fine. My callback gets called and I call accept. However,
> when accept returns -1 with errno EMFILE (too many files open), I just exit
> out of my callback. But it is called again immediately.

Of course.

> I suppose at this point I could count the number of file descriptors my
> program is using, stop the watcher, and when one of my file descriptors is
> closed, start accepting again.

I would say if you need more file desscriptors you should give it to the
progrma in the first place.

> Not only is that tedious, but I'm not even sure it works as I think the
> reason I got into this state is old crashed clients in time-wait state.

if you indeed run out of file handles then this cnanot be the case, as
time-wait states do not coutn towards your fd count (because you closed
the socket already, supposedly - if you don't close sockets when the
connection goes down thats the bug then).

> Instead, is there some way I can tell the callback to be called less
> often (say every second)? I suppose I could just stop the callback and
> create a timer to start it again... or is there a better way?

Of course, stop it, start a timer, and start it again a bit later. It is
much better to fix the cause, however, and e.g. increase the number of

> I don't want to up the ulimit at this point - I just want to properly
> handle this condition.

The proper handling is to up the ulimit if your server is indeed that
busy. If it isn't, then there is a bug somewhere, e.g. you forget to close
your connections.

                The choice of a       Deliantra, the free code+content MORPG
      -----==-     _GNU_              http://www.deliantra.net
      ----==-- _       generation
      ---==---(_)__  __ ____  __      Marc Lehmann
      --==---/ / _ \/ // /\ \/ /      pcg at goof.com
      -=====/_/_//_/\_,_/ /_/\_\

More information about the libev mailing list