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 1b1649baa85c2f56b302975bee7fffd9ed9ebd97 Author: Marco Casaroli <[email protected]> AuthorDate: Thu Nov 26 08:37:24 2026 +0100 net/tcp: Honour SO_RCVTIMEO in accept(). A blocking accept() of a TCP socket waited for ever: it ignored the receive timeout (SO_RCVTIMEO). Linux ends the wait after the timeout with EAGAIN, and the usrsock accept() of NuttX does the same. Now psock_tcp_accept() waits for the receive timeout of the listener and returns EAGAIN when it expires. A connection can come after the wait ended with a timeout or a signal, before psock_tcp_accept() takes the lock of the listener again. The accept callback then gave the connection to accept() already, but accept() returned the error, and nothing freed the connection. Now accept() returns such a connection. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- net/tcp/tcp_accept.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/net/tcp/tcp_accept.c b/net/tcp/tcp_accept.c index fa49f8fd322..2b4af3e84d8 100644 --- a/net/tcp/tcp_accept.c +++ b/net/tcp/tcp_accept.c @@ -271,12 +271,17 @@ int psock_tcp_accept(FAR struct socket *psock, FAR struct sockaddr *addr, conn->accept_private = (FAR void *)&state; conn->accept = accept_eventhandler; - /* Wait for the send to complete or an error to occur: NOTES: - * conn_dev_sem_timedwait will also terminate if a signal is received. + /* Wait for a connection, a signal or the receive timeout + * (SO_RCVTIMEO). A timeout gives EAGAIN, as Linux. */ - ret = conn_dev_sem_timedwait(&state.acpt_sem, true, UINT_MAX, + ret = conn_dev_sem_timedwait(&state.acpt_sem, true, + _SO_TIMEOUT(conn->sconn.s_rcvtimeo), &conn->sconn, NULL); + if (ret == -ETIMEDOUT) + { + ret = -EAGAIN; + } /* Make sure that no further events are processed */ @@ -285,11 +290,20 @@ int psock_tcp_accept(FAR struct socket *psock, FAR struct sockaddr *addr, nxsem_destroy(&state.acpt_sem); + /* A connection can come after the end of the wait, before the lock + * is taken again: accept it, or nothing would ever free it. + */ + + if (state.acpt_newconn != NULL) + { + ret = OK; + } + /* Check for a errors. Errors are signalled by negative errno values * for the send length. */ - if (state.acpt_result != 0) + else if (state.acpt_result != 0) { DEBUGASSERT(state.acpt_result > 0); ret = -state.acpt_result;
