Search code examples
clinuxforklibevent

Do I need to evconnlistener_disable() before fork()?


I am developing a server app with libevent now.

The server sometimes needs to exec some external command (i.e. fork() then exec()). My concern is that, right after fork(), both the parent and the child might be effectively listening, and the child may have a chance to 'accept' a new incoming connection before it exec()s, this could cause the parent (the server app) to lose an incoming request.

Do I need to evconnlistener_disable() before fork() and evconnlistener_enable() after fork()?


Solution

  • There are several ways to do it, depending on what exactly you're aiming for. You can use wait() on the parent to effectively pause it until the child has finished. Or immediately close the listener file descriptor in the child. Or, if you're only worried about incoming requests during an exec(), you can set the close-on-exec flag on the file descriptor (FD_CLOEXEC).

    I would have thought that disabling the listener before fork() and then re-enabling it would re-enable it for the child, too.