-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[lldb] fix dead lock in TypeCategoryMap.cpp #87540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
FormatManager::GetCategoryForLanguage and FormatManager::GetCategory(can_create = true) can be called concurently and they both take the TypeCategory::m_map_mutex and the FormatManager::m_language_categories_mutex but in reverse order. Without the patch, we can have a dead lock.
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-lldb Author: Vincent Belliard (v-bulle) ChangesFormatManager::GetCategoryForLanguage and Without the patch, we can have a dead lock. Full diff: https://github.com/llvm/llvm-project/pull/87540.diff 1 Files Affected:
diff --git a/lldb/source/DataFormatters/TypeCategoryMap.cpp b/lldb/source/DataFormatters/TypeCategoryMap.cpp
index fd76bab95826af..0d1f55fff473d1 100644
--- a/lldb/source/DataFormatters/TypeCategoryMap.cpp
+++ b/lldb/source/DataFormatters/TypeCategoryMap.cpp
@@ -25,19 +25,23 @@ TypeCategoryMap::TypeCategoryMap(IFormatChangeListener *lst)
}
void TypeCategoryMap::Add(KeyType name, const TypeCategoryImplSP &entry) {
- std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
- m_map[name] = entry;
+ {
+ std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
+ m_map[name] = entry;
+ }
if (listener)
listener->Changed();
}
bool TypeCategoryMap::Delete(KeyType name) {
- std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
- MapIterator iter = m_map.find(name);
- if (iter == m_map.end())
- return false;
- m_map.erase(name);
- Disable(name);
+ {
+ std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
+ MapIterator iter = m_map.find(name);
+ if (iter == m_map.end())
+ return false;
+ m_map.erase(name);
+ Disable(name);
+ }
if (listener)
listener->Changed();
return true;
@@ -123,9 +127,11 @@ void TypeCategoryMap::DisableAllCategories() {
}
void TypeCategoryMap::Clear() {
- std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
- m_map.clear();
- m_active_categories.clear();
+ {
+ std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
+ m_map.clear();
+ m_active_categories.clear();
+ }
if (listener)
listener->Changed();
}
|
The change itself looks fine, but I'm not sure I follow the motivation. I see where |
I'm struggling to understand how your description matches the change as well. You mention Something that might help is a way to reproduce the deadlock. I know deadlocks are not deterministically reproducible in most cases, but if you could walk us through the scenario, it might be easier to see. |
In one thread we have: For the first thread, GetCategoryForLanguage takes m_language_categories_mutex and then ends calling TypeCategoryMap::Get which takes m_map_mutex In the other thread GetCategory calls TypeCategoryMap::Add which takes m_map_mutex and then calls FormatManager::Changed() which takes m_language_categories_mutex With the change, in TypeCategoryMap::Add, we release m_map_mutex before calling Changed (which doesn't need it) and avoid the dead lock. The pull request also releases the lock in Delete and Clear to be consistent. |
Alright, so this happens when |
std::lock_guard<std::recursive_mutex> guard(m_map_mutex); | ||
m_map[name] = entry; | ||
} | ||
// The lock is now released for the eventual call to Changed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment explains what you're doing, which the code already tells me, but not why.
I'd expect something like this, although it doesn't need to be quite so verbose:
// Release the mutex to avoid a potential deadlock between TypeCategoryMap::m_map_mutex and FormatManager::m_language_categories_mutex which can be acquired in reverse order when calling FormatManager::Changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@v-bulle Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested Please check whether problems have been caused by your change specifically, as How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
FormatManager::GetCategoryForLanguage and
FormatManager::GetCategory(can_create = true) can be called concurrently and they both take the TypeCategory::m_map_mutex and the FormatManager::m_language_categories_mutex but in reverse order.
On one thread, GetCategoryForLanguage takes m_language_categories_mutex and then ends calling TypeCategoryMap::Get which takes m_map_mutex
On another thread GetCategory calls TypeCategoryMap::Add which takes m_map_mutex and then calls FormatManager::Changed() which takes m_language_categories_mutex
If both threads are running concurrently, we have a dead lock.
The patch releases the m_map_mutex before calling Changed which avoids the dead lock.