11

I read the code in libevent epoll, here is the code:

       if (what & (EPOLLHUP|EPOLLERR)) {
            ev = EV_READ | EV_WRITE;
        } else {
            if (what & EPOLLIN)
                ev |= EV_READ;
            if (what & EPOLLOUT)
                ev |= EV_WRITE;
            if (what & EPOLLRDHUP)
                ev |= EV_CLOSED;
        }

To my understanding, when EPOLLERR or EPOLLHUP happens, the connection should be closed. But in the above code, when EPOLLHUP|EPOLLERR encounters, the event mask is set to EV_READ | EV_WRITE. So my question is :

  • What makes EPOLLERR and EPOLLHUP happen?
  • When EPOLLERR and EPOLLHUP happen, what should the program do in the event handle function? And please explain the reason behind it in detail.

Thanks in advance!

Charles0429
  • 1,406
  • 5
  • 15
  • 31

1 Answers1

9

What makes EPOLLERR and EPOLLHUP happen?

man epoll_ctl

   EPOLLERR
          Error condition happened on the associated file descriptor.
          epoll_wait(2) will always wait for this event; it is not
          necessary to set it in events.

   EPOLLHUP
          Hang up happened on the associated file descriptor.
          epoll_wait(2) will always wait for this event; it is not
          necessary to set it in events.

When EPOLLERR and EPOLLHUP happen, what should the program do in the event handle function?

As libevent passes the events EV_READ | EV_WRITE in this case, the callback function calls e. g. recv(), which likely returns 0 when the peer has performed an orderly shutdown (EPOLLHUP), or -1 if an error occurred (EPOLLERR); the program might then clean up the connection, and if it's a client, possibly reestablish it.

Armali
  • 18,255
  • 14
  • 57
  • 171