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 dc5df918e184d05a80aeca472599bfb30edce624
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 19 06:05:06 2026 +0100

    net/tcp: Free a closing connection when close() finds no callback.
    
    close() needs a devif callback to send the queued data and the FIN.
    When all callbacks were in use, it freed the connection at once: it
    sent no FIN, dropped the data that was still in the write queue, and
    close() returned 0.  The callbacks are often held by connections in
    TIME_WAIT that no socket uses any more.
    
    tcp_alloc() already freed such a connection when the connection pool
    was empty.  Move this fall-back to tcp_reclaim(), and call it also from
    close() when the callback allocation fails; close() then tries once
    more.  tcp_reclaim() also fixes two faults of the old fall-back:
    
    - It held the connection list lock while tcp_free() took the lock of
      the device of that connection.  The poll takes the device lock, then
      the list lock: the two could wait for each other, with one device
      (from socket()).  It also held the list lock while tcp_free() waited
      for the timer work, which takes the list lock.  tcp_reclaim() now
      takes the device lock first and calls tcp_free() after it released
      its own locks (a caller in the input path still holds the lock of its
      device).
      With the lock of another device held (a SYN for a listener), it only
      tries the lock of the device of the connection.
    
    - It could take a connection that another path was freeing at the same
      time: tcp_close_work() frees a closed connection after it dropped its
      locks, and so does close().  tcp_reclaim() now takes a connection that
      still has its close callback, or a closed one whose close work it can
      take from the work queue.  Under the device lock it frees the
      callbacks of the connection and marks it closed, so that no event uses
      it before tcp_free().
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/tcp/tcp.h       |  19 ++++
 net/tcp/tcp_close.c |  59 ++++++++++--
 net/tcp/tcp_conn.c  | 257 ++++++++++++++++++++++++++++++++++++++--------------
 3 files changed, 259 insertions(+), 76 deletions(-)

diff --git a/net/tcp/tcp.h b/net/tcp/tcp.h
index 05c2b59a18c..067d34cdbd1 100644
--- a/net/tcp/tcp.h
+++ b/net/tcp/tcp.h
@@ -484,6 +484,25 @@ extern "C"
 
 FAR struct tcp_conn_s *tcp_alloc(uint8_t domain);
 
+/****************************************************************************
+ * Name: tcp_reclaim
+ *
+ * Description:
+ *   Free the oldest connection that no socket refers to and that only
+ *   waits for the end of its close (for example in TIME_WAIT).  This gives
+ *   back the connection structure and the callbacks that it holds.
+ *
+ * Input Parameters:
+ *   dev - The device whose lock the caller holds, or NULL if it holds no
+ *         device lock and no connection lock.
+ *
+ * Returned Value:
+ *   true if a connection was freed.
+ *
+ ****************************************************************************/
+
+bool tcp_reclaim(FAR struct net_driver_s *dev);
+
 /****************************************************************************
  * Name: tcp_free_rx_buffers
  *
diff --git a/net/tcp/tcp_close.c b/net/tcp/tcp_close.c
index 80ed0e86c60..bfaee38e5d6 100644
--- a/net/tcp/tcp_close.c
+++ b/net/tcp/tcp_close.c
@@ -254,6 +254,25 @@ end_wait:
   return flags;
 }
 
+/****************************************************************************
+ * Name: tcp_close_waits
+ *
+ * Description:
+ *   Return true if a close of the connection must wait for its close
+ *   callback (to send the queued data and the FIN, or to see the FIN
+ *   acknowledged).
+ *
+ ****************************************************************************/
+
+static bool tcp_close_waits(FAR struct tcp_conn_s *conn)
+{
+  return conn->tcpstateflags == TCP_ESTABLISHED ||
+         conn->tcpstateflags == TCP_FIN_WAIT_1 ||
+         conn->tcpstateflags == TCP_FIN_WAIT_2 ||
+         conn->tcpstateflags == TCP_LAST_ACK ||
+         conn->tcpstateflags == TCP_CLOSE_WAIT;
+}
+
 /****************************************************************************
  * Name: tcp_close_disconnect
  *
@@ -274,6 +293,7 @@ end_wait:
 static inline int tcp_close_disconnect(FAR struct socket *psock)
 {
   FAR struct tcp_conn_s *conn;
+  bool reclaimed;
   int ret = OK;
 
   /* Interrupts are disabled here to avoid race conditions */
@@ -282,10 +302,6 @@ static inline int tcp_close_disconnect(FAR struct socket 
*psock)
 
   conn_dev_lock(&conn->sconn, conn->dev);
 
-  /* Discard our reference to the connection */
-
-  conn->crefs = 0;
-
   /* No socket refers to the connection any more: stop the monitor now, not
    * when the connection is freed after TIME_WAIT.  The close callback can
    * then use its callback structure.
@@ -301,12 +317,35 @@ static inline int tcp_close_disconnect(FAR struct socket 
*psock)
    *   retransmitting the FIN, before disposing the connection.
    */
 
-  if ((conn->tcpstateflags == TCP_ESTABLISHED ||
-       conn->tcpstateflags == TCP_FIN_WAIT_1 ||
-       conn->tcpstateflags == TCP_FIN_WAIT_2 ||
-       conn->tcpstateflags == TCP_LAST_ACK ||
-       conn->tcpstateflags == TCP_CLOSE_WAIT) &&
-      (conn->clscb = tcp_callback_alloc(conn)) != NULL)
+  if (tcp_close_waits(conn))
+    {
+      conn->clscb = tcp_callback_alloc(conn);
+      if (conn->clscb == NULL)
+        {
+          /* All callbacks are in use: free a connection that only waits
+           * for the end of its close (for example in TIME_WAIT), or this
+           * one goes without a FIN and its queued data is lost.
+           * tcp_reclaim() takes a device lock and the list lock: not with
+           * our locks held.  It does not take this connection: it has no
+           * close callback.
+           */
+
+          conn_dev_unlock(&conn->sconn, conn->dev);
+          reclaimed = tcp_reclaim(NULL);
+          conn_dev_lock(&conn->sconn, conn->dev);
+
+          if (reclaimed && tcp_close_waits(conn))
+            {
+              conn->clscb = tcp_callback_alloc(conn);
+            }
+        }
+    }
+
+  /* Discard our reference to the connection */
+
+  conn->crefs = 0;
+
+  if (conn->clscb != NULL)
     {
       /* Set up to receive TCP data event callbacks */
 
diff --git a/net/tcp/tcp_conn.c b/net/tcp/tcp_conn.c
index 3c6c7d038d1..b576519f8e7 100644
--- a/net/tcp/tcp_conn.c
+++ b/net/tcp/tcp_conn.c
@@ -519,6 +519,38 @@ static inline int tcp_ipv6_bind(FAR struct tcp_conn_s 
*conn,
 }
 #endif /* CONFIG_NET_IPv6 */
 
+/****************************************************************************
+ * Name: tcp_reclaimable
+ *
+ * Description:
+ *   Return true if the connection can be freed to make room: no socket
+ *   refers to it, and it is closed with its close work still queued, or
+ *   it only waits for the end of its close with its close callback.  Any
+ *   other connection without a socket is freed by tcp_close_work() or by
+ *   tcp_close(), maybe at this moment.
+ *
+ ****************************************************************************/
+
+static bool tcp_reclaimable(FAR struct tcp_conn_s *conn)
+{
+  if (conn->crefs != 0 || conn->dev == NULL)
+    {
+      return false;
+    }
+
+  if (conn->tcpstateflags == TCP_CLOSED)
+    {
+      return (conn->flags & TCP_CLOSE_ARRANGED) != 0;
+    }
+
+  return conn->clscb != NULL &&
+         (conn->tcpstateflags == TCP_CLOSING    ||
+          conn->tcpstateflags == TCP_FIN_WAIT_1 ||
+          conn->tcpstateflags == TCP_FIN_WAIT_2 ||
+          conn->tcpstateflags == TCP_TIME_WAIT  ||
+          conn->tcpstateflags == TCP_LAST_ACK);
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -618,103 +650,180 @@ int tcp_selectport(uint8_t domain,
 }
 
 /****************************************************************************
- * Name: tcp_alloc
+ * Name: tcp_reclaim
  *
  * Description:
- *   Find a free TCP/IP connection structure and allocate it
- *   for use.  This is normally something done by the implementation of the
- *   socket() API but is also called from the event processing logic when a
- *   TCP packet is received while "listening"
+ *   Free the oldest connection that no socket refers to and that only
+ *   waits for the end of its close (for example in TIME_WAIT).  This gives
+ *   back the connection structure and the callbacks that it holds.
+ *
+ * Input Parameters:
+ *   dev - The device whose lock the caller holds, or NULL if it holds no
+ *         device lock and no connection lock.
+ *
+ * Returned Value:
+ *   true if a connection was freed.
  *
  ****************************************************************************/
 
-FAR struct tcp_conn_s *tcp_alloc(uint8_t domain)
+bool tcp_reclaim(FAR struct net_driver_s *dev)
 {
-  FAR struct tcp_conn_s *conn;
+  FAR struct devif_callback_s *cb;
+  FAR struct devif_callback_s *next;
+  FAR struct net_driver_s *conndev;
+  FAR struct tcp_conn_s *conn = NULL;
+  FAR struct tcp_conn_s *tmp;
 
-  /* Because this routine is called from both event processing (with the
-   * network locked) and and from user level.  Make sure that the network
-   * locked in any cased while accessing g_free_tcp_connections[];
+  /* Search the active connection list for the oldest connection that is
+   * about to be closed anyway.
    */
 
   tcp_conn_list_lock();
+  for (tmp = (FAR struct tcp_conn_s *)g_active_tcp_connections.head;
+       tmp != NULL;
+       tmp = (FAR struct tcp_conn_s *)tmp->sconn.s_node.flink)
+    {
+      if (tcp_reclaimable(tmp) &&
+          (conn == NULL || tmp->timer < conn->timer))
+        {
+          conn = tmp;
+        }
+    }
 
-  /* Return the entry from the head of the free list */
+  conndev = conn != NULL ? conn->dev : NULL;
+  tcp_conn_list_unlock();
 
-  conn = NET_BUFPOOL_TRYALLOC(g_tcp_connections);
+  if (conn == NULL)
+    {
+      return false;
+    }
 
-  /* Is the free list empty? */
+  /* The poll takes the device lock, then the list lock: do the same here.
+   * With the lock of another device held, only try it: two such callers
+   * could wait for each other.
+   */
 
-  if (!conn)
+  if (dev == NULL || dev == conndev)
     {
-      /* As a fall-back, check for connection structures which can be
-       * stalled.
-       * Search the active connection list for the oldest connection
-       * that is about to be closed anyway.
-       */
+      netdev_lock(conndev);
+    }
+  else if (netdev_trylock(conndev) < 0)
+    {
+      return false;
+    }
+
+  /* The connection may be gone meanwhile: look again */
+
+  tcp_conn_list_lock();
+  for (tmp = (FAR struct tcp_conn_s *)g_active_tcp_connections.head;
+       tmp != NULL && tmp != conn;
+       tmp = (FAR struct tcp_conn_s *)tmp->sconn.s_node.flink)
+    {
+    }
+
+  tcp_conn_list_unlock();
+
+  if (tmp == NULL || conn->dev != conndev || !tcp_reclaimable(conn))
+    {
+      netdev_unlock(conndev);
+      return false;
+    }
 
-      FAR struct tcp_conn_s *tmp =
-        (FAR struct tcp_conn_s *)g_active_tcp_connections.head;
+  /* A closed connection: take its close work from the queue.  If the work
+   * runs already, it frees the connection itself.
+   */
 
-      while (tmp)
+  if ((conn->flags & TCP_CLOSE_ARRANGED) != 0)
+    {
+      if (work_cancel(LPWORK, &conn->clswork) < 0)
         {
-          ninfo("conn: %p state: %02x\n", tmp, tmp->tcpstateflags);
+          netdev_unlock(conndev);
+          return false;
+        }
 
-          /* Is this connection in a state we can sacrifice. */
+      conn->flags &= ~TCP_CLOSE_ARRANGED;
+    }
 
-          if ((tmp->crefs == 0) &&
-              (tmp->tcpstateflags == TCP_CLOSED    ||
-              tmp->tcpstateflags == TCP_CLOSING    ||
-              tmp->tcpstateflags == TCP_FIN_WAIT_1 ||
-              tmp->tcpstateflags == TCP_FIN_WAIT_2 ||
-              tmp->tcpstateflags == TCP_TIME_WAIT  ||
-              tmp->tcpstateflags == TCP_LAST_ACK))
-            {
-              /* Yes.. Is it the oldest one we have seen so far? */
+  nwarn("WARNING: Closing unestablished connection: %p\n", conn);
 
-              if (!conn || tmp->timer < conn->timer)
-                {
-                  /* Yes.. remember it */
+  /* No other path frees the connection now: its close work is not queued,
+   * and the events that start it need the device lock.  Take its
+   * callbacks and mark it closed: then no event uses it, and its close
+   * never starts tcp_close_work().  Release the locks taken here before
+   * tcp_free(), as tcp_close_work() does.  A caller in the input path
+   * (tcp_alloc_accept()) still holds the lock of its own device.
+   */
 
-                  conn = tmp;
-                }
-            }
+  conn_lock(&conn->sconn);
+  tcp_stop_monitor(conn, TCP_ABORT);
+  for (cb = conn->sconn.s_list; cb != NULL; cb = next)
+    {
+      next = cb->nxtconn;
+      tcp_callback_free(conn, cb);
+    }
 
-          /* Look at the next active connection */
+  conn->clscb = NULL;
+  conn->shdcb = NULL;
+#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
+  conn->sndcb = NULL;
+#endif
+  conn->tcpstateflags = TCP_CLOSED;
+  conn_unlock(&conn->sconn);
+  netdev_unlock(conndev);
 
-          tmp = (FAR struct tcp_conn_s *)tmp->sconn.s_node.flink;
-        }
+  /* No socket refers to it any more (crefs == 0) and close() does not
+   * wait for the close callback, also not with SO_LINGER, so nobody needs
+   * to be informed.
+   */
 
-      /* Did we find a connection that we can reuse? */
+  tcp_free(conn);
+  return true;
+}
 
-      if (conn != NULL)
-        {
-          nwarn("WARNING: Closing unestablished connection: %p\n", conn);
-
-          /* Yes... free it.  This will remove the connection from the list
-           * of active connections and release all resources held by the
-           * connection.
-           *
-           * No socket refers to it any more (crefs == 0) and close()
-           * does not wait for the close callback, also not with
-           * SO_LINGER, so nobody needs to be informed.
-           */
+/****************************************************************************
+ * Name: tcp_alloc_conn
+ *
+ * Description:
+ *   Find a free TCP/IP connection structure and allocate it for use.  dev
+ *   is the device whose lock the caller holds, or NULL.
+ *
+ ****************************************************************************/
 
-          tcp_free(conn);
+static FAR struct tcp_conn_s *tcp_alloc_conn(uint8_t domain,
+                                             FAR struct net_driver_s *dev)
+{
+  FAR struct tcp_conn_s *conn;
 
-          /* Now there should be one free connection. If dynamic connections
-           * allocation is disabled, it is guaranteed so. In case that
-           * dynamic connections are used, it may be already in the free
-           * list, or at least there should be enough space in the heap for
-           * a new connection.
-           */
+  /* Because this routine is called from both event processing (with the
+   * network locked) and and from user level.  Make sure that the network
+   * locked in any cased while accessing g_free_tcp_connections[];
+   */
 
-          conn = NET_BUFPOOL_TRYALLOC(g_tcp_connections);
-        }
-    }
+  tcp_conn_list_lock();
 
+  /* Return the entry from the head of the free list */
+
+  conn = NET_BUFPOOL_TRYALLOC(g_tcp_connections);
   tcp_conn_list_unlock();
 
+  /* Is the free list empty?  As a fall-back, free the oldest connection
+   * that is about to be closed anyway.
+   */
+
+  if (!conn && tcp_reclaim(dev))
+    {
+      /* Now there should be one free connection. If dynamic connections
+       * allocation is disabled, it is guaranteed so. In case that
+       * dynamic connections are used, it may be already in the free
+       * list, or at least there should be enough space in the heap for
+       * a new connection.
+       */
+
+      tcp_conn_list_lock();
+      conn = NET_BUFPOOL_TRYALLOC(g_tcp_connections);
+      tcp_conn_list_unlock();
+    }
+
   /* Mark the connection allocated */
 
   if (conn)
@@ -768,6 +877,22 @@ FAR struct tcp_conn_s *tcp_alloc(uint8_t domain)
   return conn;
 }
 
+/****************************************************************************
+ * Name: tcp_alloc
+ *
+ * Description:
+ *   Find a free TCP/IP connection structure and allocate it
+ *   for use.  This is normally something done by the implementation of the
+ *   socket() API but is also called from the event processing logic when a
+ *   TCP packet is received while "listening"
+ *
+ ****************************************************************************/
+
+FAR struct tcp_conn_s *tcp_alloc(uint8_t domain)
+{
+  return tcp_alloc_conn(domain, NULL);
+}
+
 /****************************************************************************
  * Name: tcp_free_rx_buffers
  *
@@ -1077,7 +1202,7 @@ FAR struct tcp_conn_s *tcp_alloc_accept(FAR struct 
net_driver_s *dev,
 
   /* Allocate the connection structure */
 
-  conn = tcp_alloc(domain);
+  conn = tcp_alloc_conn(domain, dev);
   if (conn)
     {
       /* Set up the local address (laddr) and the remote address (raddr)

Reply via email to