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

archer 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 27214321bea net/can,udp: fix conn unlock position in callback
27214321bea is described below

commit 27214321beada2ac9231a04e48f876c0d5b5043e
Author: zhanghongyu <[email protected]>
AuthorDate: Thu Apr 2 16:48:32 2026 +0800

    net/can,udp: fix conn unlock position in callback
    
    Move conn_unlock() after the data event handling in can_callback()
    and udp_callback(). Previously, the connection lock was released
    immediately after devif_conn_event(), leaving the subsequent
    can_data_event()/net_dataevent() and read-ahead buffer operations
    unprotected. This could lead to a race condition where another
    context modifies the connection state while data is being stored.
    
    Hold the lock until the entire callback processing is complete to
    ensure thread safety.
    
    Signed-off-by: zhanghongyu <[email protected]>
---
 net/can/can_callback.c | 3 ++-
 net/udp/udp_callback.c | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/net/can/can_callback.c b/net/can/can_callback.c
index 0aac77cd6f4..c4780e7beca 100644
--- a/net/can/can_callback.c
+++ b/net/can/can_callback.c
@@ -152,7 +152,6 @@ uint32_t can_callback(FAR struct net_driver_s *dev,
 
       conn_lock(&conn->sconn);
       flags = devif_conn_event(dev, flags, conn->sconn.list);
-      conn_unlock(&conn->sconn);
 
       /* Either we did not get the lock or there is no application listening
        * If we did not get a lock we store the frame in the read-ahead buffer
@@ -164,6 +163,8 @@ uint32_t can_callback(FAR struct net_driver_s *dev,
 
           flags = can_data_event(dev, conn, flags);
         }
+
+      conn_unlock(&conn->sconn);
     }
 
   return flags;
diff --git a/net/udp/udp_callback.c b/net/udp/udp_callback.c
index 7b363e3acd4..92257fea6af 100644
--- a/net/udp/udp_callback.c
+++ b/net/udp/udp_callback.c
@@ -319,7 +319,6 @@ uint32_t udp_callback(FAR struct net_driver_s *dev,
 
       conn_lock(&conn->sconn);
       flags = devif_conn_event(dev, flags, conn->sconn.list);
-      conn_unlock(&conn->sconn);
 
       if ((flags & UDP_NEWDATA) != 0)
         {
@@ -327,6 +326,8 @@ uint32_t udp_callback(FAR struct net_driver_s *dev,
 
           flags = net_dataevent(dev, conn, flags);
         }
+
+      conn_unlock(&conn->sconn);
     }
 
   return flags;

Reply via email to