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 :
Thanks in advance!
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.
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.
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().
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With