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 dbd9dcb191970f181d5cee48823a90934ce23cae
Author: yangjianqing <[email protected]>
AuthorDate: Mon Jan 19 20:06:58 2026 +0800

    net/tcp:fixed the issue of tx_unacked counting exception.
    
    1. `tx_unacked` indicates that a number of bytes were sent but not yet 
acknowledged. `tx_unacked` will not change during retransmission and should not 
be manipulated.
    2. Operations on `tx_unacked` during retransmission are not atomic. After 
`conn->tx_unacked -= sent` but before `tx_unacked` is incremented, if a packet 
is received from the server, the process might enter `conn->tx_unacked < 
unackseq - ackseq, old ack`, resulting in packet loss.
    3. The BT-NET protocol stack is relatively simple and cannot detect packet 
loss during server-sent data, nor does it retransmit, leading to anomalies.
    
    Signed-off-by: yangjianqing <[email protected]>
---
 net/tcp/tcp_send_buffered.c | 26 ++++----------------------
 1 file changed, 4 insertions(+), 22 deletions(-)

diff --git a/net/tcp/tcp_send_buffered.c b/net/tcp/tcp_send_buffered.c
index 087418547fc..c6c389cee46 100644
--- a/net/tcp/tcp_send_buffered.c
+++ b/net/tcp/tcp_send_buffered.c
@@ -217,15 +217,6 @@ static void retransmit_segment(FAR struct tcp_conn_s *conn,
 
       /* Reset the number of bytes sent sent from the write buffer */
 
-      if (conn->tx_unacked > sent)
-        {
-          conn->tx_unacked -= sent;
-        }
-      else
-        {
-          conn->tx_unacked = 0;
-        }
-
       if (conn->sent > sent)
         {
           conn->sent -= sent;
@@ -958,15 +949,6 @@ static uint32_t psock_send_eventhandler(FAR struct 
net_driver_s *dev,
                * the write buffer
                */
 
-              if (conn->tx_unacked > sent)
-                {
-                  conn->tx_unacked -= sent;
-                }
-              else
-                {
-                  conn->tx_unacked = 0;
-                }
-
               if (conn->sent > sent)
                 {
                   conn->sent -= sent;
@@ -1147,11 +1129,10 @@ static uint32_t psock_send_eventhandler(FAR struct 
net_driver_s *dev,
            * number calculations.
            */
 
-          conn->tx_unacked += sndlen;
-          conn->sent       += sndlen;
+          conn->sent += sndlen;
 
-          /* Below prediction will become true,
-           * unless retransmission occurrence
+          /* Below prediction will become true, unless retransmission
+           * occurrence. tx_unacked does not update during retransmission.
            */
 
           predicted_seqno = tcp_getsequence(conn->sndseq) + sndlen;
@@ -1159,6 +1140,7 @@ static uint32_t psock_send_eventhandler(FAR struct 
net_driver_s *dev,
           if (TCP_SEQ_GT(predicted_seqno, conn->sndseq_max))
             {
               conn->sndseq_max = predicted_seqno;
+              conn->tx_unacked += sndlen;
             }
 
           ninfo("SEND: wrb=%p nrtx=%u tx_unacked=%" PRIu32

Reply via email to