Skip to content

timing may cause socket to enter invalid state which cannot reconnect #368

@paul-szczepanek-arm

Description

@paul-szczepanek-arm

I'm running FreeRTOS in virtual environment that creates unusual timing issues which is probably why I ran into this but I believe this could also happen on real hardware.

The scenario is that I lose a connection and I try to shutdown the socket and reconnect. This fails because the socket is already in the wrong state and FreeRTOS_shutdown returns pdFREERTOS_ERRNO_EOPNOTSUPP causing the AWS stack to abort execution and failing to reconnect later as the the socket was not fully shutdown.

The reason pdFREERTOS_ERRNO_EOPNOTSUPP is returned is because the socket that loses connection can have its state change away from established before FreeRTOS_shutdown is called.

    BaseType_t FreeRTOS_shutdown( Socket_t xSocket,
                                  BaseType_t xHow )
    {
        FreeRTOS_Socket_t * pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
        BaseType_t xResult;

        if( prvValidSocket( pxSocket, FREERTOS_IPPROTO_TCP, pdTRUE ) == pdFALSE )
        {
            /*_RB_ Is this comment correct?  The socket is not of a type that
             * supports the listen() operation. */
            xResult = -pdFREERTOS_ERRNO_EOPNOTSUPP;
        }
        else if( pxSocket->u.xTCP.ucTCPState != ( uint8_t ) eESTABLISHED )
        {
            /*_RB_ Is this comment correct?  The socket is not of a type that
             * supports the listen() operation. */
            xResult = -pdFREERTOS_ERRNO_EOPNOTSUPP;
        }

The condition else if( pxSocket->u.xTCP.ucTCPState != ( uint8_t ) eESTABLISHED ) is entered.

https://github.com/sifive/Amazon-FreeRTOS/blob/3cb6cc8d0bf2551eee5eda602501b89a5d5791b2/libraries/freertos_plus/standard/freertos_plus_tcp/source/FreeRTOS_Sockets.c#L2745

The comment inside the condition makes no sense to me. It is fine for the FreeRTOS_shutdown to not do anything in this case but it should return a more useful error, something like maybe pdFREERTOS_ERRNO_ENOTCONN. This way the stack can disregard the error and interpret it as shutdown not needed so I can continue safely instead of aborting. Right now SOCKETS_Shutdown in lib/amazon_freertos/libraries/abstractions/secure_sockets/freertos_plus_tcp/iot_secure_sockets.c has no way to distinguish between the two and passes an error further up the stack causing execution to be aborted leaving the socket in unusable state.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions