13

According to the linux man page,

EPOLLHUP

When reading from a channel such as a pipe or a stream socket, this event merely indicates that the peer closed its end of the channel.

EPOLLRDHUP

Stream socket peer closed connection, or shut down writing half of connection.

I can hardly tell any difference between EPOLLHUP and EPOLLRDHUP.

To me, whenever EPOLLRDHUP is used EPOLLHUP can be used instead with the same semantics.

Am I right? If not, any explanations?

xmllmx
  • 39,765
  • 26
  • 162
  • 323
  • 4
    EPOLLHUP means you can't write, EPOLLRDHUP (and read()==0) means you can't read. https://medium.com/where-the-flamingcow-roams/down-the-epoll-rabbit-hole-5c0447cb6329 – n. m. could be an AI Aug 14 '18 at 03:51
  • No. EPOLLHUP means you half-close the socket and the peer half-closes the socket too. (So you can't either read or write) See https://stackoverflow.com/questions/52976152/tcp-when-is-epollhup-generated. – kbridge4096 Jul 09 '19 at 11:36
  • Here is nice explanation for these two flags: https://stackoverflow.com/a/6438173/7888101 – Komal Udhani Mar 27 '20 at 09:03
  • They mean slightly different things and require different handling. Read the man page for shutdown (2) – hookenz Mar 11 '21 at 03:03

1 Answers1

0
  • EPOLLHUP means that the peer closed their end of the connection. Writing to the connection is closed, and after any (possible) readable data is consumed, reading from the connection is closed, too.
  • EPOLLRDHUP only means that the peer closed their connection, or only half of their connection. If it's only halfway closed, the stream socket turns into a one-way, write-only connection. Writing to the connection may still be open, but after any (possible) readable data is consumed, reading from the connection is closed.

This may be done if the peer calls shutdown() on their socket descriptor, disallowing itself from writing data:

#include <sys/socket.h>

int sockfd = /* ... */;
shutdown(sockfd, SHUT_WR);
ifconfig
  • 6,242
  • 7
  • 41
  • 65
Samuel Hunter
  • 527
  • 2
  • 11
  • Not possible. The localhost cannot detect any difference between the peer closing the socket and shutting it down for output. – user207421 Mar 11 '21 at 05:12
  • 1
    @user207421 I don't understand. Where did I say that localhost can tell the difference? Shut down for output in the perspective of the peer, or localhost? – Samuel Hunter Mar 11 '21 at 05:20