Skip to content

[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

Merged
merged 3 commits into from
Apr 12, 2024
Merged

Conversation

v-bulle
Copy link
Contributor

@v-bulle v-bulle commented Apr 3, 2024

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.

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.
@v-bulle v-bulle requested a review from JDevlieghere as a code owner April 3, 2024 18:40
Copy link

github-actions bot commented Apr 3, 2024

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

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
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

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.

@llvmbot llvmbot added the lldb label Apr 3, 2024
@llvmbot
Copy link
Member

llvmbot commented Apr 3, 2024

@llvm/pr-subscribers-lldb

Author: Vincent Belliard (v-bulle)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/87540.diff

1 Files Affected:

  • (modified) lldb/source/DataFormatters/TypeCategoryMap.cpp (+17-11)
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();
 }

@JDevlieghere
Copy link
Member

The change itself looks fine, but I'm not sure I follow the motivation. I see where FormatManager::GetCategoryForLanguage locks m_language_categories_mutex and I see where FormatManager::GetCategory calls TypeCategoryMap::Get and locks m_map_mutex, but I don't immediately see where they're acquiring the other mutex. Can you elaborate on the situation that causes the deadlock?

@bulbazord
Copy link
Member

I'm struggling to understand how your description matches the change as well. You mention FormatManager::GetCategoryForLanguage and FormatManager::GetCategory but your change doesn't touch those. Instead you modify TypeCategoryMap::Add, TypeCategoryMap::Delete, and TypeCategoryMap::Clear. It makes sense that those only hold onto mutex for the short time that they need them, but why does changing those prevent the deadlock?

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.

@v-bulle
Copy link
Contributor Author

v-bulle commented Apr 4, 2024

In one thread we have:
frame #3: 0x0000560bc08118c3 lldblldb_private::TypeCategoryMap::Get(lldb_private::ConstString, std::__u::shared_ptr<lldb_private::TypeCategoryImpl>&) + 35 frame #4: 0x0000560bc0800a1e lldblldb_private::FormatManager::GetCategory(lldb_private::ConstString, bool) + 62
frame #5: 0x0000560bc07fc58e lldblldb_private::DataVisualization::Categories::GetCategory(lldb_private::ConstString, std::__u::shared_ptr<lldb_private::TypeCategoryImpl>&, bool) + 46 frame #6: 0x0000560bc099d5b9 lldb___lldb_unnamed_symbol166675 + 57
frame #7: 0x0000560bc5c42c15 lldbstd::__u::__call_once(unsigned long volatile&, void*, void (*)(void*)) + 149 frame #8: 0x0000560bc099c6ac lldblldb_private::ObjCLanguage::GetFormatters() + 76
frame #9: 0x0000560bc0805daf lldblldb_private::LanguageCategory::LanguageCategory(lldb::LanguageType) + 143 frame #10: 0x0000560bc080139e lldblldb_private::FormatManager::GetCategoryForLanguage(lldb::LanguageType) + 126
And in the other:
frame #3: 0x0000560bc07fe586 lldblldb_private::FormatManager::Changed() + 38 frame #4: 0x0000560bc08114cb lldblldb_private::TypeCategoryMap::Add(lldb_private::ConstString, std::__u::shared_ptr<lldb_private::TypeCategoryImpl> const&) + 139
frame #5: 0x0000560bc0800aba lldb`lldb_private::FormatManager::GetCategory(lldb_private::ConstString, bool) + 218

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.

@JDevlieghere
Copy link
Member

Alright, so this happens when listener in TypeCategoryMap is the TypeCategory. Please update the commit message and add a comment before the call to Changed to say why we're doing it outside the scope of the lock. With those changes this should be good to go.

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.
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Member

@JDevlieghere JDevlieghere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@oontvoo oontvoo merged commit 5752e31 into llvm:main Apr 12, 2024
Copy link

@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
by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

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.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants