# FreeRTOS\_send gets stuck replying 0

**URL:** <https://forums.freertos.org/t/freertos-send-gets-stuck-replying-0/6150>\
**Category:** Kernel\
**Created:** [August 30, 2016, 3:17pm UTC](https://forums.freertos.org/t/freertos-send-gets-stuck-replying-0/6150 "2016-08-30T15:17:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [August 30, 2016, 3:17pm UTC](https://forums.freertos.org/t/freertos-send-gets-stuck-replying-0/6150/1 "2016-08-30T15:17:08Z")

</div>

**[dibosco](https://sourceforge.net/u/dibosco/)** wrote on [Tuesday, August 30, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/07900148/#bd20):

Folks,

I have a situation where, ocassionally, FreeRTOS\_send() returns 0.

I followed the premise on this page:

[http://www.freertos.org/FreeRTOS-Plus/FreeRTOS\_Plus\_TCP/TCP\_Networking\_Tutorial\_Sending\_TCP\_Data.html](http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/TCP_Networking_Tutorial_Sending_TCP_Data.html)

However, very ocassionally, I find that in this part:

if( FreeRTOS\_connect( xSocket, &xRemoteAddress, sizeof( xRemoteAddress ) ) == 0 )  
{  
/\* Keep sending until the entire buffer has been sent. _/  
while( xAlreadyTransmitted \< xTotalLengthToSend )  
{  
/_ How many bytes are left to send? _/  
xLenToSend = xTotalLengthToSend - xAlreadyTransmitted;  
xBytesSent = FreeRTOS\_send( /_ The socket being sent to. _/  
xSocket,  
/_ The data being sent. _/  
&( pcBufferToTransmit[xAlreadyTransmitted] ),  
/_ The remaining length of data to send. _/  
xLenToSend,  
/_ ulFlags. \*/  
0 );

```
    if( xBytesSent >= 0 )
    {
        /* Data was sent successfully. */
        xAlreadyTransmitted += xBytesSent;
    }
    else
    {
        /* Error - break out of the loop for graceful socket close. */
        break;
    }
}

```

}

The xBytesSent is _always_ zero and therefore the thread gets stuck infinitely in that bit of the code and the whole web server just stops responding,

I changed the line from:

if( xBytesSent \>= 0 )

to

if( xBytesSent \> 0 )

And that seems to stop the thread from locking up, but I very ocassionally get my auto-refresh page not responding and have to manually refresh the page, then all is well again.

I’m using a version of the TCP/IP stack that is around a year old. Is it worth updating it? Or do these symptoms point to something more sinister?

If it’s of any help I did step into the code and found that the zero return value was here:

```
else if( pxSocket->u.xTcp.bits.bFinSent )
{
    /* This TCP connection is closing already, the FIN flag has been sent.
    Maybe it is still delivering or receiving data.
    Return OK in order not to get closed/deleted too quickly */
    xResult = 0;
}

```

Which is in prvTCPSendCheck() which is called by FreeRTOS\_send().

Many thanks!

Rob

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [August 30, 2016, 3:43pm UTC](https://forums.freertos.org/t/freertos-send-gets-stuck-replying-0/6150/2 "2016-08-30T15:43:23Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Tuesday, August 30, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/07900148/#ab7d):

Hi Rob,

There is a newer release available of the +TCP library than the one that you are using:

[http://www.freertos.org/FreeRTOS-Labs/RTOS\_labs\_download.html](http://www.freertos.org/FreeRTOS-Labs/RTOS_labs_download.html)

Just thinking out loud: if the FIN has been sent (`bFinSent`), then the connection is in a closing state.  
Apparently, the peer has not yet acknowledged the FIN, because `ucTCPState != eCLOSED` yet, the connection isn’t closed.

It would be interesting to see a PCAP dump of that conversation in which send() hangs. Can you create that?

On itself, your code seems correct: eventually `FreeRTOS_send()` will either send all data, or the connection gets closed and `FreeRTOS_send()` will return a negative errno code.

If `FreeRTOS_send()` returns 0, it means that the buffers are full. Does the call still block when nothing can be sent? Isn’t it just spinning around on a too high priority?

Do you have these enabled?

```auto
    #define ipconfigTCP_HANG_PROTECTION 1
    #define ipconfigTCP_KEEP_ALIVE 1

```

Not sure if it would help you with this problem though 🙂

What does your code do after sending the file? After the last successful call to `FreeRTOS_send()`? Do you wait for the data to be properly delivered?  
Do you use the same socket for more transactions?

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 1, 2016, 12:50pm UTC](https://forums.freertos.org/t/freertos-send-gets-stuck-replying-0/6150/3 "2016-09-01T12:50:41Z")

</div>

**[dibosco](https://sourceforge.net/u/dibosco/)** wrote on [Thursday, September 01, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/07900148/#ab7d/9a61):

Hi Hein,

I have attached an instance where the auto page request is sent out and  
nothing is returned to the browser.

Eventually, I hit the little x on the right of the URL bar in Firefox,  
re-request the page by hitting the curved arrow and it works fine.

In twenty minutes of the auto request page being active, it fails to  
reply once or twice.

I would say, this isn’t sending a file, this is updating the web browser  
where the system is up to, when setting up the addresses on a pair of  
DALI buses. A process that takes around twenty minutes.

Both of those #defines are enabled, yes.

I’ll try the latest version of the stack.

Thanks!

Rob

On 30/08/16 16:43, Hein Tibosch wrote:

> Hi Rob,
> 
> There is a newer release available of the +TCP library than the one that  
> you are using:
> 
> [FreeRTOS+TCP Downloads](http://www.freertos.org/FreeRTOS-Labs/RTOS_labs_download.html)
> 
> Just thinking out loud: if the FIN has been sent (|bFinSent|), then the  
> connection is in a closing state.  
> Apparently, the peer has not yet acknowledged the FIN, because  
> |ucTCPState != eCLOSED| yet, the connection isn’t closed.
> 
> It would be interesting to see a PCAP dump of that conversation in which  
> send() hangs. Can you create that?
> 
> On itself, your code seems correct: eventually |FreeRTOS\_send()| will  
> either send all data, or the connection gets closed and  
> |FreeRTOS\_send()| will return a negative errno code.
> 
> If |FreeRTOS\_send()| returns 0, it means that the buffers are full. Does  
> the call still block when nothing can be sent? Isn’t it just spinning  
> around on a too high priority?
> 
> Do you have these enabled?
> 
> ```
> #define ipconfigTCP_HANG_PROTECTION 1
> #define ipconfigTCP_KEEP_ALIVE 1
> 
> ```
> 
> Not sure if it would help you with this problem though 🙂
> 
> What does your code do after sending the file? After the last successful  
> call to |FreeRTOS\_send()|? Do you wait for the data to be properly  
> delivered?  
> Do you use the same socket for more transactions?
> 
> Regards.
> 
> * * *
> 
> FreeRTOS\_send gets stuck replying 0  
> [https://sourceforge.net/p/freertos/discussion/382005/thread/07900148/?limit=25#ab7d](https://sourceforge.net/p/freertos/discussion/382005/thread/07900148/?limit=25#ab7d)
> 
> * * *
> 
> Sent from [sourceforge.net](http://sourceforge.net) because you indicated interest in  
> [SourceForge.net: Log In to SourceForge.net](https://sourceforge.net/p/freertos/discussion/382005/)
> 
> To unsubscribe from further messages, please visit  
> [SourceForge.net: Log In to SourceForge.net](https://sourceforge.net/auth/subscriptions/)

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [September 1, 2016, 11:34pm UTC](https://forums.freertos.org/t/freertos-send-gets-stuck-replying-0/6150/4 "2016-09-01T23:34:27Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Thursday, September 01, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/07900148/#956f):

Hi Rob,

The PCAP shows a GET request that isn’t responded to. The browser waits for 30 seconds and disconnects.

Things are still not clear to me ☹

> I’ll try the latest version of the stack.

Good idea. Make sure you get the latest, which is “160831\_FreeRTOS\_Labs.zip”.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 2, 2016, 9:06am UTC](https://forums.freertos.org/t/freertos-send-gets-stuck-replying-0/6150/5 "2016-09-02T09:06:26Z")

</div>

**[dibosco](https://sourceforge.net/u/dibosco/)** wrote on [Friday, September 02, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/07900148/#956f/ef33):

Hi Hein,

I did update the stack and kernel from the link you sent and ran the  
cycle several times with no issues, so hopefully the new stack has done  
the trick!

Thanks!

Rob

On 02/09/16 00:34, Hein Tibosch wrote:

> Hi Rob,
> 
> The PCAP shows a GET request that isn’t responded to. The browser  
> waits for 30 seconds and disconnects.
> 
> Things are still not clear to me ☹
> 
> ```
> I'll try the latest version of the stack.
> 
> ```
> 
> Good idea. Make sure you get the latest, which is  
> “160831\_FreeRTOS\_Labs.zip”.
> 
> Regards.
> 
> * * *
> 
> FreeRTOS\_send gets stuck replying 0  
> [https://sourceforge.net/p/freertos/discussion/382005/thread/07900148/?limit=25#956f](https://sourceforge.net/p/freertos/discussion/382005/thread/07900148/?limit=25#956f)
> 
> * * *
> 
> Sent from [sourceforge.net](http://sourceforge.net) because you indicated interest in  
> [SourceForge.net: Log In to SourceForge.net](https://sourceforge.net/p/freertos/discussion/382005/)
> 
> To unsubscribe from further messages, please visit  
> [SourceForge.net: Log In to SourceForge.net](https://sourceforge.net/auth/subscriptions/)
