[Lldb-commits] [lldb] [lldb] Use Policy for ProcessRunLock re-entrancy (PR #195774)
Jonas Devlieghere via lldb-commits
lldb-commits at lists.llvm.org
Fri May 8 11:02:23 PDT 2026
================
@@ -32,9 +37,42 @@ bool ProcessRunLock::ReadTryLock() {
}
bool ProcessRunLock::ReadUnlock() {
+ Policy policy = PolicyStack::Get().Current();
+ if (policy.capabilities.holds_run_lock)
+ return true;
+
return ::pthread_rwlock_unlock(&m_rwlock) == 0;
}
+bool ProcessRunLock::ProcessRunLocker::TryLock(ProcessRunLock *lock) {
+ if (m_lock) {
+ if (m_lock == lock)
+ return true;
+ Unlock();
+ }
+ if (lock) {
+ if (lock->ReadTryLock()) {
+ m_lock = lock;
+ // Push a policy so re-entrant ReadTryLock calls from the same
+ // thread (e.g. provider Python calling back into SB API) skip
+ // the real lock and avoid deadlocking with a pending writer.
+ Policy policy = PolicyStack::Get().Current();
+ policy.capabilities.holds_run_lock = true;
+ PolicyStack::Get().Push(policy);
----------------
JDevlieghere wrote:
I wonder if we should find a way to make it harder to do the wrong thing. The policies should be immutable (which they are by virtue of us returning a copy when you look at the top of the stack). But if you can just modify it willy-nilly, there's really no safety net.
I'm wondering if ideally, we should have a bunch of factories that do this for you. For example, you should never set `holds_run_lock` at the same time as say `can_run_frame_recognizers`.
I'm imagining something like:
```
PolicyStack::PushRunLockPolicy(true/false);
```
which does the clone + change. The `Push` would become private. In that worlds you can get your hands on a Policy, a modify it if you want, but you can never push it.
Of course it's possible to bypass that by pushing multiple policies after each other using the factories, but at that point it's pretty obvious that what you're doing is wrong. The idea is to make it "easy to do the right thing and hard to do the wrong thing".
The downside of this approach is a bunch of factory helpers.
https://github.com/llvm/llvm-project/pull/195774
More information about the lldb-commits
mailing list