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 e08228881272c641036ef4617b9ac6e02444b99d Author: Marco Casaroli <[email protected]> AuthorDate: Fri Oct 30 08:11:11 2026 +0100 net/local: Fix poll() on a datagram socket pair. poll() of a socket of a SOCK_DGRAM socket pair used the code of an unconnected datagram socket. It reported POLLOUT at all times, also when the FIFO to the peer was full. So a sender that got EAGAIN got POLLOUT again at once. A socket pair has the two FIFOs of a connected stream socket, so it now uses the same code: POLLIN from the FIFO that the socket reads, POLLOUT from the FIFO that it writes, POLLHUP when the peer was closed and the FIFO is empty. The poll structures for POLLIN and POLLOUT together (lc_inout_fds) are no longer only for stream sockets. The poll thresholds fit datagrams: POLLIN when the FIFO has more than the first field of a header, POLLOUT when more than half of the FIFO is free. local_set_pollthreshold() did not set the POLLOUT threshold (it checked for a positive return value of an ioctl that returns 0); it now sets both thresholds of the FIFO that a socket reads, also after SO_RCVBUF changed its size. The same in all build modes; no architecture code. struct local_conn_s is larger by 2 * LOCAL_NPOLLWAITERS poll structures when NET_LOCAL_DGRAM is on and NET_LOCAL_STREAM is off. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- net/local/local.h | 13 +++++++++---- net/local/local_fifo.c | 19 +++++++++++-------- net/local/local_netpoll.c | 23 ++++++++++++++++------- net/local/local_sockif.c | 11 ++++++++++- 4 files changed, 46 insertions(+), 20 deletions(-) diff --git a/net/local/local.h b/net/local/local.h index 05c4f7609a3..9fce2db14fb 100644 --- a/net/local/local.h +++ b/net/local/local.h @@ -153,6 +153,12 @@ struct local_conn_s */ #endif + /* The poll structures for the two FIFOs of a connected stream socket or + * of a socket pair, for poll() of POLLIN and POLLOUT together. + */ + + struct pollfd_s lc_inout_fds[2*LOCAL_NPOLLWAITERS]; + #ifdef CONFIG_NET_LOCAL_STREAM /* SOCK_STREAM fields common to both client and server */ @@ -163,7 +169,6 @@ struct local_conn_s */ FAR struct pollfd_s *lc_event_fds[LOCAL_NPOLLWAITERS]; - struct pollfd_s lc_inout_fds[2*LOCAL_NPOLLWAITERS]; /* Union of fields unique to SOCK_STREAM client, server, and connected * peers. @@ -799,13 +804,13 @@ int32_t local_generate_instance_id(void); * Name: local_set_pollthreshold * * Description: - * Set the local pollin and pollout threshold: + * Set the poll thresholds of the FIFO that a socket of a SOCK_DGRAM + * socket pair reads. * ****************************************************************************/ #ifdef CONFIG_NET_LOCAL_DGRAM -int local_set_pollthreshold(FAR struct local_conn_s *conn, - unsigned long threshold); +int local_set_pollthreshold(FAR struct local_conn_s *conn); #endif /**************************************************************************** diff --git a/net/local/local_fifo.c b/net/local/local_fifo.c index c69c3349590..66a6b95a2d3 100644 --- a/net/local/local_fifo.c +++ b/net/local/local_fifo.c @@ -413,22 +413,25 @@ static int local_set_polloutthreshold(FAR struct file *filep, * Name: local_set_pollthreshold * * Description: - * Set the local pollin and pollout threshold: + * Set the poll thresholds of the FIFO that a socket of a SOCK_DGRAM + * socket pair reads: POLLIN after the first field of a header, POLLOUT + * (for the writer) when more than half of the FIFO is free. * ****************************************************************************/ #ifdef CONFIG_NET_LOCAL_DGRAM -int local_set_pollthreshold(FAR struct local_conn_s *conn, - unsigned long threshold) +int local_set_pollthreshold(FAR struct local_conn_s *conn) { int ret; - /* Set the buffer poll threshold */ - - ret = local_set_pollinthreshold(&conn->lc_infile, threshold); - if (ret > 0) + ret = local_set_pollinthreshold(&conn->lc_infile, sizeof(lc_size_t)); + if (ret >= 0) { - ret = local_set_polloutthreshold(&conn->lc_outfile, threshold); + ret = file_ioctl(&conn->lc_infile, PIPEIOC_GETSIZE, 0); + if (ret > 0) + { + ret = local_set_polloutthreshold(&conn->lc_infile, ret / 2); + } } return ret; diff --git a/net/local/local_netpoll.c b/net/local/local_netpoll.c index 7e04c2c19b1..c0646ee0846 100644 --- a/net/local/local_netpoll.c +++ b/net/local/local_netpoll.c @@ -107,6 +107,7 @@ static int local_event_pollsetup(FAR struct local_conn_s *conn, return OK; } +#endif /* CONFIG_NET_LOCAL_STREAM */ /**************************************************************************** * Name: local_inout_poll_cb @@ -118,7 +119,6 @@ static void local_inout_poll_cb(FAR struct pollfd_s *fds) poll_notify(&originfds, 1, fds->revents); } -#endif /**************************************************************************** * Public Functions @@ -159,7 +159,9 @@ int local_pollsetup(FAR struct socket *psock, FAR struct pollfd_s *fds) FAR struct local_conn_s *conn = psock->s_conn; int ret = OK; - if (conn->lc_proto == SOCK_DGRAM) +#ifdef CONFIG_NET_LOCAL_DGRAM + if (conn->lc_proto == SOCK_DGRAM && + conn->lc_state != LOCAL_STATE_CONNECTED) { /* A datagram send opens its FIFO on demand and has no send buffer to * wait for, so it is always possible to send. Report POLLOUT @@ -182,12 +184,19 @@ int local_pollsetup(FAR struct socket *psock, FAR struct pollfd_s *fds) return OK; } +#endif #ifdef CONFIG_NET_LOCAL_STREAM if (conn->lc_state == LOCAL_STATE_LISTENING) { return local_event_pollsetup(conn, fds, true); } +#endif + + /* A connected stream socket or a socket of a socket pair (stream or + * datagram): POLLIN from the FIFO that it reads, POLLOUT from the FIFO + * that it writes. + */ if (conn->lc_state == LOCAL_STATE_DISCONNECTED) { @@ -295,15 +304,12 @@ int local_pollsetup(FAR struct socket *psock, FAR struct pollfd_s *fds) default: break; } -#endif return ret; -#ifdef CONFIG_NET_LOCAL_STREAM pollerr: poll_notify(&fds, 1, POLLERR); return OK; -#endif } /**************************************************************************** @@ -327,7 +333,9 @@ int local_pollteardown(FAR struct socket *psock, FAR struct pollfd_s *fds) FAR struct local_conn_s *conn = psock->s_conn; int ret = OK; - if (conn->lc_proto == SOCK_DGRAM) +#ifdef CONFIG_NET_LOCAL_DGRAM + if (conn->lc_proto == SOCK_DGRAM && + conn->lc_state != LOCAL_STATE_CONNECTED) { /* Only a registered poll (fds->priv set by setup) needs teardown. */ @@ -338,12 +346,14 @@ int local_pollteardown(FAR struct socket *psock, FAR struct pollfd_s *fds) return OK; } +#endif #ifdef CONFIG_NET_LOCAL_STREAM if (conn->lc_state == LOCAL_STATE_LISTENING) { return local_event_pollsetup(conn, fds, false); } +#endif if (conn->lc_state == LOCAL_STATE_DISCONNECTED) { @@ -402,7 +412,6 @@ int local_pollteardown(FAR struct socket *psock, FAR struct pollfd_s *fds) default: break; } -#endif return ret; } diff --git a/net/local/local_sockif.c b/net/local/local_sockif.c index 6f0ea723270..706e93244a9 100644 --- a/net/local/local_sockif.c +++ b/net/local/local_sockif.c @@ -784,6 +784,15 @@ static int local_setsockopt(FAR struct socket *psock, int level, int option, { ret = file_ioctl(&conn->lc_infile, PIPEIOC_SETSIZE, rcvsize); +#ifdef CONFIG_NET_LOCAL_DGRAM + /* A socket pair: thresholds for the new size */ + + if (ret >= 0 && psock->s_type == SOCK_DGRAM && + conn->lc_state == LOCAL_STATE_CONNECTED) + { + ret = local_set_pollthreshold(conn); + } +#endif } #ifdef CONFIG_NET_LOCAL_DGRAM else if (psock->s_type == SOCK_DGRAM && @@ -1250,7 +1259,7 @@ static int local_socketpair(FAR struct socket *psocks[2]) { for (i = 0; i < 2; i++) { - ret = local_set_pollthreshold(conns[i], sizeof(lc_size_t)); + ret = local_set_pollthreshold(conns[i]); if (ret < 0) { goto errout;
