Skip to content

Commit 2721432

Browse files
zhhyu7anchao
authored andcommitted
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 <zhanghongyu@xiaomi.com>
1 parent 11d15bb commit 2721432

2 files changed

Lines changed: 4 additions & 2 deletions

File tree

net/can/can_callback.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ uint32_t can_callback(FAR struct net_driver_s *dev,
152152

153153
conn_lock(&conn->sconn);
154154
flags = devif_conn_event(dev, flags, conn->sconn.list);
155-
conn_unlock(&conn->sconn);
156155

157156
/* Either we did not get the lock or there is no application listening
158157
* 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,
164163

165164
flags = can_data_event(dev, conn, flags);
166165
}
166+
167+
conn_unlock(&conn->sconn);
167168
}
168169

169170
return flags;

net/udp/udp_callback.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,15 @@ uint32_t udp_callback(FAR struct net_driver_s *dev,
319319

320320
conn_lock(&conn->sconn);
321321
flags = devif_conn_event(dev, flags, conn->sconn.list);
322-
conn_unlock(&conn->sconn);
323322

324323
if ((flags & UDP_NEWDATA) != 0)
325324
{
326325
/* Data was not handled.. dispose of it appropriately */
327326

328327
flags = net_dataevent(dev, conn, flags);
329328
}
329+
330+
conn_unlock(&conn->sconn);
330331
}
331332

332333
return flags;

0 commit comments

Comments
 (0)