Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


FreeRTOS+TCP : Why do sockets stay in the status eCLOSE_WAIT ?

Posted by heinbali01 on April 2, 2016

A user of FreeRTOS+TCP asked the following question:

Look at the output of FreeRTOS_netstat(). It was recorded on a K63F machine:

~~~~ Prot Port IP-Remote : Port R/T Status Alive tmout Child TCP 3123 : 0 0/0 eTCPLISTEN 0 0 0/2 TCP 80 : 0 0/0 eTCPLISTEN 0 0 3/3 TCP 80 : 55940 1/0 eCLOSEWAIT 0 19973 TCP 80 : 55942 1/0 eCLOSEWAIT 0 19975 TCP 80 : 55944 1/0 eCLOSEWAIT 0 20000 FreeRTOSnetstat: 5 sockets 2 < 3 < 3 buffers free ~~~~ is the PC running several wget instances in parallel, all connecting to K63F. Port 80 has a listen limit of 3 clients. After 1000-4000 seconds no new HTTP connections can be initiated, since there are no free "slots".

I don't understand why those closed sockets (all wgets are killed on the PC) in eCLOSE_WAIT state remain there; they should disappear, shouldn't they? There's a 'tmout' field, which reaches zero, but then the countdown start from 20000 again.

What could be the reason for these persistent, non-closing sockets?

From WiKiPedia:

    (both server and client) represents waiting for a connection
    termination request from the local user.

A socket in the status "eCLOSE_WAIT" is waiting for you! It has done all it could and it may be closed by calling FreeRTOS_closesocket().

How can you prevent this?

1) Check the return code of all API's :

~~~~ // Same for FreeRTOSsend() xRc = FreeRTOSrecv(s, b, sizeof b, 0); if( ( xRc < 0 ) && ( xRc != -pdFREERTOSERRNOEWOULDBLOCK ) ) { FreeRTOS_closesocket( s ); s = NULL; return -1; } ~~~~

Note: if a socket's connection is closed but it still has RX data, FreeRTOS_recv() will return positive values until all data have been read.

2) After your socket is connected, you may call FreeRTOS_issocketconnected() to see if it is still connected. If it returns false you can close the socket.

3) You can call FreeRTOS_connstatus() to check the status, but that is only recommended for debugging.

4) Always try to close sockets gracefully. Also under other operating systems that communicate with your embedded systems. If your program decides to close a TCP connection, follow this procedure (symbolically):

~~~~ FreeRTOSshutdown(s, 0); /* A FIN will be sent. Wait up to a few seconds for the other party to ACK our FIN. If the other party still had data to sent, it will do so. Keep on calling FreeRTOSrecv() for this socket until it returns a negative value (not equal to -EWOULDBLOCK) */

for( ;; )
    /* Only do this if reading from 's' will block. */
    xRc = FreeRTOS_recv(s, buf, sizeof buf, 0);
    if( xRc > 0 )
        /* Use the received data in 'buf'. */
    else if( ( xRc < 0 ) && ( xRc != -pdFREERTOS_ERRNO_EWOULDBLOCK ) )
        FreeRTOS_closesocket( s );
        s = NULL;


The flag for shutdown() (RD, WR, and RDWR) has not been implemented in +TCP.

PS about FreeRTOS_issocketconnected():

For an active connection, using FreeRTOS_connect(), you can call FreeRTOS_issocketconnected() as well. It will return false until the socket gets connected. Once it is connected, it will return true until the connection is closed.

For passive connections (using FreeRTOS_accept()), the socket will start in a connected state.

FreeRTOS+TCP : Why do sockets stay in the status eCLOSE_WAIT ?

Posted by tselmeci on April 5, 2016

Hello Hein!

(I was the user with the issues above)

I've applied some changes (mostly the use of FreeRTOS_issocketconnected) and since then I can't observe zombie sockets any longer. But I'm facing to other problems...

Here's my function to close TCP sockets:

~~~~ int mysocketclose(Sockett sock) { /* wait while TX buffer flushes */ while (FreeRTOSoutstanding(sock) > 0 && FreeRTOSissocketconnected(sock) == pdTRUE) vTaskDelay(pdMSTO_TICKS(20));

/* signalling that we want to gracefully break the connection up */
FreeRTOS_shutdown(sock, FREERTOS_SHUT_RDWR);
/* get rid of any accumulated data */
while ((ret = FreeRTOS_recv(sock, buff, sizeof(buff), 0)) >= 0) {
    if (ret == 0)
    return RC_OK;
else {
    printf("%s: ret = %d --> it may crash", __FUNCTION__, ret);
return RC_OK;

} ~~~~ Most of the time it works great. But sometimes the return value stored in 'ret' becomes -536818635 and the machine freezes. Maybe there's a memory write issue somewhere else in my program, but I'd be curious about your opinion...

[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

Version 10.1.0 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers


STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner