For any epoll TCP socket event, if EPOLLRDHUP = 0 and EPOLLIN = 1; is the next call to read () / recv () guaranteed to return a read size not equal to 0?

From the epoll_ctl manual:

EPOLLRDHUP (since Linux 2.6.17)

Peer-to-peer connection or closing half of the connection. (This flag is especially useful for writing simple code to detect peer outages when using Edge Triggered monitoring.)

From the recv manual:

If the messages are not available and the peer completed the ordered completion, recv () should return 0.

It seems to me that both of the above cover the same scenarios and that if I catch the EPOLLRDHUP events first, I should never get read () or recv () of length 0 (and therefore should not be taken care of). But is it guaranteed that this is true?

+6
source share
1 answer

If you received an event with EPOLLRDHUP=1 , just close the connection immediately without reading. If you get an event with EPOLLRDHUP=0 and EPOLLIN=1 , then go EPOLLIN=1 and read it, but you should be prepared for recv() to return 0 anyway, just in case. Maybe FIN will arrive after you get EPOLLIN=1 , but before you actually call recv() .

+6
source

Source: https://habr.com/ru/post/944672/


All Articles