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

yiguolei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new f7ed2817ad [fix] [ubsan] Fix TCMalloc Hook deadlocks when 
ThreadContext is initialized (#10310)
f7ed2817ad is described below

commit f7ed2817ada06101808689694419fb8ce6e5b5b8
Author: Kidd <[email protected]>
AuthorDate: Wed Jun 22 14:37:48 2022 +0800

    [fix] [ubsan] Fix TCMalloc Hook deadlocks when ThreadContext is initialized 
(#10310)
---
 be/src/runtime/tcmalloc_hook.h    | 8 ++++++--
 be/src/runtime/thread_context.cpp | 1 +
 be/src/runtime/thread_context.h   | 9 ++++++++-
 3 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/be/src/runtime/tcmalloc_hook.h b/be/src/runtime/tcmalloc_hook.h
index 600668aea9..59208b334a 100644
--- a/be/src/runtime/tcmalloc_hook.h
+++ b/be/src/runtime/tcmalloc_hook.h
@@ -36,11 +36,15 @@
 //  destructor to control the behavior of consume can lead to unexpected 
behavior,
 //  like this: if (LIKELY(doris::start_thread_mem_tracker)) {
 void new_hook(const void* ptr, size_t size) {
-    doris::tls_ctx()->consume_mem(tc_nallocx(size, 0));
+    if (doris::tls_ctx()) {
+        doris::tls_ctx()->consume_mem(tc_nallocx(size, 0));
+    }
 }
 
 void delete_hook(const void* ptr) {
-    doris::tls_ctx()->release_mem(tc_malloc_size(const_cast<void*>(ptr)));
+    if (doris::tls_ctx()) {
+        doris::tls_ctx()->release_mem(tc_malloc_size(const_cast<void*>(ptr)));
+    }
 }
 
 void init_hook() {
diff --git a/be/src/runtime/thread_context.cpp 
b/be/src/runtime/thread_context.cpp
index 2139469393..a439b4ca8f 100644
--- a/be/src/runtime/thread_context.cpp
+++ b/be/src/runtime/thread_context.cpp
@@ -26,6 +26,7 @@ DEFINE_STATIC_THREAD_LOCAL(ThreadContext, ThreadContextPtr, 
thread_local_ctx);
 
 ThreadContextPtr::ThreadContextPtr() {
     INIT_STATIC_THREAD_LOCAL(ThreadContext, thread_local_ctx);
+    _init = true;
 }
 
 ThreadContext* ThreadContextPtr::get() {
diff --git a/be/src/runtime/thread_context.h b/be/src/runtime/thread_context.h
index 9b8c6aad02..7b58315b09 100644
--- a/be/src/runtime/thread_context.h
+++ b/be/src/runtime/thread_context.h
@@ -210,6 +210,8 @@ public:
 
     ThreadContext* get();
 
+    bool _init = false;
+
 private:
     DECLARE_STATIC_THREAD_LOCAL(ThreadContext, thread_local_ctx);
 };
@@ -221,7 +223,12 @@ static ThreadContext* tls_ctx() {
     if (tls != nullptr) {
         return tls;
     } else {
-        return thread_local_ctx.get();
+        if (thread_local_ctx._init) {
+            return thread_local_ctx.get();
+        } else {
+            // TCMalloc hook is triggered during ThreadContext construction, 
which may lead to deadlock.
+            return nullptr;
+        }
     }
 }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to