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 aa3401b7a4f8b918ad53a0f9f34fa756e4b20722
Author: wenquan1 <[email protected]>
AuthorDate: Mon Jan 12 10:43:03 2026 +0800

    net/tcp: fix passive established conn cannot be free if not accepted
    
    Fixed the resource leak issue.
    
    Signed-off-by: wenquan1 <[email protected]>
---
 net/tcp/tcp_backlog.c |  2 ++
 net/tcp/tcp_conn.c    | 13 +++++--------
 net/tcp/tcp_input.c   | 43 ++++++++++++++++++++++++++++++++++++++++++-
 net/tcp/tcp_listen.c  |  3 +++
 4 files changed, 52 insertions(+), 9 deletions(-)

diff --git a/net/tcp/tcp_backlog.c b/net/tcp/tcp_backlog.c
index e9ab1360a8a..1a9a7963d93 100644
--- a/net/tcp/tcp_backlog.c
+++ b/net/tcp/tcp_backlog.c
@@ -258,6 +258,7 @@ int tcp_backlogadd(FAR struct tcp_conn_s *conn,
            */
 
           blc->bc_conn = blconn;
+          blconn->blparent = conn;
           sq_addlast(&blc->bc_node, &bls->bl_pending);
           ret = OK;
         }
@@ -341,6 +342,7 @@ FAR struct tcp_conn_s *tcp_backlogremove(FAR struct 
tcp_conn_s *conn)
 
           blconn       = blc->bc_conn;
           blc->bc_conn = NULL;
+          blconn->blparent = NULL;
           sq_addlast(&blc->bc_node, &bls->bl_free);
         }
     }
diff --git a/net/tcp/tcp_conn.c b/net/tcp/tcp_conn.c
index 9e2e8ee6e0f..43675bc4a66 100644
--- a/net/tcp/tcp_conn.c
+++ b/net/tcp/tcp_conn.c
@@ -892,6 +892,8 @@ void tcp_free(FAR struct tcp_conn_s *conn)
 #endif
 
 #ifdef CONFIG_NET_TCPBACKLOG
+  tcp_conn_list_lock();
+
   /* Remove any backlog attached to this connection */
 
   if (conn->backlog)
@@ -899,16 +901,11 @@ void tcp_free(FAR struct tcp_conn_s *conn)
       tcp_backlogdestroy(conn);
     }
 
-  /* If this connection is, itself, backlogged, then remove it from the
-   * parent connection's backlog list.
-   */
-
-  if (conn->blparent)
-    {
-      tcp_backlogdelete(conn->blparent, conn);
-    }
+  tcp_conn_list_unlock();
 #endif
 
+  nxrmutex_destroy(&conn->sconn.s_lock);
+
   /* Mark the connection available. */
 
   conn->tcpstateflags = TCP_CLOSED;
diff --git a/net/tcp/tcp_input.c b/net/tcp/tcp_input.c
index 07c7dcf05ba..322ed9c9dae 100644
--- a/net/tcp/tcp_input.c
+++ b/net/tcp/tcp_input.c
@@ -1082,8 +1082,49 @@ found:
         }
       else
         {
-          conn->tcpstateflags = TCP_CLOSED;
           nwarn("WARNING: RESET TCP state: TCP_CLOSED\n");
+#ifdef CONFIG_NET_TCPBACKLOG
+          /* Race condition:
+           * case1) blparent is in close progress
+           * case2) conn is in accept progress
+           */
+
+          tcp_conn_list_lock();
+          if (conn->blparent != NULL)
+            {
+              bool orphaned = false;
+              /* If this connection is, itself, backlogged,
+               * then remove it from the parent connection's
+               * backlog list.
+               */
+
+              conn_lock(&conn->blparent->sconn);
+
+              /* Double check the connection is in accept progress */
+
+              if (conn->blparent != NULL)
+                {
+                  tcp_backlogdelete(conn->blparent, conn);
+                  orphaned = true;
+                }
+
+              conn_unlock(&conn->blparent->sconn);
+
+              if (orphaned)
+                {
+                  tcp_conn_list_unlock();
+                  DEBUGASSERT(conn->crefs == 1);
+                  conn->tcpstateflags = TCP_CLOSED;
+                  conn->crefs = 0;
+                  tcp_free(conn);
+                  goto drop;
+                }
+            }
+
+          tcp_conn_list_unlock();
+#endif
+
+          conn->tcpstateflags = TCP_CLOSED;
 
           /* Notify this connection of the reset event */
 
diff --git a/net/tcp/tcp_listen.c b/net/tcp/tcp_listen.c
index 122f66f7b32..3c200725ec3 100644
--- a/net/tcp/tcp_listen.c
+++ b/net/tcp/tcp_listen.c
@@ -54,6 +54,7 @@
 #include <nuttx/net/net.h>
 
 #include "devif/devif.h"
+#include "utils/utils.h"
 #include "inet/inet.h"
 #include "tcp/tcp.h"
 
@@ -271,7 +272,9 @@ int tcp_accept_connection(FAR struct net_driver_s *dev,
            * available.
            */
 
+          conn_lock(&listener->sconn);
           ret = tcp_backlogadd(listener, conn);
+          conn_unlock(&listener->sconn);
           if (ret == OK)
             {
               tcp_callback(dev, listener, TCP_BACKLOG);

Reply via email to