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 1a1a3355fbbc177367663288d9a6cd493d99e86e
Author: Marco Casaroli <[email protected]>
AuthorDate: Wed Nov 18 11:13:06 2026 +0100

    net/tcp: Close a closing connection when its device goes down.
    
    After close() the close callback of a connection waits for its end.  On
    NETDEV_DOWN the callback frees itself and queues tcp_close_work(), but
    tcp_close_work() frees only a connection in TCP_CLOSED, and NETDEV_DOWN
    does not change the state.  A connection in TIME_WAIT (or FIN_WAIT_1,
    FIN_WAIT_2, CLOSING, LAST_ACK) thus kept its state with no callback;
    when its timer ended, it became TCP_CLOSED with no reference and stayed
    in the active list.  With dynamic connections (CONFIG_NET_TCP_ALLOC_
    CONNS) tcp_alloc() never takes it back: each "ifdown" or loss of carrier
    keeps all connections that close at that time, and the heap grows.
    
    Now the close callback sets TCP_CLOSED on NETDEV_DOWN, so that
    tcp_close_work() frees the connection.  No socket refers to it, and the
    device sends nothing more.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/tcp/tcp_close.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/net/tcp/tcp_close.c b/net/tcp/tcp_close.c
index bfaee38e5d6..65d115ae6a7 100644
--- a/net/tcp/tcp_close.c
+++ b/net/tcp/tcp_close.c
@@ -144,6 +144,18 @@ static uint32_t tcp_close_eventhandler(FAR struct 
net_driver_s *dev,
        *   NETWORK_DOWN event is processed further.
        */
 
+      if ((flags & NETDEV_DOWN) != 0)
+        {
+          /* No socket refers to the connection and its device sends
+           * nothing more.  Close it now: tcp_close_work() frees only a
+           * connection in TCP_CLOSED, and after this callback is freed no
+           * one frees it.  A connection in TIME_WAIT, for example, would
+           * become TCP_CLOSED later and stay in the active list.
+           */
+
+          conn->tcpstateflags = TCP_CLOSED;
+        }
+
       goto end_wait;
     }
 

Reply via email to