This is an automated email from the ASF dual-hosted git repository.

acassis 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 a920f93f60f net/tcp: Do not wait for the timer work in tcp_free().
a920f93f60f is described below

commit a920f93f60f1a21e136b7dded9898f6b4baa93f5
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 19 04:42:13 2026 +0100

    net/tcp: Do not wait for the timer work in tcp_free().
    
    Since bc862acb935, tcp_stop_timer() cancels the timer of the
    connection with work_cancel_sync(): tcp_free() waits for a
    tcp_timer_expiry() that runs already.  That work takes the connection
    list lock and then the device lock.  Many callers of tcp_free() hold
    one of them: tcp_input() and the reclaim of tcp_alloc_accept() hold
    the device lock; tcp_timer() holds the device lock and the list lock
    (devif_poll_tcp_connections()), and a packet to a local address is
    received inside that poll with both locks held; tcp_alloc() and
    tcp_unlisten() hold the list lock.  If the timer work of the
    connection has started, it waits for that lock and tcp_free() waits
    for the work: the network stops.
    
    tcp_timer_expiry() uses the connection only when it finds it in the
    active list, under the list lock (also since bc862acb935), and
    tcp_free() removes the connection from that list before it stops the
    timer.  A timer work that runs already does not use the connection
    after that, so cancel the timer without waiting.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/tcp/tcp.h       |  2 +-
 net/tcp/tcp_timer.c | 11 +++++++++--
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/net/tcp/tcp.h b/net/tcp/tcp.h
index 82704a2673b..18dcac7804c 100644
--- a/net/tcp/tcp.h
+++ b/net/tcp/tcp.h
@@ -1150,7 +1150,7 @@ void tcp_update_keeptimer(FAR struct tcp_conn_s *conn, 
int timeout);
  *   None
  *
  * Assumptions:
- *   conn is not NULL.
+ *   conn is not NULL and is not in the active connection list.
  *
  ****************************************************************************/
 
diff --git a/net/tcp/tcp_timer.c b/net/tcp/tcp_timer.c
index 68e2f5fee44..3084006a6ed 100644
--- a/net/tcp/tcp_timer.c
+++ b/net/tcp/tcp_timer.c
@@ -343,13 +343,20 @@ void tcp_update_keeptimer(FAR struct tcp_conn_s *conn, 
int timeout)
  *   None
  *
  * Assumptions:
- *   conn is not NULL.
+ *   conn is not NULL and is not in the active connection list.
  *
  ****************************************************************************/
 
 void tcp_stop_timer(FAR struct tcp_conn_s *conn)
 {
-  work_cancel_sync(LPWORK, &conn->work);
+  /* Do not wait for a tcp_timer_expiry() that runs already.  It takes the
+   * connection list lock and then the device lock, and the callers of
+   * tcp_free() can hold either of them.  It uses the connection only when
+   * it finds it in the active list, under the list lock, and tcp_free()
+   * removes the connection from that list before it stops the timer.
+   */
+
+  work_cancel(LPWORK, &conn->work);
 }
 
 /****************************************************************************

Reply via email to