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

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


The following commit(s) were added to refs/heads/master by this push:
     new 536de664bd3 net/local: Keep the lock while a listener frees its 
waiting connections.
536de664bd3 is described below

commit 536de664bd3f9fc53e442deb0aaf0cef059ba71b
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 26 15:36:56 2026 +0100

    net/local: Keep the lock while a listener frees its waiting connections.
    
    The callers of local_release() hold the lock of the local sockets, and
    local_release() does not take it (0ed9e99c198).  But when a listening
    socket closes, local_release() released the lock around local_subref()
    for each connection that waits for accept() (8c2f25343b1).  So
    local_release() and local_free() of that connection ran without the
    lock, and local_free() removes the connection from g_local_connections
    and disconnects its peer.
    
    In this time another thread can use the local sockets.  For example, a
    client that wakes up when its connection ends can connect() again to the
    listener that closes: the connect() succeeds, and the close() then ends
    the new connection too.  Two threads can also change
    g_local_connections at the same time.
    
    Keep the lock held.  local_subref() and local_release() do not take the
    lock again, so the call does not wait for itself.  The loop still takes
    each connection from the queue before it frees it.  Now a connect() in
    this time waits for the close(), and then fails with ECONNREFUSED or
    ENOENT.
    
    The behavior is the same in all build modes.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/local/local_release.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/net/local/local_release.c b/net/local/local_release.c
index 8390a546ad7..1fe3051b5a9 100644
--- a/net/local/local_release.c
+++ b/net/local/local_release.c
@@ -79,17 +79,16 @@ int local_release(FAR struct local_conn_s *conn)
       DEBUGASSERT(conn->lc_proto == SOCK_STREAM);
 
       /* Are there still clients waiting for a connection to the server?
-       * local_subref() can come back to this function for the accept
-       * connection, and that call takes the lock.
+       * local_subref() comes back to this function for the accept
+       * connection.  The lock stays held: local_free() takes the
+       * connection from g_local_connections and unlinks its peer.
        */
 
       while ((waiter = dq_remfirst(&conn->u.server.lc_waiters)) != NULL)
         {
           accept = container_of(waiter, struct local_conn_s,
                                 u.accept.lc_waiter);
-          local_unlock();
           local_subref(accept);
-          local_lock();
         }
 
       conn->u.server.lc_pending = 0;

Reply via email to