Issue with netconn_close closing connection before netconn_write's have completed

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Issue with netconn_close closing connection before netconn_write's have completed

Terry Barnaby
I am using Lwip on an STM32F7 system along with FreeRTOS and our own
system harness ode. Lwip is generally working very well supporting a
number of TCPIP services and client functions. However we have one small
issue.

If we call netconn_close() shortly after a netconn_write() the remote
host often does not get data from the netconn_write() unless we put in a
short delay before netconn_close() is called. An example is in a simple
HTTP server like:

     while(1){
         // Accept any incoming connection
         accept_err = netconn_accept(osocketListen, &osocket);
         if(accept_err == ERR_OK){
             dl4printf("Listen: accept connection\n");
             netconn_set_recvtimeout(osocket, 10000);

             // Serve connection
             err = processRequest();

             // We shouldn't need this but TCP write packets can be lost
if we don't for some reason
             delayMs(1);

             // Close connection
             netconn_close(osocket);

             // Delete connection
             netconn_delete(osocket);
             osocket = 0;
         }
     }

In this case, depending on how much is written by netconn_write(), a WEB
browser will display a window stating that the client has closed the
connection unless the 1 ms delay is included. The processRequest()
function uses netconn_recv() and netconn_write() with the flag
NETCONN_COPY. All of the netconn HTTP processing is within one thread.

I am unclear from the documentation and code if netconn_close() will
wait for the TCPIP write buffers to be sent before closing or not. I
have tried setting the lwip config option LWIP_SO_LINGER in case that
was needed but this didn't have any effect. Any info on if
netconn_close() should wait for the write buffers to be sent and if so
what config options may be needed to provide this behaviour ?


_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: Issue with netconn_close closing connection before netconn_write's have completed

Terry Barnaby
No one have any ideas on this ?

On 19/04/2019 11:17, Terry Barnaby wrote:

> I am using Lwip on an STM32F7 system along with FreeRTOS and our own
> system harness ode. Lwip is generally working very well supporting a
> number of TCPIP services and client functions. However we have one
> small issue.
>
> If we call netconn_close() shortly after a netconn_write() the remote
> host often does not get data from the netconn_write() unless we put in
> a short delay before netconn_close() is called. An example is in a
> simple HTTP server like:
>
>     while(1){
>         // Accept any incoming connection
>         accept_err = netconn_accept(osocketListen, &osocket);
>         if(accept_err == ERR_OK){
>             dl4printf("Listen: accept connection\n");
>             netconn_set_recvtimeout(osocket, 10000);
>
>             // Serve connection
>             err = processRequest();
>
>             // We shouldn't need this but TCP write packets can be
> lost if we don't for some reason
>             delayMs(1);
>
>             // Close connection
>             netconn_close(osocket);
>
>             // Delete connection
>             netconn_delete(osocket);
>             osocket = 0;
>         }
>     }
>
> In this case, depending on how much is written by netconn_write(), a
> WEB browser will display a window stating that the client has closed
> the connection unless the 1 ms delay is included. The processRequest()
> function uses netconn_recv() and netconn_write() with the flag
> NETCONN_COPY. All of the netconn HTTP processing is within one thread.
>
> I am unclear from the documentation and code if netconn_close() will
> wait for the TCPIP write buffers to be sent before closing or not. I
> have tried setting the lwip config option LWIP_SO_LINGER in case that
> was needed but this didn't have any effect. Any info on if
> netconn_close() should wait for the write buffers to be sent and if so
> what config options may be needed to provide this behaviour ?
>
>
> _______________________________________________
> lwip-users mailing list
> [hidden email]
> https://lists.nongnu.org/mailman/listinfo/lwip-users

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: Issue with netconn_close closing connection before netconn_write's have completed

ricardoschoof
In reply to this post by Terry Barnaby
Dear Terry,

This morning I started a similar thread:
http://lwip.100.n7.nabble.com/Socket-Connection-closed-before-sending-all-data-td34503.html,
except I use sockets instead of netconn.
Did you manage to find a solution for your problem?





--
Sent from: http://lwip.100.n7.nabble.com/lwip-users-f3.html

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: Issue with netconn_close closing connection before netconn_write's have completed

Terry Barnaby
On 21/05/2019 13:55, ricardoschoof wrote:

> Dear Terry,
>
> This morning I started a similar thread:
> http://lwip.100.n7.nabble.com/Socket-Connection-closed-before-sending-all-data-td34503.html,
> except I use sockets instead of netconn.
> Did you manage to find a solution for your problem?
>
>
>
>
>
> --
> Sent from: http://lwip.100.n7.nabble.com/lwip-users-f3.html
>
> _______________________________________________
> lwip-users mailing list
> [hidden email]
> https://lists.nongnu.org/mailman/listinfo/lwip-users

No I haven't found a proper solution to the problem. Adding a short
delay (1ms) between my last netconn_write() and the netconn_close()
works around the issue.

     while(1){
         // Accept any incoming connection
         accept_err = netconn_accept(osocketListen, &osocket);
         if(accept_err == ERR_OK){
             dl4printf("Listen: accept connection\n");
             netconn_set_recvtimeout(osocket, 10000);

             // Serve connection
             err = processRequest();

             // We shouldn't need this but TCP write packets can be lost
if we don't for some reason
             delayMs(1);

             // Close connection
             netconn_close(osocket);

             // Delete connection
             netconn_delete(osocket);
             osocket = 0;
         }
     }


_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: Issue with netconn_close closing connection before netconn_write's have completed

ricardoschoof
Hi,
I have fixed my code (although I'm using sockets). It seems the socket
implementation was closing the connection even when there was pending data.
Enabling SO_LINGER cause the application to close the socket after all data
was sent, or a timeout occured.

The netconn solution also has a linger option, this might be the solution
you need?

netconn linger:
https://www.nongnu.org/lwip/2_0_x/structnetconn.html#a25ed06d944da0b0b9e7db5265be3fa3d

For more info on linger see (search for SO_LINGER)
http://man7.org/linux/man-pages/man7/socket.7.html



--
Sent from: http://lwip.100.n7.nabble.com/lwip-users-f3.html

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: Issue with netconn_close closing connection before netconn_write's have completed

Terry Barnaby
Thanks for the info.

I had enabled "#define LWIP_SO_LINGER  1" in lwipopts.h which did not
affect the issue.

But looking at the link you sent I might also have to "struct netconn*
nc; nc->linger = 30;" or something like that to set the linger seconds
value of a particular netconn connection. I will have a play with that.

Terry

On 23/05/2019 14:45, ricardoschoof wrote:

> Hi,
> I have fixed my code (although I'm using sockets). It seems the socket
> implementation was closing the connection even when there was pending data.
> Enabling SO_LINGER cause the application to close the socket after all data
> was sent, or a timeout occured.
>
> The netconn solution also has a linger option, this might be the solution
> you need?
>
> netconn linger:
> https://www.nongnu.org/lwip/2_0_x/structnetconn.html#a25ed06d944da0b0b9e7db5265be3fa3d
>
> For more info on linger see (search for SO_LINGER)
> http://man7.org/linux/man-pages/man7/socket.7.html
>
>
>
> --
> Sent from: http://lwip.100.n7.nabble.com/lwip-users-f3.html
>
> _______________________________________________
> lwip-users mailing list
> [hidden email]
> https://lists.nongnu.org/mailman/listinfo/lwip-users

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users