Skip to content

Don't hold the Target's ModuleListLock over running LoadScriptingResourceInTarget #138216

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 2 commits into from
May 2, 2025

Conversation

jimingham
Copy link
Collaborator

That calls an unknown amount of Python code, and can do quite a bit of work - especially if people do things like launch scripted processes in this script affordance. Doing that while holding a major lock like the ModuleList lock is asking for trouble.

I tried to make a test that would actually stall without this, but I couldn't come up with anything that reliably failed. You always have to get pretty unlucky.

…urceInTarget.

That calls an unknown amount of Python code, and can do quite a bit of work -
especially if people do things like launch scripted processes in this script
affordance.  Doing that while holding a major lock like the ModuleList lock
is asking for trouble.

I tried to make a test that would actually stall without this, but I couldn't
come up with anything that reliably failed.  You always have to get pretty
unlucky.
@jimingham jimingham requested a review from JDevlieghere as a code owner May 1, 2025 23:37
@llvmbot llvmbot added the lldb label May 1, 2025
@llvmbot
Copy link
Member

llvmbot commented May 1, 2025

@llvm/pr-subscribers-lldb

Author: None (jimingham)

Changes

That calls an unknown amount of Python code, and can do quite a bit of work - especially if people do things like launch scripted processes in this script affordance. Doing that while holding a major lock like the ModuleList lock is asking for trouble.

I tried to make a test that would actually stall without this, but I couldn't come up with anything that reliably failed. You always have to get pretty unlucky.


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

1 Files Affected:

  • (modified) lldb/source/Core/ModuleList.cpp (+8-2)
diff --git a/lldb/source/Core/ModuleList.cpp b/lldb/source/Core/ModuleList.cpp
index 6052cc151744d..9724039decf0d 100644
--- a/lldb/source/Core/ModuleList.cpp
+++ b/lldb/source/Core/ModuleList.cpp
@@ -1046,8 +1046,14 @@ bool ModuleList::LoadScriptingResourcesInTarget(Target *target,
                                                 bool continue_on_error) {
   if (!target)
     return false;
-  std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
-  for (auto module : m_modules) {
+  m_modules_mutex.lock();
+  // Don't hold the module list mutex while loading the scripting resources,
+  // The initializer might do any amount of work, and having that happen while
+  // the module list is held is asking for A/B locking problems.
+  const ModuleList tmp_module_list(*this);
+  m_modules_mutex.unlock();
+  
+  for (auto module : tmp_module_list.ModulesNoLocking()) {
     if (module) {
       Status error;
       if (!module->LoadScriptingResourceInTarget(target, error,

Copy link

github-actions bot commented May 1, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff HEAD~1 HEAD --extensions cpp -- lldb/source/Core/ModuleList.cpp
View the diff from clang-format here.
diff --git a/lldb/source/Core/ModuleList.cpp b/lldb/source/Core/ModuleList.cpp
index 9724039de..d5ddf6e84 100644
--- a/lldb/source/Core/ModuleList.cpp
+++ b/lldb/source/Core/ModuleList.cpp
@@ -1052,7 +1052,7 @@ bool ModuleList::LoadScriptingResourcesInTarget(Target *target,
   // the module list is held is asking for A/B locking problems.
   const ModuleList tmp_module_list(*this);
   m_modules_mutex.unlock();
-  
+
   for (auto module : tmp_module_list.ModulesNoLocking()) {
     if (module) {
       Status error;

@jimingham jimingham merged commit 0ddcd20 into llvm:main May 2, 2025
5 of 9 checks passed
@jimingham jimingham deleted the no-module-list-lock branch May 2, 2025 18:51
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…urceInTarget (llvm#138216)

That calls an unknown amount of Python code, and can do quite a bit of
work - especially if people do things like launch scripted processes in
this script affordance. Doing that while holding a major lock like the
ModuleList lock is asking for trouble.

I tried to make a test that would actually stall without this, but I
couldn't come up with anything that reliably failed. You always have to
get pretty unlucky.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…urceInTarget (llvm#138216)

That calls an unknown amount of Python code, and can do quite a bit of
work - especially if people do things like launch scripted processes in
this script affordance. Doing that while holding a major lock like the
ModuleList lock is asking for trouble.

I tried to make a test that would actually stall without this, but I
couldn't come up with anything that reliably failed. You always have to
get pretty unlucky.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…urceInTarget (llvm#138216)

That calls an unknown amount of Python code, and can do quite a bit of
work - especially if people do things like launch scripted processes in
this script affordance. Doing that while holding a major lock like the
ModuleList lock is asking for trouble.

I tried to make a test that would actually stall without this, but I
couldn't come up with anything that reliably failed. You always have to
get pretty unlucky.
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
…urceInTarget (llvm#138216)

That calls an unknown amount of Python code, and can do quite a bit of
work - especially if people do things like launch scripted processes in
this script affordance. Doing that while holding a major lock like the
ModuleList lock is asking for trouble.

I tried to make a test that would actually stall without this, but I
couldn't come up with anything that reliably failed. You always have to
get pretty unlucky.
jimingham added a commit to jimingham/from-apple-llvm-project that referenced this pull request May 23, 2025
…urceInTarget (llvm#138216)

That calls an unknown amount of Python code, and can do quite a bit of
work - especially if people do things like launch scripted processes in
this script affordance. Doing that while holding a major lock like the
ModuleList lock is asking for trouble.

I tried to make a test that would actually stall without this, but I
couldn't come up with anything that reliably failed. You always have to
get pretty unlucky.

(cherry picked from commit 0ddcd20)
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.

3 participants