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

commit b0d8fd9d75dd7d826eb2dd9ad4eafee5509560ed
Author: wangchen <wangche...@xiaomi.com>
AuthorDate: Wed May 15 11:34:59 2024 +0800

    udp:add tls cleanup protection to protect waitsem in udp_txdrain
    
    Signed-off-by: wangchen <wangche...@xiaomi.com>
---
 net/udp/udp.h          |  2 +-
 net/udp/udp_notifier.c |  4 ++--
 net/udp/udp_txdrain.c  | 12 +++++++++++-
 3 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/net/udp/udp.h b/net/udp/udp.h
index f131556816..86e2a3ce32 100644
--- a/net/udp/udp.h
+++ b/net/udp/udp.h
@@ -897,7 +897,7 @@ int udp_writebuffer_notifier_setup(worker_t worker,
  ****************************************************************************/
 
 #ifdef CONFIG_NET_UDP_NOTIFIER
-void udp_notifier_teardown(int key);
+void udp_notifier_teardown(FAR void *key);
 #endif
 
 /****************************************************************************
diff --git a/net/udp/udp_notifier.c b/net/udp/udp_notifier.c
index 82c78dad17..bb9b07d255 100644
--- a/net/udp/udp_notifier.c
+++ b/net/udp/udp_notifier.c
@@ -170,11 +170,11 @@ int udp_writebuffer_notifier_setup(worker_t worker,
  *
  ****************************************************************************/
 
-void udp_notifier_teardown(int key)
+void udp_notifier_teardown(FAR void *key)
 {
   /* This is just a simple wrapper around work_notifier_teardown(). */
 
-  work_notifier_teardown(key);
+  work_notifier_teardown(*(FAR int *)key);
 }
 
 /****************************************************************************
diff --git a/net/udp/udp_txdrain.c b/net/udp/udp_txdrain.c
index 791cc0a518..62a04d6157 100644
--- a/net/udp/udp_txdrain.c
+++ b/net/udp/udp_txdrain.c
@@ -31,8 +31,10 @@
 #include <assert.h>
 #include <errno.h>
 
+#include <nuttx/cancelpt.h>
 #include <nuttx/net/net.h>
 #include <nuttx/semaphore.h>
+#include <nuttx/tls.h>
 
 #include "utils/utils.h"
 #include "udp/udp.h"
@@ -96,6 +98,10 @@ int udp_txdrain(FAR struct socket *psock, unsigned int 
timeout)
 
   DEBUGASSERT(psock->s_type == SOCK_DGRAM);
 
+  /* udp_txdrain() is a cancellation point */
+
+  enter_cancellation_point();
+
   conn = psock->s_conn;
 
   /* Initialize the wait semaphore */
@@ -112,18 +118,22 @@ int udp_txdrain(FAR struct socket *psock, unsigned int 
timeout)
 
       /* There is pending write data.. wait for it to drain. */
 
+      tls_cleanup_push(tls_get_info(), udp_notifier_teardown, &key);
       ret = net_sem_timedwait_uninterruptible(&waitsem, timeout);
 
       /* Tear down the notifier (in case we timed out or were canceled) */
 
       if (ret < 0)
         {
-          udp_notifier_teardown(key);
+          udp_notifier_teardown(&key);
         }
+
+      tls_cleanup_pop(tls_get_info(), 0);
     }
 
   net_unlock();
   nxsem_destroy(&waitsem);
+  leave_cancellation_point();
   return ret;
 }
 

Reply via email to