From e2db0eaca8fe481581df45a3a4beb3dde66165cd Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Thu, 27 May 2021 20:31:00 +0200 Subject: [PATCH] Fix a graph invariant violation on cycle detection --- CHANGELOG.md | 3 +++ src/graph.rs | 2 +- src/lib.rs | 12 ++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c30027..2ccf6a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). type aliases function the same as the ones they belong to, resolving to either the tracing versions when debug assertions are enabled or the standard one when they're not. +### Fixed +- Fixed a corruption error where deallocating a previously cyclic mutex could result in a panic. + ## [0.1.1] - 2021-05-24 ### Changed diff --git a/src/graph.rs b/src/graph.rs index b1a8de3..1068bcb 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -127,7 +127,7 @@ where // We use map instead of unwrap to avoid an `unwrap()` but we know that these // entries are present as we just added them above. self.nodes.get_mut(&y).map(|node| node.in_edges.remove(&x)); - self.nodes.get_mut(&x).map(|node| node.out_edges.remove(&x)); + self.nodes.get_mut(&x).map(|node| node.out_edges.remove(&y)); // No edge was added return false; diff --git a/src/lib.rs b/src/lib.rs index 8aae2f0..a578753 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -304,6 +304,18 @@ mod tests { assert!(get_dependency_graph().add_edge(c.value(), a.value())); } + /// Test creating a cycle, then panicking. + #[test] + #[should_panic] + fn test_mutex_id_conflict() { + let ids = [MutexId::new(), MutexId::new(), MutexId::new()]; + + for i in 0..3 { + let _first_lock = ids[i].get_borrowed(); + let _second_lock = ids[(i + 1) % 3].get_borrowed(); + } + } + /// Fuzz the global dependency graph by fake-acquiring lots of mutexes in a valid order. /// /// This test generates all possible forward edges in a 100-node graph consisting of natural