This is an automated email from the ASF dual-hosted git repository.
GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new e3f8fa51f3a net/tcp: Do not leak a connection when shutdown() gets no
callback.
e3f8fa51f3a is described below
commit e3f8fa51f3afa058eb77afb418417b6285febb2e
Author: Ricard Rosson <[email protected]>
AuthorDate: Thu Nov 26 22:12:44 2026 +0000
net/tcp: Do not leak a connection when shutdown() gets no callback.
tcp_shutdown() set SHUT_WR in conn->shutdown and then called
tcp_send_fin(), which returns -EBUSY when no devif callback is free.
The connection was left marked as shut down although no FIN would be
sent. The close callback sends no FIN for a connection that is marked
with SHUT_WR, so a later close() sent none either. Once the peer
closed, the connection stayed in CLOSE_WAIT with no socket: that state
has no timeout and tcp_reclaim() does not take it, so the connection
and its callback were lost until the next reset.
The callbacks run out easily: each closed connection keeps its close
callback while it waits in TIME_WAIT. A server that answers, calls
shutdown(SHUT_WR) and then close() loses every connection after the
first ones, until none is left.
Free a connection that only waits for the end of its close when no
callback is free, as close() does, and take SHUT_WR back when the FIN
cannot be sent, so that close() sends it.
Assisted-by: Claude Code:claude-fable-5-2
Signed-off-by: Ricard Rosson <[email protected]>
---
net/tcp/tcp_shutdown.c | 41 ++++++++++++++++++++++++++++++++++++++---
1 file changed, 38 insertions(+), 3 deletions(-)
diff --git a/net/tcp/tcp_shutdown.c b/net/tcp/tcp_shutdown.c
index 966d1e52323..364f1eb1484 100644
--- a/net/tcp/tcp_shutdown.c
+++ b/net/tcp/tcp_shutdown.c
@@ -132,6 +132,7 @@ static uint32_t tcp_shutdown_eventhandler(FAR struct
net_driver_s *dev,
static inline int tcp_send_fin(FAR struct socket *psock)
{
FAR struct tcp_conn_s *conn;
+ bool reclaimed;
int ret = OK;
/* Interrupts are disabled here to avoid race conditions */
@@ -152,8 +153,28 @@ static inline int tcp_send_fin(FAR struct socket *psock)
if (conn->shdcb == NULL &&
(conn->shdcb = tcp_callback_alloc(conn)) == NULL)
{
- ret = -EBUSY;
- goto out;
+ /* All callbacks are in use: free a connection that only waits
+ * for the end of its close (for example in TIME_WAIT), as close()
+ * does. tcp_reclaim() takes a device lock and the list lock: not
+ * with our locks held. It does not take this connection: a
+ * socket refers to it.
+ */
+
+ conn_dev_unlock(&conn->sconn, conn->dev);
+ reclaimed = tcp_reclaim(NULL);
+ conn_dev_lock(&conn->sconn, conn->dev);
+
+ if (!reclaimed ||
+ (conn->tcpstateflags != TCP_ESTABLISHED &&
+ conn->tcpstateflags != TCP_SYN_SENT &&
+ conn->tcpstateflags != TCP_SYN_RCVD &&
+ conn->tcpstateflags != TCP_CLOSE_WAIT) ||
+ (conn->shdcb == NULL &&
+ (conn->shdcb = tcp_callback_alloc(conn)) == NULL))
+ {
+ ret = -EBUSY;
+ goto out;
+ }
}
/* Set up to receive TCP data event callbacks */
@@ -194,6 +215,8 @@ out:
int tcp_shutdown(FAR struct socket *psock, int how)
{
FAR struct tcp_conn_s *conn;
+ uint8_t oldshutdown;
+ int ret;
conn = psock->s_conn;
DEBUGASSERT(conn != NULL);
@@ -203,11 +226,23 @@ int tcp_shutdown(FAR struct socket *psock, int how)
return -EOPNOTSUPP;
}
+ oldshutdown = conn->shutdown;
conn->shutdown |= how;
tcp_unlisten(psock->s_conn); /* No longer accepting connections */
- return tcp_send_fin(psock);
+ ret = tcp_send_fin(psock);
+ if (ret < 0)
+ {
+ /* No FIN will be sent: do not leave the connection marked as shut
+ * down, or close() sends no FIN either ("avoid multiple FIN") and
+ * the connection stays for ever once the peer closes (CLOSE_WAIT).
+ */
+
+ conn->shutdown = oldshutdown;
+ }
+
+ return ret;
}
#endif /* CONFIG_NET_TCP */