Libevent tcp server crash for fast disconnected clients

We’re using libevent (2.1 beta version) on our tcp server application.

Everything works fine. But, when clients closes their tcp connections as soon as connection establishment phase, our server applications crashed with unbroken pipe error.

I’m testing our application with the following command (8000 is server port number):

$ for i in `seq 1000`; do echo "TEST" > /dev/tcp/; done

How can I fix this broke pipe errors?

This problem is not related with libevent library.

Under POSIX systems when a process received a signal and there is no signal handler or signal block mask exists for it, decision made by the kernel according to signal’s default action.

Default action of signals can be ignore, terminate, core dump, continue and stop. You can see more details on man 7 signal.

As you can see on the man page, default action of SIGPIPE is terminate, so within your application if there is no handler or block mask exist for SIGPIPE, it will be terminated automatically.

You can setup a signal handler or you can just block SIGPIPE with the code similar like:

        sigset_t mask;
        sigset_t orig_mask;

        sigaddset(&mask, SIGPIPE);

        if (sigprocmask(SIG_BLOCK, &mask, &orig_mask) < 0) {
                perror ("sigprocmask");
                return 1;

In your case, libevent accepts new tcp connections and setup new read, write and event callbacks. Probably you’re trying to write client’s socket but socket already closed by the client itself. This is called a “broken pipe” and you’re getting SIGPIPE. It is safe to block this signal.