Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with EPOLLERR and EPOLLHUP?

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!

like image 976
Charles0429 Avatar asked Jun 09 '14 11:06

Charles0429


People also ask

What is Epollhup?

EPOLLHUP is UNMASKABLE event (...). It means that after we received EOF , poll always returns immediately, making impossible poll() on write() in state CLOSE_WAIT . One solution is evident --- to set EPOLLHUP if and only if shutdown has been made in both directions.

What is Epoll_ctl?

Description. The interface epoll_ctl() shall control an epoll file descriptor. The parameter epfd shall specify the epoll file descriptor to control. The parameter op shall specify the operation to perform on the specified target file descriptor.

What is epoll_wait?

The epoll_wait() system call waits for events on the epoll(7) instance referred to by the file descriptor epfd. The buffer pointed to by events is used to return information from the ready list about file descriptors in the interest list that have some events available. Up to maxevents are returned by epoll_wait().


1 Answers

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.

like image 138
Armali Avatar answered Sep 30 '22 14:09

Armali