Skip to content

Conversation

@htibosch
Copy link
Contributor

@htibosch htibosch commented Feb 8, 2023

Description

If during the SYN phase, a sockets goes to the eCLOSED state, it must be closed/released. But at that moment, it may happen that the application does a successful call to FreeRTOS_accept(), and gets the ownership of the socket.
The IP-task in a meanwhile is planning to close the socket as well. See the function vSocketCloseNextTime()

Here is the proposed change:

     if( ( eTCPState == eCLOSED ) ||
         ( eTCPState == eCLOSE_WAIT ) )
     {
         /* Socket goes to status eCLOSED because of a RST.
          * When nobody owns the socket yet, delete it. */
+        vTaskSuspendAll();
         {
             if( ( pxSocket->u.xTCP.bits.bPassQueued != pdFALSE_UNSIGNED ) ||
                 ( pxSocket->u.xTCP.bits.bPassAccept != pdFALSE_UNSIGNED ) )
             {
+                if( pxSocket->u.xTCP.bits.bReuseSocket == pdFALSE_UNSIGNED )
+                {
+                    /* Make sure the the ownership will not be transferred. */
+                    pxSocket->u.xTCP.bits.bPassQueued = pdFALSE_UNSIGNED;
+                    pxSocket->u.xTCP.bits.bPassAccept = pdFALSE_UNSIGNED;
+                }
+                xTaskResumeAll();
     
                 FreeRTOS_debug_printf( ( "vTCPStateChange: Closing socket\n" ) );
     
                 if( pxSocket->u.xTCP.bits.bReuseSocket == pdFALSE_UNSIGNED )
                 {
                     /* Plan to close the socket. */
                     vSocketCloseNextTime( pxSocket );
                 }
             }
+            else
+            {
+                xTaskResumeAll();
+            }
         }
     }

The application can "take" the socket until vTaskSuspendAll() is called. After that the IP-task will "take the ownership" by clearing both bPass bits.

This PR was tested in a set-up where the DUT received a TCP reset packet during the SYN phase.

Thanks Paul Helter, who first discovered and described this problem in PR #571. Mentioned PR was not merged because I missed a proper diagnosis of the cause. Only last week we saw exactly what happened, and could repair it.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@htibosch htibosch requested a review from a team as a code owner February 8, 2023 04:38
@amazonKamath
Copy link
Member

Closing this PR since PR #707 took care of fixing unit tests on top of this PR and got merged.

@htibosch htibosch deleted the Make_sure_socket_is_closed_only_once branch September 17, 2025 06:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants