Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/shard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ impl<Key, Val, We, B, L, Plh: SharedPlaceholder> CacheShard<Key, Val, We, B, L,
matches!(entry, Entry::Placeholder(Placeholder { shared, .. }) if shared.same_as(placeholder))
}) {
entry.remove();
self.entries.remove(placeholder.idx());
}
}
Comment thread
hanabi1224 marked this conversation as resolved.

Expand Down
52 changes: 52 additions & 0 deletions src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,7 @@ impl<Key, Val> Lifecycle<Key, Val> for DefaultLifecycle<Key, Val> {
#[cfg(test)]
mod tests {
use super::*;
use crate::shard::SharedPlaceholder as _;
use std::{
sync::{Arc, Barrier},
thread,
Expand Down Expand Up @@ -1763,4 +1764,55 @@ mod tests {
drop(guards);
assert_eq!(cache.get(&2), None);
}

#[test]
fn test_guard_leak() {
let cache: Cache<i32, i32> = Cache::new(8);
let guard1 = match cache.get_value_or_guard(&1, None) {
GuardResult::Guard(g) => g,
_ => panic!("expected guard"),
};
let idx1 = guard1.shared().idx();
drop(guard1);
let guard2 = match cache.get_value_or_guard(&1, None) {
GuardResult::Guard(g) => g,
_ => panic!("expected guard"),
};
let idx2 = guard2.shared().idx();
drop(guard2);
assert_eq!(idx1, idx2);
}

// A real insert overwrites the placeholder in place, reusing its slab slot as
// a Resident. Dropping the now-stale guard must not free that slot, otherwise
// the live entry is evicted while the map still references it.
#[test]
fn test_guard_drop_after_overwrite_insert() {
let cache: Cache<i32, i32> = Cache::new(8);
let guard = match cache.get_value_or_guard(&1, None) {
GuardResult::Guard(g) => g,
_ => panic!("expected guard"),
};
cache.insert(1, 100);
assert_eq!(cache.get(&1), Some(100));
drop(guard);
assert_eq!(cache.get(&1), Some(100));
}

// A remove frees the placeholder's slab slot, which a later insert reuses for a
// different key. Dropping the original guard must not free that slot again, or
// it evicts the unrelated key.
#[test]
fn test_guard_drop_after_remove_and_reuse() {
let cache: Cache<i32, i32> = Cache::new(8);
let guard = match cache.get_value_or_guard(&1, None) {
GuardResult::Guard(g) => g,
_ => panic!("expected guard"),
};
cache.remove(&1);
cache.insert(2, 222);
assert_eq!(cache.get(&2), Some(222));
drop(guard);
assert_eq!(cache.get(&2), Some(222));
}
}
7 changes: 7 additions & 0 deletions src/sync_placeholder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ pub struct PlaceholderGuard<'a, Key, Val, We, B, L> {
inserted: bool,
}

#[cfg(test)]
impl<'a, Key, Val, We, B, L> PlaceholderGuard<'a, Key, Val, We, B, L> {
pub fn shared(&self) -> &SharedPlaceholder<Val> {
&self.shared
}
}

#[derive(Debug)]
enum Waiter {
Thread {
Expand Down
Loading