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

commit 93d3ce3766217d3f27d2fcf49d5ea52c5db09253
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 26 15:36:08 2026 +0100

    net/tcp: Reset the pending connections of a backlog.
    
    When a listener closes, or listen() again makes its backlog smaller,
    the backlog freed each pending connection without a segment to the peer
    ("REVISIT -- such connections really need to be gracefully closed").
    The peer stays in ESTABLISHED, or in FIN_WAIT_2 when it closed its
    socket and the pending connection acknowledged its FIN.  It does not
    learn that the connection is gone until it sends data.
    
    tcp_close_abort() now ends a connection that no socket refers to with a
    RST at the next poll of its device, and then frees it.  A connection in
    another state than ESTABLISHED or CLOSE_WAIT, or without a device, is
    freed at once, as before.  The backlog uses it for each pending
    connection.  Linux also sends a RST to each pending connection when a
    listener closes.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/tcp/tcp.h         | 19 +++++++++++
 net/tcp/tcp_backlog.c | 15 +++++----
 net/tcp/tcp_close.c   | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 115 insertions(+), 7 deletions(-)

diff --git a/net/tcp/tcp.h b/net/tcp/tcp.h
index 1acf79caf8d..53689d0b4f3 100644
--- a/net/tcp/tcp.h
+++ b/net/tcp/tcp.h
@@ -1003,6 +1003,25 @@ ssize_t tcp_reset_report(FAR struct tcp_conn_s *conn, 
ssize_t ret);
 
 int tcp_close(FAR struct socket *psock);
 
+/****************************************************************************
+ * Name: tcp_close_abort
+ *
+ * Description:
+ *   End a connection that no socket refers to (a pending connection of a
+ *   backlog) with a RST to the peer, then free it.  The RST goes out at the
+ *   next poll of the device.  A connection in another state than
+ *   ESTABLISHED or CLOSE_WAIT, or without a device, is freed at once.
+ *
+ * Input Parameters:
+ *   conn - The TCP connection
+ *
+ * Assumptions:
+ *   Called from normal task logic without a lock of the network held.
+ *
+ ****************************************************************************/
+
+void tcp_close_abort(FAR struct tcp_conn_s *conn);
+
 /****************************************************************************
  * Name: tcp_shutdown
  *
diff --git a/net/tcp/tcp_backlog.c b/net/tcp/tcp_backlog.c
index cf6904d8dd4..98ad1392e97 100644
--- a/net/tcp/tcp_backlog.c
+++ b/net/tcp/tcp_backlog.c
@@ -206,11 +206,12 @@ FAR struct tcp_backlog_s *tcp_backlogdetach(FAR struct 
tcp_conn_s *conn)
  *   Free a backlog that tcp_backlogdetach() returned, and the pending
  *   connections in it.
  *
- *   NOTE: This function re-enters tcp_free() for each pending connection.
+ *   NOTE: Each pending connection is ended with tcp_close_abort(), which
+ *   frees it (tcp_free()) at once or after its RST.
  *
  * Assumptions:
- *   The caller holds no lock of the network: tcp_free() takes the device
- *   lock of each pending connection, and the input path takes the TCP
+ *   The caller holds no lock of the network: tcp_close_abort() takes the
+ *   device lock of each pending connection, and the input path takes the TCP
  *   connection list lock under a device lock.
  *
  ****************************************************************************/
@@ -233,13 +234,13 @@ void tcp_backlogfree(FAR struct tcp_backlog_s *blg)
       blconn = blc->bc_conn;
       if (blconn)
         {
-          /* REVISIT
-           * -- such connections really need to be gracefully closed
+          /* No socket will accept this connection: end it with a RST to
+           * the peer (as Linux at the close of a listener), so that the
+           * peer does not wait for it for ever.
            */
 
           blconn->backlog = NULL;
-          blconn->crefs   = 0;
-          tcp_free(blconn);
+          tcp_close_abort(blconn);
         }
     }
 
diff --git a/net/tcp/tcp_close.c b/net/tcp/tcp_close.c
index 65d115ae6a7..2dbb557c437 100644
--- a/net/tcp/tcp_close.c
+++ b/net/tcp/tcp_close.c
@@ -285,6 +285,47 @@ static bool tcp_close_waits(FAR struct tcp_conn_s *conn)
          conn->tcpstateflags == TCP_CLOSE_WAIT;
 }
 
+/****************************************************************************
+ * Name: tcp_close_abort_eventhandler
+ ****************************************************************************/
+
+static uint32_t tcp_close_abort_eventhandler(FAR struct net_driver_s *dev,
+                                             FAR void *pvpriv,
+                                             uint32_t flags)
+{
+  FAR struct tcp_conn_s *conn = pvpriv;
+
+  ninfo("flags: %" PRIx32 "\n", flags);
+
+  /* Drop received data.  Unless the peer reset the connection, it timed
+   * out or the device went down, send the RST now (tcp_appsend() sets
+   * TCP_CLOSED).
+   */
+
+  dev->d_len = 0;
+  flags &= ~TCP_NEWDATA;
+  if ((flags & TCP_DISCONN_EVENTS) == 0)
+    {
+      flags |= TCP_ABORT;
+    }
+  else
+    {
+      conn->tcpstateflags = TCP_CLOSED;
+    }
+
+  if (conn->clscb != NULL)
+    {
+      tcp_callback_free(conn, conn->clscb);
+      conn->clscb = NULL;
+    }
+
+  /* Free the connection after this event */
+
+  conn->flags |= TCP_CLOSE_ARRANGED;
+  work_queue(LPWORK, &conn->clswork, tcp_close_work, conn, 0);
+  return flags;
+}
+
 /****************************************************************************
  * Name: tcp_close_disconnect
  *
@@ -462,4 +503,51 @@ int tcp_close(FAR struct socket *psock)
   return tcp_close_disconnect(psock);
 }
 
+/****************************************************************************
+ * Name: tcp_close_abort
+ *
+ * Description:
+ *   End a connection that no socket refers to (a pending connection of a
+ *   backlog) with a RST to the peer, then free it.  The RST goes out at the
+ *   next poll of the device.  A connection in another state than
+ *   ESTABLISHED or CLOSE_WAIT, or without a device, is freed at once.
+ *
+ * Input Parameters:
+ *   conn - The TCP connection
+ *
+ * Assumptions:
+ *   Called from normal task logic without a lock of the network held.
+ *
+ ****************************************************************************/
+
+void tcp_close_abort(FAR struct tcp_conn_s *conn)
+{
+  FAR struct net_driver_s *dev = conn->dev;
+  uint8_t state;
+
+  conn_dev_lock(&conn->sconn, dev);
+  conn->crefs = 0;
+
+  /* tcp_poll() gives TCP_POLL only in these states to a connection that
+   * did not send a FIN.
+   */
+
+  state = conn->tcpstateflags & TCP_STATE_MASK;
+  if (dev != NULL &&
+      (state == TCP_ESTABLISHED || state == TCP_CLOSE_WAIT) &&
+      (conn->clscb = tcp_callback_alloc(conn)) != NULL)
+    {
+      conn->clscb->flags = TCP_NEWDATA | TCP_ACKDATA | TCP_POLL |
+                           TCP_DISCONN_EVENTS;
+      conn->clscb->event = tcp_close_abort_eventhandler;
+      conn->clscb->priv  = conn;
+      netdev_txnotify_dev(dev, TCP_POLL);
+      conn_dev_unlock(&conn->sconn, dev);
+      return;
+    }
+
+  conn_dev_unlock(&conn->sconn, dev);
+  tcp_free(conn);
+}
+
 #endif /* CONFIG_NET_TCP */

Reply via email to