This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 32e4a9ad17de065fc6c096b8f4ab896e48e0d2d5
Author: Marco Casaroli <[email protected]>
AuthorDate: Wed Nov 11 23:09:07 2026 +0100

    net/tcp: Report a hang-up of a socket as Linux does.
    
    After the peer closed or shut down its writing half, TCP reports POLLIN
    alone.  A caller cannot tell the end of the stream from data, and a
    caller that asks only for POLLRDHUP gets nothing.  After this socket
    also shut down its writing half, there is no POLLHUP.  When the
    connection ended (a reset, a timeout), there is no POLLRDHUP, and after
    a timeout no POLLIN, but read() does not block.
    
    Linux reports (tcp_poll()):
    
    - no more data comes in (the peer closed or shut down its writing half,
      or shutdown(SHUT_RD)): POLLIN | POLLRDHUP
    - both directions shut down, or the connection ended: also POLLHUP
    - an error (a reset): also POLLERR
    
    The poll now gives POLLIN | POLLRDHUP for SHUT_RD in conn->shutdown and
    for the TCP_RXCLOSE event, POLLHUP when both directions are shut down,
    and POLLIN | POLLRDHUP also when the connection ended.  A socket that
    never connected gives POLLERR | POLLHUP, as before.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/tcp/tcp_netpoll.c | 55 +++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 42 insertions(+), 13 deletions(-)

diff --git a/net/tcp/tcp_netpoll.c b/net/tcp/tcp_netpoll.c
index 66cf9614149..45881b62445 100644
--- a/net/tcp/tcp_netpoll.c
+++ b/net/tcp/tcp_netpoll.c
@@ -84,11 +84,24 @@ static uint32_t tcp_poll_eventhandler(FAR struct 
net_driver_s *dev,
 
       /* Check for data or connection availability events. */
 
-      if ((flags & (TCP_NEWDATA | TCP_BACKLOG | TCP_RXCLOSE)) != 0)
+      if ((flags & (TCP_NEWDATA | TCP_BACKLOG)) != 0)
         {
           eventset |= POLLIN;
         }
 
+      /* The peer closed or shut down its writing half: read() does not
+       * block.  POLLHUP when this socket also shut down its writing half.
+       */
+
+      if ((flags & TCP_RXCLOSE) != 0)
+        {
+          eventset |= POLLIN | POLLRDHUP;
+          if ((info->conn->shutdown & SHUT_WR) != 0)
+            {
+              eventset |= POLLHUP;
+            }
+        }
+
       /* Non-blocking connection */
 
       if ((flags & TCP_CONNECTED) != 0)
@@ -142,17 +155,10 @@ static uint32_t tcp_poll_eventhandler(FAR struct 
net_driver_s *dev,
           _SO_CONN_SETERRNO(info->conn, reason);
 #endif
 
-          /* After a reset of the peer, a receive does not block */
-
-          if ((info->conn->flags & TCP_RESET) != 0)
-            {
-              eventset |= POLLIN;
-            }
-
           /* Mark that the connection has been lost */
 
           tcp_lost_connection(info->conn, info->cb, flags);
-          eventset |= (POLLERR | POLLHUP);
+          eventset |= POLLIN | POLLRDHUP | POLLERR | POLLHUP;
         }
 
       /* A poll is a sign that we are free to send data. */
@@ -294,7 +300,9 @@ int tcp_pollsetup(FAR struct socket *psock, FAR struct 
pollfd_s *fds)
    * callback processing.
    */
 
-  cb->flags = TCP_DISCONN_EVENTS;
+  /* TCP_RXCLOSE also for a caller that asks only for POLLRDHUP */
+
+  cb->flags = TCP_DISCONN_EVENTS | TCP_RXCLOSE;
   cb->priv  = info;
   cb->event = tcp_poll_eventhandler;
 
@@ -315,7 +323,7 @@ int tcp_pollsetup(FAR struct socket *psock, FAR struct 
pollfd_s *fds)
 
   if ((fds->events & POLLIN) != 0)
     {
-      cb->flags |= TCP_NEWDATA | TCP_BACKLOG | TCP_RXCLOSE;
+      cb->flags |= TCP_NEWDATA | TCP_BACKLOG;
     }
 
   /* Save the reference in the poll info structure as fds private as well
@@ -326,14 +334,28 @@ int tcp_pollsetup(FAR struct socket *psock, FAR struct 
pollfd_s *fds)
 
   /* Check for read data or backlogged connection availability now */
 
-  if (conn->readahead != NULL || tcp_backlogpending(conn) ||
-      (conn->shutdown & SHUT_RD) != 0)
+  if (conn->readahead != NULL || tcp_backlogpending(conn))
     {
       /* Normal data may be read without blocking. */
 
       eventset |= POLLRDNORM;
     }
 
+  /* As on Linux: no more data comes in (the peer closed or shut down its
+   * writing half, or shutdown(SHUT_RD)): POLLIN and POLLRDHUP.  Both
+   * directions shut down: POLLHUP.
+   */
+
+  if ((conn->shutdown & SHUT_RD) != 0)
+    {
+      eventset |= POLLIN | POLLRDHUP;
+    }
+
+  if ((conn->shutdown & SHUT_RDWR) == SHUT_RDWR)
+    {
+      eventset |= POLLHUP;
+    }
+
   /* Check for a loss of connection events.  We need to be careful here.
    * There are four possibilities:
    *
@@ -400,6 +422,13 @@ int tcp_pollsetup(FAR struct socket *psock, FAR struct 
pollfd_s *fds)
                                    ECONNREFUSED : ENOTCONN));
           eventset |= POLLERR | POLLHUP;
         }
+
+      /* A connection that ended (a reset, a timeout) gets no more data */
+
+      if (conn->tcpstateflags != TCP_ALLOCATED)
+        {
+          eventset |= POLLIN | POLLRDHUP;
+        }
     }
   else if (_SS_ISCONNECTED(conn->sconn.s_flags) &&
            psock_tcp_cansend(conn) >= 0)

Reply via email to