-
Notifications
You must be signed in to change notification settings - Fork 182
Open
Description
It is possible to have two concurrently modified sets A, B, such that neither version of A is equal to any version of B, but == returns true.
Set equality is implemented like this:
Lines 386 to 390 in 366ce7e
| impl<K: Eq + Hash, S: BuildHasher + Clone> PartialEq for DashSet<K, S> { | |
| fn eq(&self, other: &Self) -> bool { | |
| self.len() == other.len() && self.iter().all(|r| other.contains(r.key())) | |
| } | |
| } |
Consider the following sequence of operations:
- We start with
A = {1}, B = {2}. - Thread 1 starts the comparison, verifying
A.len() == B.len(). - Thread 2 inserts
1intoB, resulting inB = {1, 2}. - Thread 1 finishes the comparison, verifying that the sole element
1ofAis present inB.
A similar counterexample exists for maps.
This means that PartialEq is not just inefficient, but strictly speaking incorrect. If the intention is testing code, perhaps eq should lock the whole map/set before performing comparison? I'm guessing consistently locking the shards in order should avoid deadlocks.
Metadata
Metadata
Assignees
Labels
No labels