# TLS\_FreeRTOS\_Disconnect takes more than 30 secs if a delay is not provided before the function call

**URL:** <https://forums.freertos.org/t/tls-freertos-disconnect-takes-more-than-30-secs-if-a-delay-is-not-provided-before-the-function-call/19248>\
**Category:** Renesas\
**Created:** [February 22, 2024, 3:34pm UTC](https://forums.freertos.org/t/tls-freertos-disconnect-takes-more-than-30-secs-if-a-delay-is-not-provided-before-the-function-call/19248 "2024-02-22T15:34:38Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![siddharthtaunk](https://avatars.discourse-cdn.com/v4/letter/s/df705f/32.png) [@siddharthtaunk](https://forums.freertos.org/u/siddharthtaunk)\
**Post date:** [February 22, 2024, 3:34pm UTC](https://forums.freertos.org/t/tls-freertos-disconnect-takes-more-than-30-secs-if-a-delay-is-not-provided-before-the-function-call/19248/1 "2024-02-22T15:34:38Z")

</div>

Hi,  
I have a code where after completing the operations, want to disconnect an established TLS connection with TLS\_FreeRTOS\_Disconnect

The issue is:  
**Delay (\>10 msec)**  
**TLS\_FreeRTOS\_Disconnect**  
Disconnection time = **15 msec**

**No delay**  
**TLS\_FreeRTOS\_Disconnect**  
Disconnection time = **45 secs**

is the delay required? if so can you please help me with the documentation/explanation for why the delay is required?

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [February 25, 2024, 1:41am UTC](https://forums.freertos.org/t/tls-freertos-disconnect-takes-more-than-30-secs-if-a-delay-is-not-provided-before-the-function-call/19248/2 "2024-02-25T01:41:43Z")

</div>

Apologies but you will have to provide more information before anybody can suggest a solution.

> [@siddharthtaunk](#):
>
> have a code where after completing the operations, want to disconnect an established TLS connection

What is being disconnected? Is it an existing TLS connection?

> [@siddharthtaunk](#):
>
> TLS\_FreeRTOS\_Disconnect

Which TCP/IP stack are you using, and which version? How is this function implemented?

> [@siddharthtaunk](#):
>
> The issue is:  
> **Delay (\>10 msec)**  
> **TLS\_FreeRTOS\_Disconnect**  
> Disconnection time = **15 msec**
> 
> **No delay**  
> **TLS\_FreeRTOS\_Disconnect**  
> Disconnection time = **45 secs**

Where is the delay - is it something you are measuring? Or a delay you are adding into the code?

---

<div class="post-metadata">

**Author:** ![siddharthtaunk](https://avatars.discourse-cdn.com/v4/letter/s/df705f/32.png) [@siddharthtaunk](https://forums.freertos.org/u/siddharthtaunk)\
**Post date:** [February 25, 2024, 4:33pm UTC](https://forums.freertos.org/t/tls-freertos-disconnect-takes-more-than-30-secs-if-a-delay-is-not-provided-before-the-function-call/19248/3 "2024-02-25T16:33:25Z")

</div>

Hi,

So sorry if I wasn’t clear with my question.

Let me put a piece of code to explain the scenario in detail

```auto
            APP_PRINT("before TLS_FreeRTOS_Disconnect\r\n");
            R_BSP_SoftwareDelay(50, BSP_DELAY_UNITS_MILLISECONDS);
            gpt_init_data_process(0,1); - Starts the timer
            TLS_FreeRTOS_Disconnect (&xNetworkContext);
            gpt_init_data_process(1,1); - Stops the timer and calculate the elapsed time
            APP_PRINT(" after TLS_FreeRTOS_Disconnect\r\n");

```

With the above peice of code the execution time of TLS\_FreeRTOS\_Disconnect function takes 15 ms

```auto
> APP_PRINT("before TLS_FreeRTOS_Disconnect\r\n");
> gpt_init_data_process(0,1); - Starts the timer
> TLS_FreeRTOS_Disconnect (&xNetworkContext);
> gpt_init_data_process(1,1); - Stops the timer and calculate the elapsed time
> APP_PRINT(" after TLS_FreeRTOS_Disconnect\r\n");

```

With no delay before TLS\_FreeRTOS\_Disconnect the execution time increases to 45 secs

PN: TLS\_FreeRTOS\_Connect function executes successfully and we are able to connect to the server too.

Here is the implementation for TLS\_FreeRTOS\_Disconnect

```auto
void TLS_FreeRTOS_Disconnect( NetworkContext_t * pNetworkContext )
{
    TlsTransportParams_t * pTlsTransportParams = NULL;
    BaseType_t tlsStatus = 0;

    if( ( pNetworkContext != NULL ) && ( pNetworkContext->pParams != NULL ) )
    {
        pTlsTransportParams = pNetworkContext->pParams;
        /* Attempting to terminate TLS connection. */
        tlsStatus = ( BaseType_t ) mbedtls_ssl_close_notify( &( pTlsTransportParams->sslContext.context ) );

        /* Ignore the WANT_READ and WANT_WRITE return values. */
        if( ( tlsStatus != ( BaseType_t ) MBEDTLS_ERR_SSL_WANT_READ ) &&
            ( tlsStatus != ( BaseType_t ) MBEDTLS_ERR_SSL_WANT_WRITE ) )
        {
            if( tlsStatus == 0 )
            {
                LogInfo( ( "(Network connection %p) TLS close-notify sent.",
                           pNetworkContext ) );
            }
            else
            {
                LogError( ( "(Network connection %p) Failed to send TLS close-notify: mbedTLSError= %s : %s.",
                            pNetworkContext,
                            mbedtlsHighLevelCodeOrDefault( tlsStatus ),
                            mbedtlsLowLevelCodeOrDefault( tlsStatus ) ) );
            }
        }

        /* Call socket shutdown function to close connection. */
        TCP_Sockets_Disconnect( pTlsTransportParams->tcpSocket );

        /* Free mbed TLS contexts. */
        sslContextFree( &( pTlsTransportParams->sslContext ) );
    }
}

```

The versions are specific to renesas

```auto
|FreeRTOS|v10.6.1+fsp.5.2.0.beta.0|
|FreeRTOS - Memory Management - Heap 4|v10.6.1+fsp.5.2.0.beta.0|
|Arm mbed TLS|v3.4.0+renesas.5.fsp.5.2.0.beta.0|
|AWS PKCS11 to mbedTLS|v3.5.0+fsp.5.2.0.beta.0|

```

---

<div class="post-metadata">

**Author:** ![RAc](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rac/32/1389_2.png) [@RAc](https://forums.freertos.org/u/RAc)\
**Post date:** [February 25, 2024, 5:25pm UTC](https://forums.freertos.org/t/tls-freertos-disconnect-takes-more-than-30-secs-if-a-delay-is-not-provided-before-the-function-call/19248/4 "2024-02-25T17:25:04Z")

</div>

post obsoleted, can not delete

---

<div class="post-metadata">

**Author:** ![siddharthtaunk](https://avatars.discourse-cdn.com/v4/letter/s/df705f/32.png) [@siddharthtaunk](https://forums.freertos.org/u/siddharthtaunk)\
**Post date:** [February 25, 2024, 5:27pm UTC](https://forums.freertos.org/t/tls-freertos-disconnect-takes-more-than-30-secs-if-a-delay-is-not-provided-before-the-function-call/19248/5 "2024-02-25T17:27:16Z")

</div>

My apologies, It is 45 seconds if we don’t add the delay

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [February 25, 2024, 9:40pm UTC](https://forums.freertos.org/t/tls-freertos-disconnect-takes-more-than-30-secs-if-a-delay-is-not-provided-before-the-function-call/19248/6 "2024-02-25T21:40:48Z")

</div>

45 seconds, or milliseconds?

I would guess the delay is because the connection is completing something before the disconnect operation can proceed. Putting the delay in just happens to ensure whatever that operation is completes before the disconnect operation starts. That could explain a 45ms delay, but not a 45 second delay. What is happening on the socket before you attempt to disconnect it?

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [February 26, 2024, 10:24am UTC](https://forums.freertos.org/t/tls-freertos-disconnect-takes-more-than-30-secs-if-a-delay-is-not-provided-before-the-function-call/19248/7 "2024-02-26T10:24:15Z")

</div>

How many other tasks do you have at the same priority as the one calling `TLS_FreeRTOS_Disconnect`? Can you try increasing the priority of this task? Also, is it possible to capture network trace to see when the actual disconnect is sent?

---

<div class="post-metadata">

**Author:** ![kstribrn](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/kstribrn/32/6511_2.png) [@kstribrn](https://forums.freertos.org/u/kstribrn)\
**Post date:** [February 26, 2024, 8:27pm UTC](https://forums.freertos.org/t/tls-freertos-disconnect-takes-more-than-30-secs-if-a-delay-is-not-provided-before-the-function-call/19248/8 "2024-02-26T20:27:40Z")

</div>

Another idea would be to place timestamped logs (you could even use the FreeRTOS tick count) around various calls made in `TLS_FreeRTOS_Disconnect`. This might shed some light onto which call is taking 45 seconds. If the calling task is not being pre-empted or interrupted then this will could be a simple way to figure out which function call to research further.

45 seconds is quite the delay though…

---

<div class="post-metadata">

**Author:** ![siddharthtaunk](https://avatars.discourse-cdn.com/v4/letter/s/df705f/32.png) [@siddharthtaunk](https://forums.freertos.org/u/siddharthtaunk)\
**Post date:** [February 27, 2024, 7:04am UTC](https://forums.freertos.org/t/tls-freertos-disconnect-takes-more-than-30-secs-if-a-delay-is-not-provided-before-the-function-call/19248/10 "2024-02-27T07:04:34Z")

</div>

I did increase the priority with no affect.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [February 27, 2024, 12:11pm UTC](https://forums.freertos.org/t/tls-freertos-disconnect-takes-more-than-30-secs-if-a-delay-is-not-provided-before-the-function-call/19248/11 "2024-02-27T12:11:22Z")

</div>

Did you try @kstribrn’s suggestion of adding timestamps to logs? Are you able to capture network trace?

---

<div class="post-metadata">

**Author:** ![siddharthtaunk](https://avatars.discourse-cdn.com/v4/letter/s/df705f/32.png) [@siddharthtaunk](https://forums.freertos.org/u/siddharthtaunk)\
**Post date:** [June 17, 2024, 5:53am UTC](https://forums.freertos.org/t/tls-freertos-disconnect-takes-more-than-30-secs-if-a-delay-is-not-provided-before-the-function-call/19248/12 "2024-06-17T05:53:33Z")

</div>

When checked the wireshark logs. the Network disconnect happens instantly. but the Freertos takes very long time to respond.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [June 17, 2024, 7:23am UTC](https://forums.freertos.org/t/tls-freertos-disconnect-takes-more-than-30-secs-if-a-delay-is-not-provided-before-the-function-call/19248/13 "2024-06-17T07:23:52Z")

</div>

> [@siddharthtaunk](#):
>
> the Network disconnect happens instantly. but the Freertos takes very long time to respond.

Thank you for sharing that. We need to figure out what is causing this delay - can you try to break the code in debugger and see what it is doing in those 45 seconds? Is that task calling `TLS_FreeRTOS_Disconnect` ready or is it blocked? Which task is running? Also, as I asked before, did you try @kstribrn’s suggestion of adding timestamps to logs?

Another way could be to integrate [a tracing tool](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/.github/third_party_tools.md#tracing-tools) to figure out what is happening in those 45 seconds.

---

<div class="post-metadata">

**Author:** ![kstribrn](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/kstribrn/32/6511_2.png) [@kstribrn](https://forums.freertos.org/u/kstribrn)\
**Post date:** [June 19, 2024, 5:15pm UTC](https://forums.freertos.org/t/tls-freertos-disconnect-takes-more-than-30-secs-if-a-delay-is-not-provided-before-the-function-call/19248/14 "2024-06-19T17:15:06Z")

</div>

To build off of what Gaurav is mentioning…Figuring out why this delay occurs is going to happen in a few steps. The first being discovering which function/task/socket is causing the 45 second delay. Once this has been discovered then we can being drilling down.

If you can give us this information we should be able to get you to a solution.
