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 86471bb16d9f5e3178265677fa6450d2e3a03af9
Author: Marco Casaroli <[email protected]>
AuthorDate: Tue Nov 17 11:10:23 2026 +0100

    net/tcp: Send the FIN of shutdown(SHUT_WR) after the queued data.
    
    tcp_shutdown() armed a callback that sent the FIN at the next poll of
    the device, and that callback also disabled the send callback of the
    write buffers.  Data that the application wrote before shutdown() and
    that still waited in the write queue (for example behind a zero window)
    was then lost, or sent after the FIN.  tcp_appsend() also sets
    tx_unacked to 1 for the FIN, so the two ends no longer agreed on the
    sequence numbers.  Over the loopback device this ended in an ACK storm
    between the two closing connections that took a CPU and the network
    locks.
    
    Now the shutdown callback waits, as close() does, while data waits in
    the write queue or is not yet acknowledged.  It also runs on an ACK,
    and it frees itself when the connection is lost.  As it now runs on
    input events, it no longer sets d_len to 0: an ACK can carry data for
    the reader.  A second shutdown() uses the callback that still waits.
    
    close() after shutdown(SHUT_WR) has its own callback after the shutdown
    callback.  That callback ended its wait at any TCP_TXCLOSE, also the one
    that the shutdown callback sets to send its FIN, and freed itself while
    the connection was still open: no one freed the connection after
    TIME_WAIT.  The FIN that now waits for the data made this much more
    likely.  The close callback now ends its wait at TCP_TXCLOSE only in
    TCP_CLOSED: the event of the ACK of our FIN in LAST_ACK.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/tcp/tcp_close.c    |  8 +++++++-
 net/tcp/tcp_shutdown.c | 35 ++++++++++++++++++++++++++++++++---
 2 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/net/tcp/tcp_close.c b/net/tcp/tcp_close.c
index 6acc8fdb9e0..9b2036c49c6 100644
--- a/net/tcp/tcp_close.c
+++ b/net/tcp/tcp_close.c
@@ -81,9 +81,15 @@ static uint32_t tcp_close_eventhandler(FAR struct 
net_driver_s *dev,
    *   TCP_ABORT:    The remote host has aborted the connection
    *   TCP_TIMEDOUT: The remote did not respond, the connection timed out
    *   NETDEV_DOWN:  The network device went down
+   *
+   * TCP_TXCLOSE ends the close only when the peer acknowledged our FIN in
+   * LAST_ACK (the state is TCP_CLOSED then).  The callback of shutdown()
+   * runs before this one and sets TCP_TXCLOSE to send its FIN: this
+   * callback must stay until the connection ends, or no one frees it.
    */
 
-  if ((flags & (TCP_TXCLOSE | TCP_DISCONN_EVENTS)) != 0)
+  if ((flags & TCP_DISCONN_EVENTS) != 0 ||
+      ((flags & TCP_TXCLOSE) != 0 && conn->tcpstateflags == TCP_CLOSED))
     {
       /* The disconnection is complete.  Wake up the waiting thread with an
        * appropriate result.  Success is returned in these cases:
diff --git a/net/tcp/tcp_shutdown.c b/net/tcp/tcp_shutdown.c
index f9d07dad8c6..bd7136fbd73 100644
--- a/net/tcp/tcp_shutdown.c
+++ b/net/tcp/tcp_shutdown.c
@@ -56,6 +56,31 @@ static uint32_t tcp_shutdown_eventhandler(FAR struct 
net_driver_s *dev,
 
   ninfo("flags: %" PRIx32 "\n", flags);
 
+  /* The connection is gone: nothing more to send */
+
+  if ((flags & TCP_DISCONN_EVENTS) != 0)
+    {
+      tcp_callback_free(conn, conn->shdcb);
+      conn->shdcb = NULL;
+      return flags;
+    }
+
+  /* The FIN goes after the data that the application wrote before
+   * shutdown(): wait while data waits in the write queue or is not yet
+   * acknowledged, as close() does.  The send callback still sends it.
+   * An ACK can come with new data: leave it to the reader (no d_len = 0).
+   */
+
+  if ((conn->shutdown & SHUT_WR) != 0 &&
+      (conn->tx_unacked != 0
+#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
+       || !sq_empty(&conn->write_q)
+#endif
+      ))
+    {
+      return flags;
+    }
+
 #ifdef CONFIG_NET_TCP_WRITE_BUFFERS
   /* We don't need the send callback anymore. */
 
@@ -70,7 +95,6 @@ static uint32_t tcp_shutdown_eventhandler(FAR struct 
net_driver_s *dev,
     }
 #endif
 
-  dev->d_len = 0;
   if ((conn->shutdown & SHUT_WR) != 0)
     {
       flags |= TCP_TXCLOSE;
@@ -123,7 +147,12 @@ static inline int tcp_send_fin(FAR struct socket *psock)
        conn->tcpstateflags == TCP_SYN_RCVD ||
        conn->tcpstateflags == TCP_CLOSE_WAIT))
     {
-      if ((conn->shdcb = tcp_callback_alloc(conn)) == NULL)
+      /* A callback of an earlier shutdown() may still wait for the queued
+       * data: it reads conn->shutdown when it runs, so use it again.
+       */
+
+      if (conn->shdcb == NULL &&
+          (conn->shdcb = tcp_callback_alloc(conn)) == NULL)
         {
           ret = -EBUSY;
           goto out;
@@ -131,7 +160,7 @@ static inline int tcp_send_fin(FAR struct socket *psock)
 
       /* Set up to receive TCP data event callbacks */
 
-      conn->shdcb->flags = TCP_POLL;
+      conn->shdcb->flags = TCP_POLL | TCP_ACKDATA | TCP_DISCONN_EVENTS;
       conn->shdcb->event = tcp_shutdown_eventhandler;
       conn->shdcb->priv  = conn; /* reference for event handler to free cb */
 

Reply via email to