Skip to content

Commit 8697b64

Browse files
committed
Using parking lot to avoid poison lock during panic
Signed-off-by: Xuanwo <github@xuanwo.io>
1 parent 53ce0ab commit 8697b64

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/common/base/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ futures = "0.3.24"
3434
libc = "0.2.133"
3535
num_cpus = "1.13.1"
3636
once_cell = "1.15.0"
37+
parking_lot = "0.12"
3738
pin-project-lite = "0.2.9"
3839
pprof = { version = "0.10.1", features = [
3940
"flamegraph",

src/common/base/src/base/singleton_instance.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub enum Singleton {
2525
Production(Container![Send + Sync]),
2626

2727
#[cfg(debug_assertions)]
28-
Testing(std::sync::Mutex<std::collections::HashMap<String, Container![Send + Sync]>>),
28+
Testing(parking_lot::Mutex<std::collections::HashMap<String, Container![Send + Sync]>>),
2929
}
3030

3131
unsafe impl Send for Singleton {}
@@ -45,7 +45,7 @@ impl Singleton {
4545
Some(name) => name,
4646
None => panic!("thread doesn't have name"),
4747
};
48-
let guard = c.lock().expect("lock must succeed");
48+
let guard = c.lock();
4949
let v: &T = guard
5050
.get(thread_name)
5151
.expect("thread {name} is not initiated")
@@ -65,7 +65,7 @@ impl Singleton {
6565
Some(name) => name,
6666
None => panic!("thread doesn't have name"),
6767
};
68-
let mut guard = c.lock().expect("lock must succeed");
68+
let mut guard = c.lock();
6969
let c = guard.entry(thread_name.to_string()).or_default();
7070
c.set(value)
7171
}
@@ -104,7 +104,7 @@ impl GlobalInstance {
104104
/// Should only be initiated once and only used in testing.
105105
#[cfg(debug_assertions)]
106106
pub fn init_testing() {
107-
let _ = GLOBAL.set(Singleton::Testing(std::sync::Mutex::default()));
107+
let _ = GLOBAL.set(Singleton::Testing(parking_lot::Mutex::default()));
108108
}
109109

110110
/// drop testing global data by thread name.
@@ -117,7 +117,7 @@ impl GlobalInstance {
117117
unreachable!("drop_testing should never be called on production global")
118118
}
119119
Singleton::Testing(c) => {
120-
let mut guard = c.lock().expect("lock must succeed");
120+
let mut guard = c.lock();
121121
let _ = guard.remove(thread_name);
122122
}
123123
}

0 commit comments

Comments
 (0)