ricardgb opened a new pull request, #22450:
URL: https://github.com/apache/nuttx/pull/22450
## Summary
Fixes #22449.
`tcp_shutdown()` marked the connection with `SHUT_WR` and then called
`tcp_send_fin()`, which
returns `-EBUSY` when no devif callback is free. The mark stayed, so the
`close()` that followed
sent no FIN either ("avoid sending multiple FIN"), and once the peer closed
the connection
stayed in `CLOSE_WAIT` with no socket, for ever: no timeout, and not taken
by `tcp_reclaim()`.
Two changes in `net/tcp/tcp_shutdown.c`:
* `tcp_send_fin()`: when no callback is free, free a connection that only
waits for the end of
its close (`tcp_reclaim(NULL)`, with our locks released, as
`tcp_close_disconnect()` does),
check the state again and allocate again.
* `tcp_shutdown()`: if the FIN still cannot be sent, restore
`conn->shutdown`, so that `close()`
sends the FIN.
## Impact
Any server that calls `shutdown(SHUT_WR)` before `close()` in a
configuration where the devif
callbacks can run out before the TCP connections. No change when a callback
is free.
`shutdown()` can still return `-EBUSY` when nothing can be reclaimed, but
the connection is no
longer lost.
## Testing
Host: Ubuntu, GCC. Master `a8bb89bf680`.
**sim:tcploop** + `EXAMPLES_HELLO`, `NET_TCP_PREALLOC_CONNS=16`,
`NET_TCP_WAIT_TIMEOUT=5`, with
the reproducer of the issue (server and client over the IPv6 loopback, 40
connections, wait for
`TIME_WAIT`, 10 more):
| | Round 1 answered | shutdown() failures | Round 2 answered |
|---|---|---|---|
| master | 17 of 40 | 25 | 0 of 10 (FAIL) |
| only the `conn->shutdown` restore | 12 of 40 | 29 | 10 of 10 |
| this PR | 40 of 40 | 0 | 10 of 10 (PASS) |
**ESP32-S3** (custom board, CDC-NCM link, 16 connections / 16 callbacks),
HTTP server that
answers, `shutdown(SHUT_WR)`, `close()`; `curl` in a loop from Linux:
| | Result |
|---|---|
| master | fails at request 28 (3 runs), 14 connections in `CLOSE_WAIT` with
`ref` 0 |
| this PR | 200 of 200, connections end in `TIME_WAIT` and are reclaimed |
`./tools/checkpatch.sh -g HEAD~1..HEAD`: all checks pass.
*Disclosure: this change was prepared with the help of an AI agent (Claude
Code, Anthropic); it
was tested in the simulator and on real hardware and human-reviewed.*
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
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]