ricardgb opened a new issue, #22449:
URL: https://github.com/apache/nuttx/issues/22449
**Description**
A TCP server that answers a request, calls `shutdown(fd, SHUT_WR)` and then
`close(fd)` loses
its connections one by one until no TCP connection can be opened any more.
`tcp_shutdown()` (`net/tcp/tcp_shutdown.c`) does:
```c
conn->shutdown |= how;
...
return tcp_send_fin(psock);
```
and `tcp_send_fin()` returns `-EBUSY` when `tcp_callback_alloc()` finds no
free devif callback.
The connection stays marked with `SHUT_WR` although no FIN will be sent. The
close callback
(`tcp_close_eventhandler()`) sends its FIN only `if ((conn->shutdown &
SHUT_WR) == 0)`, so the
`close()` that follows sends none either. When the peer closes, the
connection goes to
`TCP_CLOSE_WAIT` with `crefs == 0` and stays there: that state has no
timeout, and
`tcp_reclaimable()` does not list it.
The callbacks run out in normal use: every closed connection keeps its close
callback while it
waits in `TIME_WAIT`. `close()` handles that case with `tcp_reclaim()`;
`shutdown()` does not.
So it needs a configuration where the TCP connections do not run out before
the callbacks
(`CONFIG_NET_TCP_PREALLOC_CONNS` ≥ `CONFIG_NET_PREALLOC_DEVIF_CALLBACKS`;
seen with both set to
no dynamic allocation).
**Reproduce (sim, unmodified master)**
Replace `apps/examples/hello/hello_main.c` with `nuttx-tcp-shutdown-repro.c`
below
(a server thread and a client over the IPv6 loopback), then:
```sh
./tools/configure.sh sim:tcploop
kconfig-tweak --enable EXAMPLES_HELLO \
--set-val NET_TCP_PREALLOC_CONNS 16 \
--set-val NET_TCP_WAIT_TIMEOUT 5 # only to keep the run
short
make olddefconfig && make
./nuttx
nsh> hello
```
Result on master `a8bb89bf680`:
```
Round 1: 17 of 40 connections answered, 25 shutdown() failures
Waiting 10 s for TIME_WAIT to pass...
Round 2: 0 of 10 connections answered
FAIL: connections were leaked
```
Round 2 runs after every socket of round 1 is closed and `TIME_WAIT` has
passed, so all
connections should be free again; none is.
**Seen first on hardware**
ESP32-S3 board with a CDC-NCM link, a small HTTP server (answer,
`shutdown(SHUT_WR)`, `close()`),
16 connections and 16 callbacks, `curl` in a loop from a Linux host: the
28th request and all
later ones time out, also on other ports (telnet). `/proc/net/tcp` then
shows 14 entries like
```
2: 07 40 0 0 0 0 0 192.168.7.1:80 192.168.7.2:57968
```
(`st` 07 = `TCP_CLOSE_WAIT`, `ref` 0), unchanged 20 s later, and the board
still refused
connections minutes later. With `close()` only, 80 of 80
requests pass and the closed connections are reclaimed from `TIME_WAIT` as
intended.
**Proposed fix**
PR to follow: in `tcp_send_fin()` call `tcp_reclaim()` when no callback is
free, as `close()`
does, and in `tcp_shutdown()` restore `conn->shutdown` when the FIN cannot
be sent.
<details>
<summary>nuttx-tcp-shutdown-repro.c (replaces
apps/examples/hello/hello_main.c)</summary>
```c
/* Reproducer: shutdown(SHUT_WR) that fails with EBUSY leaks the connection.
*
* Drop-in replacement for apps/examples/hello/hello_main.c, run on
* sim:tcploop (IPv6 loopback) with:
* CONFIG_EXAMPLES_HELLO=y
* CONFIG_NET_TCP_PREALLOC_CONNS=16 (as many as devif callbacks)
* CONFIG_NET_TCP_WAIT_TIMEOUT=5 (only to keep the run short)
*
* A server thread answers each connection, then shutdown(SHUT_WR) + close().
* The client reads the reply (known length) and closes. Once the closed
* server connections in TIME_WAIT hold all the devif callbacks, shutdown()
* returns EBUSY; such a connection never sends its FIN and stays in
* CLOSE_WAIT for ever. The test waits until TIME_WAIT has passed, when
* every connection of the first round should be free again, and tries a
* second round.
*/
#include <nuttx/config.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8080
#define NCONNS 40 /* First round: more than there are connections */
#define NAFTER 10 /* Second round, after TIME_WAIT has passed */
static const char g_reply[] = "hello from the server\n";
static int g_shut_failed;
static void *server(void *arg)
{
int ls = (int)(intptr_t)arg;
for (; ; )
{
char c;
int fd = accept(ls, NULL, NULL);
if (fd < 0)
{
continue;
}
if (recv(fd, &c, 1, 0) == 1)
{
send(fd, g_reply, sizeof(g_reply) - 1, 0);
if (shutdown(fd, SHUT_WR) < 0)
{
g_shut_failed++;
}
}
close(fd);
}
return NULL;
}
/* Open one connection, ask, and read the whole reply. Return 1 if the
* reply came.
*/
static int request(const struct sockaddr_in6 *sa)
{
char buf[sizeof(g_reply)];
size_t got = 0;
struct timeval tv =
{
1, 0
};
int fd = socket(AF_INET6, SOCK_STREAM, 0);
if (fd < 0)
{
return 0;
}
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
if (connect(fd, (const struct sockaddr *)sa, sizeof(*sa)) == 0)
{
send(fd, "x", 1, 0);
while (got < sizeof(g_reply) - 1)
{
ssize_t n = recv(fd, buf + got, sizeof(g_reply) - 1 - got, 0);
if (n <= 0)
{
break;
}
got += n;
}
}
close(fd);
return got == sizeof(g_reply) - 1;
}
int main(int argc, char *argv[])
{
struct sockaddr_in6 sa;
pthread_t tid;
int ok = 0;
int ls;
int i;
memset(&sa, 0, sizeof(sa));
sa.sin6_family = AF_INET6;
sa.sin6_port = htons(PORT);
sa.sin6_addr = in6addr_loopback;
ls = socket(AF_INET6, SOCK_STREAM, 0);
if (ls < 0 || bind(ls, (struct sockaddr *)&sa, sizeof(sa)) < 0 ||
listen(ls, 4) < 0)
{
printf("listen failed: %d\n", errno);
return 1;
}
pthread_create(&tid, NULL, server, (void *)(intptr_t)ls);
for (i = 0; i < NCONNS; i++)
{
ok += request(&sa);
}
printf("Round 1: %d of %d connections answered, "
"%d shutdown() failures\n", ok, NCONNS, g_shut_failed);
/* Every socket of round 1 is closed. After TIME_WAIT all of its
* connections should be free again.
*/
printf("Waiting %d s for TIME_WAIT to pass...\n",
CONFIG_NET_TCP_WAIT_TIMEOUT + 5);
sleep(CONFIG_NET_TCP_WAIT_TIMEOUT + 5);
for (ok = 0, i = 0; i < NAFTER; i++)
{
ok += request(&sa);
sleep(1);
}
printf("Round 2: %d of %d connections answered\n", ok, NAFTER);
printf("%s\n", ok == NAFTER ? "PASS" : "FAIL: connections were leaked");
return 0;
}
```
</details>
*Disclosure: this report and the patch were prepared with the help of an AI
agent (Claude Code,
Anthropic); the failure and the fix were reproduced in the simulator and on
real hardware and
human-reviewed.*
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]