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 169eaa0e6d6ca6f9e325c4ab656e0b6cd235d426 Author: Marco Casaroli <[email protected]> AuthorDate: Wed Nov 18 02:41:19 2026 +0100 net/tcp: Free the send callback and the monitor of a closed connection. After close(), a connection that reaches TIME_WAIT kept three devif callbacks for the whole TIME_WAIT timeout (CONFIG_NET_TCP_WAIT_TIMEOUT, 120 s by default): the close callback, the disabled send callback and the monitor. With the default pool of 16 callbacks (CONFIG_NET_PREALLOC_DEVIF_CALLBACKS, CONFIG_NET_ALLOC_DEVIF_CALLBACKS 0), four connections in TIME_WAIT and two open sockets use all of them. The next close() then gets no close callback: it frees the connection at once, with no FIN, and drops the data that is still in the write queue. Free the send callback when the FIN is queued, in the close callback and in the shutdown callback: the write queue is empty then, and the timer retransmits the FIN. Stop the monitor at the start of close(): no socket refers to the connection any more, and the close callback can then use its callback structure. The close callback stays: it frees the connection when TIME_WAIT ends. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- net/tcp/tcp_close.c | 20 +++++++++++--------- net/tcp/tcp_shutdown.c | 10 ++++------ 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/net/tcp/tcp_close.c b/net/tcp/tcp_close.c index c435ee84fda..80ed0e86c60 100644 --- a/net/tcp/tcp_close.c +++ b/net/tcp/tcp_close.c @@ -214,15 +214,13 @@ static uint32_t tcp_close_eventhandler(FAR struct net_driver_s *dev, /* Make sure that TCP_TXCLOSE is set in the response */ #ifdef CONFIG_NET_TCP_WRITE_BUFFERS - /* We don't need the send callback anymore. */ + /* We don't need the send callback anymore. Free it now: the + * connection can stay in TIME_WAIT for a long time. + */ if (conn->sndcb != NULL) { - conn->sndcb->flags = 0; - conn->sndcb->event = NULL; - - /* The callback will be freed by tcp_free. */ - + tcp_callback_free(conn, conn->sndcb); conn->sndcb = NULL; } #endif @@ -288,6 +286,13 @@ static inline int tcp_close_disconnect(FAR struct socket *psock) conn->crefs = 0; + /* No socket refers to the connection any more: stop the monitor now, not + * when the connection is freed after TIME_WAIT. The close callback can + * then use its callback structure. + */ + + tcp_stop_monitor(conn, TCP_TXCLOSE); + /* TCP_ESTABLISHED * We need to initiate an active close and wait for its completion. * @@ -363,9 +368,6 @@ static inline int tcp_close_disconnect(FAR struct socket *psock) } else { - /* Stop the network monitor for all sockets */ - - tcp_stop_monitor(conn, TCP_TXCLOSE); conn_dev_unlock(&conn->sconn, conn->dev); /* Free network resources */ diff --git a/net/tcp/tcp_shutdown.c b/net/tcp/tcp_shutdown.c index bd7136fbd73..966d1e52323 100644 --- a/net/tcp/tcp_shutdown.c +++ b/net/tcp/tcp_shutdown.c @@ -82,15 +82,13 @@ static uint32_t tcp_shutdown_eventhandler(FAR struct net_driver_s *dev, } #ifdef CONFIG_NET_TCP_WRITE_BUFFERS - /* We don't need the send callback anymore. */ + /* We don't need the send callback anymore. Free it now: the + * connection can stay in TIME_WAIT for a long time. + */ if (conn->sndcb != NULL) { - conn->sndcb->flags = 0; - conn->sndcb->event = NULL; - - /* The callback will be freed by tcp_free. */ - + tcp_callback_free(conn, conn->sndcb); conn->sndcb = NULL; } #endif
