Skip to content

Commit f9166b7

Browse files
xinyiZzzxiaokang
authored andcommitted
[fix](memory) fix mem tracker grace exit 2 (#22003)
fix: #21136 mem tracker group uses class static variables instead of global variables https://stackoverflow.com/questions/2204608/does-c-call-destructors-for-global-and-class-static-variables TODO: A mem tracker manager is required, don't use global variables, it will sad ==3623982==ERROR: AddressSanitizer: heap-use-after-free on address 0x60f0000056b8 at pc 0x56478bbe3ae0 bp 0x7f20953d2270 sp 0x7f20953d2268 READ of size 8 at 0x60f0000056b8 thread T41 (memory_tracker_) *** Query id: 0-0 *** *** Aborted at 1689749969 (unix time) try "date -d @1689749969" if you are using GNU date *** *** Current BE git commitID: b3e9cad *** *** SIGSEGV address not mapped to object (@0x0) received by PID 3623982 (TID 3624277 OR 0x7f19e06dd640) from PID 0; stack trace: *** #0 0x56478bbe3adf in std::__shared_ptr::operator bool() const /var/local/ldb_toolchain/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/shared_ptr_base.h:1295:16 #1 0x56478bbe306e in doris::MemTracker::refresh_profile_counter() /doris/be/src/runtime/memory/mem_tracker.h:149:13 #2 0x56478bbec669 in doris::MemTrackerLimiter::refresh_all_tracker_profile() /doris/be/src/runtime/memory/mem_tracker_limiter.cpp:119:22 #3 0x564788f53fa0 in doris::Daemon::memory_tracker_profile_refresh_thread() /doris/be/src/common/daemon.cpp:295:9 #4 0x564788f5d04b in doris::Daemon::start()::$_4::operator()() const /doris/be/src/common/daemon.cpp:473:30 #5 0x564788f5cff6 in void std::__invoke_impl(std::__invoke_other, doris::Daemon::start()::$_4&) /var/local/ldb_toolchain/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/invoke.h:61:14 #6 0x564788f5cf78 in std::enable_if, void>::type std::__invoke_r(doris::Daemon::start()::$_4&) /var/local/ldb_toolchain/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/invoke.h:111:2 #7 0x564788f5cdae in std::_Function_handler::_M_invoke(std::_Any_data const&) /var/local/ldb_toolchain/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/std_function.h:291:9 #8 0x56478903f576 in std::function::operator()() const /var/local/ldb_toolchain/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/std_function.h:560:9 #9 0x56478c4a35af in doris::Thread::supervise_thread(void*) /doris/be/src/util/thread.cpp:465:5 #10 0x7f217c8a244f in start_thread nptl/pthread_create.c:473:8 #11 0x7f217cb27d52 in __clone misc/../sysdeps/unix/sysv/linux/x86_64/clone.S:95 0x60f0000056b8 is located 56 bytes inside of 168-byte region [0x60f000005680,0x60f000005728) freed by thread T0 here: #0 0x564788e7280d in operator delete(void*) (/mnt/hdd01/dorisTestEnv/NEREIDS_ASAN/be/lib/doris_be+0x1758280d) (BuildId: 219493cc924323ee) #1 0x56478acec1d5 in std::default_delete::operator()(doris::MemTrackerLimiter*) const /var/local/ldb_toolchain/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/unique_ptr.h:85:2 #2 0x56478ace9faf in std::unique_ptr >::~unique_ptr() /var/local/ldb_toolchain/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/unique_ptr.h:361:4 #3 0x56478ace1471 in doris::ShardedLRUCache::~ShardedLRUCache() /doris/be/src/olap/lru_cache.cpp:581:1 #4 0x56478ace14c8 in doris::ShardedLRUCache::~ShardedLRUCache() /doris/be/src/olap/lru_cache.cpp:572:37 #5 0x56478acd0984 in std::default_delete::operator()(doris::Cache*) const /var/local/ldb_toolchain/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/unique_ptr.h:85:2 #6 0x56478acceddf in std::unique_ptr >::~unique_ptr() /var/local/ldb_toolchain/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/unique_ptr.h:361:4 #7 0x56478ad96dc6 in doris::StoragePageCache::~StoragePageCache() /doris/be/src/olap/page_cache.h:78:7 #8 0x7f217ca54146 in __run_exit_handlers stdlib/exit.c:108:8 previously allocated by thread T0 here: #0 0x564788e71fad in operator new(unsigned long) (/mnt/hdd01/dorisTestEnv/NEREIDS_ASAN/be/lib/doris_be+0x17581fad) (BuildId: 219493cc924323ee) #1 0x56478ace9c90 in std::_MakeUniq::__single_object std::make_unique, std::allocator > const&>(doris::MemTrackerLimiter::Type&&, std::__cxx11::basic_string, std::allocator > const&) /var/local/ldb_toolchain/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/unique_ptr.h:962:30 #2 0x56478acde930 in doris::ShardedLRUCache::ShardedLRUCache(std::__cxx11::basic_string, std::allocator > const&, unsigned long, doris::LRUCacheType, unsigned int, unsigned int) /doris/be/src/olap/lru_cache.cpp:526:20 #3 0x56478ace22e1 in doris::new_lru_cache(std::__cxx11::basic_string, std::allocator > const&, unsigned long, doris::LRUCacheType, unsigned int) /doris/be/src/olap/lru_cache.cpp:670:16 #4 0x56478ad91da2 in doris::StoragePageCache::StoragePageCache(unsigned long, int, long, unsigned int) /doris/be/src/olap/page_cache.cpp:47:17 #5 0x56478ad9156e in doris::StoragePageCache::create_global_cache(unsigned long, int, long, unsigned int) /doris/be/src/olap/page_cache.cpp:31:29 #6 0x56478b98b3d3 in doris::ExecEnv::_init_mem_env() /doris/be/src/runtime/exec_env_init.cpp:251:5 #7 0x56478b98946c in doris::ExecEnv::_init(std::vector > const&) /doris/be/src/runtime/exec_env_init.cpp:182:5 #8 0x56478b987139 in doris::ExecEnv::init(doris::ExecEnv*, std::vector > const&) /doris/be/src/runtime/exec_env_init.cpp:98:17 #9 0x564788e79b50 in main /doris/be/src/service/doris_main.cpp:429:5 #10 0x7f217ca38564 in __libc_start_main csu/../csu/libc-start.c:332:16 Thread T41 (memory_tracker_) created by T0 here: #0 0x564788e1fcaa in pthread_create (/mnt/hdd01/dorisTestEnv/NEREIDS_ASAN/be/lib/doris_be+0x1752fcaa) (BuildId: 219493cc924323ee) #1 0x56478c4a2366 in doris::Thread::start_thread(std::__cxx11::basic_string, std::allocator > const&, std::__cxx11::basic_string, std::allocator > const&, std::function const&, unsigned long, scoped_refptr*) /doris/be/src/util/thread.cpp:419:15 #2 0x564788f59b91 in doris::Status doris::Thread::create(std::__cxx11::basic_string, std::allocator > const&, std::__cxx11::basic_string, std::allocator > const&, doris::Daemon::start()::$_4 const&, scoped_refptr*) /doris/be/src/util/thread.h:50:16 #3 0x564788f58165 in doris::Daemon::start() /doris/be/src/common/daemon.cpp:471:10 #4 0x564788e79a96 in main /doris/be/src/service/doris_main.cpp:420:12 #5 0x7f217ca38564 in __libc_start_main csu/../csu/libc-start.c:332:16
1 parent d6bc088 commit f9166b7

File tree

4 files changed

+19
-18
lines changed

4 files changed

+19
-18
lines changed

be/src/runtime/memory/mem_tracker.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,16 @@
2424

2525
#include <mutex>
2626

27-
#include "common/daemon.h"
2827
#include "runtime/memory/mem_tracker_limiter.h"
2928
#include "runtime/thread_context.h"
3029

3130
namespace doris {
3231

33-
struct TrackerGroup {
34-
std::list<MemTracker*> trackers;
35-
std::mutex group_lock;
36-
};
37-
3832
// Save all MemTrackers in use to maintain the weak relationship between MemTracker and MemTrackerLimiter.
3933
// When MemTrackerLimiter prints statistics, all MemTracker statistics with weak relationship will be printed together.
4034
// Each group corresponds to several MemTrackerLimiters and has a lock.
4135
// Multiple groups are used to reduce the impact of locks.
42-
static std::vector<TrackerGroup> mem_tracker_pool(1000);
36+
std::vector<MemTracker::TrackerGroup> MemTracker::mem_tracker_pool(1000);
4337

4438
MemTracker::MemTracker(const std::string& label, RuntimeProfile* profile, MemTrackerLimiter* parent,
4539
const std::string& profile_counter_name)
@@ -82,7 +76,7 @@ void MemTracker::bind_parent(MemTrackerLimiter* parent) {
8276
}
8377

8478
MemTracker::~MemTracker() {
85-
if (_parent_group_num != -1 && !k_doris_exit) {
79+
if (_parent_group_num != -1) {
8680
std::lock_guard<std::mutex> l(mem_tracker_pool[_parent_group_num].group_lock);
8781
if (_tracker_group_it != mem_tracker_pool[_parent_group_num].trackers.end()) {
8882
mem_tracker_pool[_parent_group_num].trackers.erase(_tracker_group_it);

be/src/runtime/memory/mem_tracker.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ class MemTracker {
5757
int64_t peak_consumption = 0;
5858
};
5959

60+
struct TrackerGroup {
61+
std::list<MemTracker*> trackers;
62+
std::mutex group_lock;
63+
};
64+
6065
// A counter that keeps track of the current and peak value seen.
6166
// Relaxed ordering, not accurate in real time.
6267
class MemCounter {
@@ -182,6 +187,8 @@ class MemTracker {
182187
// Use _parent_label to correlate with parent limiter tracker.
183188
std::string _parent_label = "-";
184189

190+
static std::vector<TrackerGroup> mem_tracker_pool;
191+
185192
// Iterator into mem_tracker_pool for this object. Stored to have O(1) remove.
186193
std::list<MemTracker*>::iterator _tracker_group_it;
187194
};

be/src/runtime/memory/mem_tracker_limiter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include <queue>
2727
#include <utility>
2828

29-
#include "common/daemon.h"
3029
#include "runtime/exec_env.h"
3130
#include "runtime/fragment_mgr.h"
3231
#include "runtime/load_channel_mgr.h"
@@ -42,7 +41,8 @@ namespace doris {
4241
// Save all MemTrackerLimiters in use.
4342
// Each group corresponds to several MemTrackerLimiters and has a lock.
4443
// Multiple groups are used to reduce the impact of locks.
45-
static std::vector<TrackerLimiterGroup> mem_tracker_limiter_pool(MEM_TRACKER_GROUP_NUM);
44+
std::vector<MemTrackerLimiter::TrackerLimiterGroup> MemTrackerLimiter::mem_tracker_limiter_pool(
45+
MEM_TRACKER_GROUP_NUM);
4646

4747
std::atomic<bool> MemTrackerLimiter::_enable_print_log_process_usage {true};
4848

@@ -91,7 +91,7 @@ MemTrackerLimiter::~MemTrackerLimiter() {
9191
// in real time. Merge its consumption into orphan when parent is process, to avoid repetition.
9292
ExecEnv::GetInstance()->orphan_mem_tracker()->consume(_consumption->current_value());
9393
_consumption->set(0);
94-
if (!k_doris_exit) {
94+
{
9595
std::lock_guard<std::mutex> l(mem_tracker_limiter_pool[_group_num].group_lock);
9696
if (_tracker_limiter_group_it != mem_tracker_limiter_pool[_group_num].trackers.end()) {
9797
mem_tracker_limiter_pool[_group_num].trackers.erase(_tracker_limiter_group_it);

be/src/runtime/memory/mem_tracker_limiter.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,6 @@ class TaskGroup;
5050
using TaskGroupPtr = std::shared_ptr<TaskGroup>;
5151
} // namespace taskgroup
5252

53-
class MemTrackerLimiter;
54-
55-
struct TrackerLimiterGroup {
56-
std::list<MemTrackerLimiter*> trackers;
57-
std::mutex group_lock;
58-
};
59-
6053
// Track and limit the memory usage of process and query.
6154
// Contains an limit, arranged into a tree structure.
6255
//
@@ -76,6 +69,11 @@ class MemTrackerLimiter final : public MemTracker {
7669
6 // Experimental memory statistics, usually inaccurate, used for debugging, and expect to add other types in the future.
7770
};
7871

72+
struct TrackerLimiterGroup {
73+
std::list<MemTrackerLimiter*> trackers;
74+
std::mutex group_lock;
75+
};
76+
7977
inline static std::unordered_map<Type, std::shared_ptr<RuntimeProfile::HighWaterMarkCounter>>
8078
TypeMemSum = {{Type::GLOBAL,
8179
std::make_shared<RuntimeProfile::HighWaterMarkCounter>(TUnit::BYTES)},
@@ -272,6 +270,8 @@ class MemTrackerLimiter final : public MemTracker {
272270
bool _enable_print_log_usage = false;
273271
static std::atomic<bool> _enable_print_log_process_usage;
274272

273+
static std::vector<TrackerLimiterGroup> mem_tracker_limiter_pool;
274+
275275
// Iterator into mem_tracker_limiter_pool for this object. Stored to have O(1) remove.
276276
std::list<MemTrackerLimiter*>::iterator _tracker_limiter_group_it;
277277
};

0 commit comments

Comments
 (0)