Skip to content

Commit 145e10d

Browse files
committed
f some sp, some stupid missing code, more docs
1 parent f7a6418 commit 145e10d

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

lightning/src/debug_sync.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::sync::RwLockWriteGuard as StdRwLockWriteGuard;
1616
use std::sync::Condvar as StdCondvar;
1717

1818
#[cfg(feature = "backtrace")]
19-
use {prelude::HashMap, backtrace::Backtrace, std::sync::Once};
19+
use {prelude::{HashMap, hash_map}, backtrace::Backtrace, std::sync::Once};
2020

2121
#[cfg(not(feature = "backtrace"))]
2222
struct Backtrace{}
@@ -111,6 +111,9 @@ impl LockMetadata {
111111
let sync_mutex_constr_regex = regex::Regex::new(r"lightning.*debug_sync.*new").unwrap();
112112
let mut found_debug_sync = false;
113113
for frame in backtrace.frames() {
114+
// If a constructor was inlined we should take the frame in which it was inlined
115+
// (as its specific to the callsite), thus we look at the last available symbol,
116+
// which the `backtrace` docs say will be the caller.
114117
let symbol_name = frame.symbols().last().unwrap().name().unwrap().as_str().unwrap();
115118
if !sync_mutex_constr_regex.is_match(symbol_name) {
116119
if found_debug_sync {
@@ -126,11 +129,21 @@ impl LockMetadata {
126129
}
127130
}
128131

129-
Arc::new(LockMetadata {
132+
let res = Arc::new(LockMetadata {
130133
locked_before: StdMutex::new(HashSet::new()),
131134
lock_idx,
132135
lock_construction_bt: backtrace,
133-
})
136+
});
137+
138+
#[cfg(feature = "backtrace")]
139+
{
140+
let mut locks = unsafe { LOCKS.as_ref() }.unwrap().lock().unwrap();
141+
match locks.entry(lock_idx) {
142+
hash_map::Entry::Occupied(e) => return Arc::clone(e.get()),
143+
hash_map::Entry::Vacant(e) => { e.insert(Arc::clone(&res)); },
144+
}
145+
}
146+
res
134147
}
135148

136149
// Returns whether we were a recursive lock (only relevant for read)
@@ -150,7 +163,7 @@ impl LockMetadata {
150163
if !read && *locked == *this {
151164
// With `feature = "backtrace"` set, we may be looking at different instances
152165
// of the same lock.
153-
debug_assert!(cfg!(feature = "backtrace"), "Tried to lock a lock while it was held!");
166+
debug_assert!(cfg!(feature = "backtrace"), "Tried to acquire a lock while it was held!");
154167
}
155168
for locked_dep in locked.locked_before.lock().unwrap().iter() {
156169
if locked_dep.lock == *this && locked_dep.lock != *locked {
@@ -409,7 +422,7 @@ mod tests {
409422
}
410423

411424
#[test]
412-
fn read_recurisve_no_lockorder() {
425+
fn read_recursive_no_lockorder() {
413426
// Like the above, but note that no lockorder is implied when we recursively read-lock a
414427
// RwLock, causing this to pass just fine.
415428
let a = RwLock::new(());

0 commit comments

Comments
 (0)