Skip to content

Commit

Permalink
adjust the sequence of lock
Browse files Browse the repository at this point in the history
Signed-off-by: Xintao <[email protected]>
  • Loading branch information
hunterlxt committed Jun 1, 2020
1 parent bd75a2c commit 45546b8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,20 +599,19 @@ pub fn new_fail_group() -> FailPointRegistry {
impl FailPointRegistry {
/// Register the current thread to this failpoints registry.
pub fn register_current(&self) {
let mut registry_group = REGISTRY_GROUP.write().unwrap();
let mut registry = self.registry.write().unwrap();
let id = thread::current().id();
REGISTRY_GROUP
.write()
.unwrap()
.insert(id, self.registry.clone());
registry_group.insert(id, self.registry.clone());
registry.threads.insert(id);
}

/// Deregister the current thread to this failpoints registry.
pub fn deregister_current(&self) {
let mut registry_group = REGISTRY_GROUP.write().unwrap();
let mut registry = self.registry.write().unwrap();
let id = thread::current().id();
REGISTRY_GROUP.write().unwrap().remove(&id);
registry_group.remove(&id);
registry.threads.remove(&id);
}

Expand Down Expand Up @@ -687,12 +686,12 @@ pub fn teardown() {

/// Clean all registered fail points.
fn cleanup(registry: &mut std::sync::RwLockWriteGuard<Registry>) {
let mut group = REGISTRY_GROUP.write().unwrap();
for p in registry.failpoints.values() {
// wake up all pause failpoint.
p.set_actions("", vec![]);
}
registry.failpoints.clear();
let mut group = REGISTRY_GROUP.write().unwrap();
for id in registry.threads.iter() {
group.remove(id);
}
Expand Down
23 changes: 23 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,26 @@ fn test_multiple_threads_cleanup() {
);
t.join().unwrap();
}

#[test]
fn test_deadlock() {
let group = fail::new_fail_group();

fail::cfg("point", "return").unwrap();
let f = || {
fail_point!("point", |_| 1);
0
};

let thread = thread::spawn(move || {
for _ in 0..5000000 {
group.register_current()
}
});

for _ in 0..5000000 {
f();
}

thread.join().unwrap();
}

0 comments on commit 45546b8

Please sign in to comment.