This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 8bde46b4fece255164909566c13b2fb6ec83c4b3 Author: Marco Casaroli <[email protected]> AuthorDate: Tue Nov 17 16:41:19 2026 +0100 net/tcp: Reset a closed connection with unread data at once. RFC 1122, section 4.2.2.13: a close() with received data that the application did not read, or new data after the close, should send a RST. tcp_close_eventhandler() did this check only after all of its own data was sent and acknowledged. When both ends of a connection had unread data and data of their own queued (two programs that each wait for the other to read), both waited for ever and kept their read and write buffers. Over the loopback device the IOB pool ran out, and every later TCP send() on the system blocked. Now the close handler checks for unread or new data first, before it waits for its queued data. It disables the send callback, frees the read buffers and resets the connection; the queued data is dropped with the connection, as for any reset. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- net/tcp/tcp_close.c | 48 ++++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/net/tcp/tcp_close.c b/net/tcp/tcp_close.c index 9b2036c49c6..cef0a25c387 100644 --- a/net/tcp/tcp_close.c +++ b/net/tcp/tcp_close.c @@ -116,6 +116,32 @@ static uint32_t tcp_close_eventhandler(FAR struct net_driver_s *dev, goto end_wait; } + /* Data that the application did not read, or that comes after the + * close, ends the connection with a RST at once (RFC 1122, section + * 4.2.2.13), also while our own data waits to be sent: the peer may + * wait for us to read before it reads, and the two connections would + * keep their buffers for ever. + */ + + else if (conn->readahead != NULL || (flags & TCP_NEWDATA) != 0) + { +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + if (conn->sndcb != NULL) + { + conn->sndcb->flags = 0; + conn->sndcb->event = NULL; + conn->sndcb = NULL; + } +#endif + + /* Set TCP_ABORT to send the RST, and free the rx buffers now */ + + dev->d_len = 0; + flags = (flags & ~TCP_NEWDATA) | TCP_ABORT; + tcp_free_rx_buffers(conn); + goto end_wait; + } + /* Check if all outstanding bytes have been ACKed. * * Note: in case of passive close, this ensures our FIN is acked. @@ -152,9 +178,7 @@ static uint32_t tcp_close_eventhandler(FAR struct net_driver_s *dev, conn->tcpstateflags == TCP_FIN_WAIT_1 || conn->tcpstateflags == TCP_FIN_WAIT_2); - /* Drop data received in this state and make sure that TCP_TXCLOSE - * is set in the response - */ + /* Make sure that TCP_TXCLOSE is set in the response */ #ifdef CONFIG_NET_TCP_WRITE_BUFFERS /* We don't need the send callback anymore. */ @@ -171,23 +195,7 @@ static uint32_t tcp_close_eventhandler(FAR struct net_driver_s *dev, #endif dev->d_len = 0; - if (conn->readahead != NULL || (flags & TCP_NEWDATA) != 0) - { - /* We need to send RST when read-ahead buffer data is not consumed - * or new data is coming. - * Set TCP_ABORT flag to trigger sending RST. - */ - - flags = flags & ~TCP_NEWDATA; - flags |= TCP_ABORT; - - /* Free rx buffers of the connection immediately */ - - tcp_free_rx_buffers(conn); - - goto end_wait; - } - else if ((conn->shutdown & SHUT_WR) == 0) + if ((conn->shutdown & SHUT_WR) == 0) { flags |= TCP_TXCLOSE;
