Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure crossbeam-epoch to run GC when dropping a cache #384

Merged
merged 5 commits into from
Jan 20, 2024
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
15 changes: 15 additions & 0 deletions src/future/base_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,21 @@ pub(crate) struct Inner<K, V, S> {
clocks: Clocks,
}

impl<K, V, S> Drop for Inner<K, V, S> {
fn drop(&mut self) {
// Ensure crossbeam-epoch to collect garbages (`deferred_fn`s) in the
// global bag so that previously cached values will be dropped.
for _ in 0..128 {
crossbeam_epoch::pin().flush();
}

// NOTE: The `CacheStore` (`cht`) will be dropped after returning from this
// `drop` method. It uses crossbeam-epoch internally, but we do not have to
// call `flush` for it because its `drop` methods do not create
// `deferred_fn`s, and drop its values in place.
}
}

//
// functions/methods used by BaseCache
//
Expand Down
12 changes: 12 additions & 0 deletions src/future/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3812,7 +3812,7 @@
// Note that MyError does not implement std::error::Error trait
// like anyhow::Error.
#[derive(Debug)]
pub struct MyError(String);

Check warning on line 3815 in src/future/cache.rs

View workflow job for this annotation

GitHub Actions / test

field `0` is never read

Check warning on line 3815 in src/future/cache.rs

View workflow job for this annotation

GitHub Actions / test

field `0` is never read

Check warning on line 3815 in src/future/cache.rs

View workflow job for this annotation

GitHub Actions / test

field `0` is never read

Check warning on line 3815 in src/future/cache.rs

View workflow job for this annotation

GitHub Actions / test

field `0` is never read

type MyResult<T> = Result<T, Arc<MyError>>;

Expand Down Expand Up @@ -3949,7 +3949,7 @@
// Note that MyError does not implement std::error::Error trait
// like anyhow::Error.
#[derive(Debug)]
pub struct MyError(String);

Check warning on line 3952 in src/future/cache.rs

View workflow job for this annotation

GitHub Actions / test

field `0` is never read

Check warning on line 3952 in src/future/cache.rs

View workflow job for this annotation

GitHub Actions / test

field `0` is never read

Check warning on line 3952 in src/future/cache.rs

View workflow job for this annotation

GitHub Actions / test

field `0` is never read

Check warning on line 3952 in src/future/cache.rs

View workflow job for this annotation

GitHub Actions / test

field `0` is never read

Check warning on line 3952 in src/future/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3952 in src/future/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3952 in src/future/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3952 in src/future/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3952 in src/future/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3952 in src/future/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3952 in src/future/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3952 in src/future/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

type MyResult<T> = Result<T, Arc<MyError>>;

Expand Down Expand Up @@ -5454,6 +5454,18 @@
assert_eq!(counters.value_dropped(), KEYS, "value_dropped");
}

// https://github.com/moka-rs/moka/issues/383
#[tokio::test]
async fn ensure_gc_runs_when_dropping_cache() {
let cache = Cache::builder().build();
let val = Arc::new(0);
cache
.get_with(1, std::future::ready(Arc::clone(&val)))
.await;
drop(cache);
assert_eq!(Arc::strong_count(&val), 1);
}

#[tokio::test]
async fn test_debug_format() {
let cache = Cache::new(10);
Expand Down
13 changes: 13 additions & 0 deletions src/sync/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3456,7 +3456,7 @@
// Note that MyError does not implement std::error::Error trait like
// anyhow::Error.
#[derive(Debug)]
pub struct MyError(String);

Check warning on line 3459 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test

field `0` is never read

Check warning on line 3459 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test

field `0` is never read

Check warning on line 3459 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test

field `0` is never read

Check warning on line 3459 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test

field `0` is never read

Check warning on line 3459 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3459 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3459 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3459 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3459 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3459 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3459 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3459 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3459 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3459 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

type MyResult<T> = Result<T, Arc<MyError>>;

Expand Down Expand Up @@ -3597,7 +3597,7 @@
// Note that MyError does not implement std::error::Error trait like
// anyhow::Error.
#[derive(Debug)]
pub struct MyError(String);

Check warning on line 3600 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test

field `0` is never read

Check warning on line 3600 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test

field `0` is never read

Check warning on line 3600 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test

field `0` is never read

Check warning on line 3600 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test

field `0` is never read

Check warning on line 3600 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3600 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3600 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3600 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3600 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3600 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3600 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3600 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3600 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3600 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3600 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3600 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3600 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3600 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3600 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3600 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3600 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

Check warning on line 3600 in src/sync/cache.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

field `0` is never read

type MyResult<T> = Result<T, Arc<MyError>>;

Expand Down Expand Up @@ -4831,6 +4831,19 @@
assert_eq!(counters.value_dropped(), KEYS, "value_dropped");
}

// https://github.com/moka-rs/moka/issues/383
#[test]
fn ensure_gc_runs_when_dropping_cache() {
let cache = Cache::builder().build();
let val = Arc::new(0);
{
let val = Arc::clone(&val);
cache.get_with(1, move || val);
}
drop(cache);
assert_eq!(Arc::strong_count(&val), 1);
}

#[test]
fn test_debug_format() {
let cache = Cache::new(10);
Expand Down
15 changes: 15 additions & 0 deletions src/sync_base/base_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,21 @@ pub(crate) struct Inner<K, V, S> {
clocks: Clocks,
}

impl<K, V, S> Drop for Inner<K, V, S> {
fn drop(&mut self) {
// Ensure crossbeam-epoch to collect garbages (`deferred_fn`s) in the
// global bag so that previously cached values will be dropped.
for _ in 0..128 {
crossbeam_epoch::pin().flush();
}

// NOTE: The `CacheStore` (`cht`) will be dropped after returning from this
// `drop` method. It uses crossbeam-epoch internally, but we do not have to
// call `flush` for it because its `drop` methods do not create
// `deferred_fn`s, and drop its values in place.
}
}

//
// functions/methods used by BaseCache
//
Expand Down
Loading