From f1ef2c6cdeb032eaa1833cc534a63b50c5058270 Mon Sep 17 00:00:00 2001 From: Andrew Webster Date: Fri, 22 Jan 2016 15:58:02 -0600 Subject: [PATCH] TCP: attempt to flush the write buffers before closing When a socket is closed, it should make sure that any pending write data is sent before the FIN is sent. It already would wait for all sent data to be acked, however it would discard any pending write data that had not been sent at least once. This change adds a check for pending write data in addition to unacked data. However, to be able to actually send any new data, the send callback must be left. The callback should be freed later when the socket is actually destroyed. --- net/socket/net_close.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/net/socket/net_close.c b/net/socket/net_close.c index 0635bab504a..f2a67f78c33 100644 --- a/net/socket/net_close.c +++ b/net/socket/net_close.c @@ -223,7 +223,7 @@ static uint16_t netclose_interrupt(FAR struct net_driver_s *dev, #ifdef CONFIG_NET_TCP_WRITE_BUFFERS /* Check if all outstanding bytes have been ACKed */ - else if (conn->unacked != 0) + else if (conn->unacked != 0 || !sq_empty(&conn->write_q)) { /* No... we are still waiting for ACKs. Drop any received data, but * do not yet report TCP_CLOSE in the response. @@ -357,14 +357,11 @@ static inline int netclose_disconnect(FAR struct socket *psock) #ifdef CONFIG_NET_TCP_WRITE_BUFFERS if (psock->s_sndcb) { - tcp_callback_free(conn, psock->s_sndcb); psock->s_sndcb = NULL; } #endif - /* There shouldn't be any callbacks registered. */ - - DEBUGASSERT(conn && conn->list == NULL); + DEBUGASSERT(conn != NULL); /* Check for the case where the host beat us and disconnected first */