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

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

commit f74a83c8d82ead2ba3629ca657e6cc7afb01d0f8
Author: Marco Casaroli <[email protected]>
AuthorDate: Wed Nov 25 19:23:32 2026 +0100

    net/udp: Report no EPOLLOUT edge for a poll of the device alone.
    
    An epoll set keeps its poll set up across its waits (POLLALWAYS), and
    each notification of the socket can be an event for it.
    udp_poll_eventhandler() set POLLOUT for each UDP_POLL callback.
    Without CONFIG_NET_UDP_WRITE_BUFFERS the device polls every UDP
    connection when one UDP socket has a datagram to send, and this does not
    change the state of the other sockets.  So an EPOLLOUT | EPOLLET waiter
    on an idle socket got an event each time another socket sent a
    datagram.
    
    Without write buffers the socket can always send.  Count the sendto()
    calls that wait for a poll of the device in the connection, and report
    POLLOUT to an epoll set only while one of them waits: the poll then
    sends a datagram of this socket (Linux reports EPOLLOUT when a datagram
    that the socket sent leaves it).  poll() and select() do not change.
    With write buffers the callback does not change: a send can return
    EAGAIN there, and the waiter must get the edge when room comes back.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/udp/udp.h                   |  4 ++++
 net/udp/udp_netpoll.c           | 14 +++++++++++---
 net/udp/udp_sendto_unbuffered.c |  6 +++++-
 3 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/net/udp/udp.h b/net/udp/udp.h
index 6533beb8c90..9c65383e4b8 100644
--- a/net/udp/udp.h
+++ b/net/udp/udp.h
@@ -137,6 +137,10 @@ struct udp_conn_s
   uint8_t  crefs;         /* Reference counts on this instance */
   uint32_t listid;        /* The order of entry in the list of UDP
                            * connections (udp_alloc()) */
+#ifndef CONFIG_NET_UDP_WRITE_BUFFERS
+  uint16_t sending;       /* sendto() calls that wait for a poll of the
+                           * device */
+#endif
 
 #if CONFIG_NET_RECV_BUFSIZE > 0
   int32_t  rcvbufs;       /* Maximum amount of bytes queued in recv */
diff --git a/net/udp/udp_netpoll.c b/net/udp/udp_netpoll.c
index 1c787682840..0b0502b3cf2 100644
--- a/net/udp/udp_netpoll.c
+++ b/net/udp/udp_netpoll.c
@@ -94,9 +94,17 @@ static uint32_t udp_poll_eventhandler(FAR struct 
net_driver_s *dev,
           eventset |= (POLLHUP | POLLERR);
         }
 
-      /* A poll is a sign that we are free to send data. */
-
-      else if (psock_udp_cansend(info->conn) >= 0)
+      /* A poll is a sign that we are free to send data.  Without write
+       * buffers the device polls every UDP connection when one of them
+       * sends: an epoll set (POLLALWAYS) gets POLLOUT only from its own.
+       */
+
+      else if (psock_udp_cansend(info->conn) >= 0
+#ifndef CONFIG_NET_UDP_WRITE_BUFFERS
+               && ((info->fds->events & POLLALWAYS) == 0 ||
+                   info->conn->sending > 0)
+#endif
+              )
         {
           eventset |= POLLOUT;
         }
diff --git a/net/udp/udp_sendto_unbuffered.c b/net/udp/udp_sendto_unbuffered.c
index 19e14fd5a0e..90486018777 100644
--- a/net/udp/udp_sendto_unbuffered.c
+++ b/net/udp/udp_sendto_unbuffered.c
@@ -500,8 +500,11 @@ ssize_t psock_udp_sendto(FAR struct socket *psock, FAR 
const void *buf,
       state.st_cb->priv  = (FAR void *)&state;
       state.st_cb->event = sendto_eventhandler;
 
-      /* Notify the device driver of the availability of TX data */
+      /* Notify the device driver of the availability of TX data.  The
+       * poll waiters report POLLOUT while conn->sending is not zero.
+       */
 
+      conn->sending++;
       netdev_txnotify_dev(state.st_dev, UDP_POLL);
 
       /* Wait for either the receive to complete or for an error/timeout to
@@ -512,6 +515,7 @@ ssize_t psock_udp_sendto(FAR struct socket *psock, FAR 
const void *buf,
       ret = conn_dev_sem_timedwait(&state.st_sem, true,
                                    _SO_TIMEOUT(conn->sconn.s_sndtimeo),
                                    &conn->sconn, state.st_dev);
+      conn->sending--;
       if (ret >= 0)
         {
           /* The result of the sendto operation is the number of bytes

Reply via email to