[lldb-dap] Simplify DAPSessionManager GetInstance#209264
Conversation
We don't need the `std::call_once` as this guaranteed to be thread safe and initialized once. I also don't see any issue with the destructor chain since the class doesn't own any data that can be affected.
|
@llvm/pr-subscribers-lldb Author: Ebuka Ezike (da-viper) ChangesWe don't need the Full diff: https://github.com/llvm/llvm-project/pull/209264.diff 1 Files Affected:
diff --git a/lldb/tools/lldb-dap/DAPSessionManager.cpp b/lldb/tools/lldb-dap/DAPSessionManager.cpp
index 580d4fa102a89..91c0538e63d5f 100644
--- a/lldb/tools/lldb-dap/DAPSessionManager.cpp
+++ b/lldb/tools/lldb-dap/DAPSessionManager.cpp
@@ -33,14 +33,8 @@ ManagedEventThread::~ManagedEventThread() {
}
DAPSessionManager &DAPSessionManager::GetInstance() {
- static std::once_flag initialized;
- static DAPSessionManager *instance =
- nullptr; // NOTE: intentional leak to avoid issues with C++ destructor
- // chain
-
- std::call_once(initialized, []() { instance = new DAPSessionManager(); });
-
- return *instance;
+ static DAPSessionManager instance;
+ return instance;
}
void DAPSessionManager::RegisterSession(lldb_private::MainLoop *loop,
|
JDevlieghere
left a comment
There was a problem hiding this comment.
Dropping call_once is fine, the statics handles thread-safe init. But this also drops the intentional leak which I think was load bearing.
Now the destructor will run at program exit. Client sessions run on detached threads and UnregisterSession ends with notify_all_at_thread_exit. The main thread only waits for the sessions map to empty, which happens before the detached thread finishes its exit tail. So there's a window where ~DAPSessionManager tears down m_sessions_mutex/m_sessions_condition while a detached thread is still notifying them. That's what the old comment was about.
Let's keep the leak but drop the call_once:
DAPSessionManager &DAPSessionManager::GetInstance() {
// Intentionally leaked: detached client threads may still be notifying on
// m_sessions_condition at exit, so it has to outlive them.
static DAPSessionManager *instance = new DAPSessionManager();
return *instance;
}
We don't need the
std::call_onceas this guaranteed to be thread safe and initialised once since c++11.I also don't see any issue with the destructor chain since the class doesn't own any data that can be affected.