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

fix once async sync_writes deadlock #175

Merged
merged 1 commit into from
Nov 6, 2023
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
6 changes: 3 additions & 3 deletions cached_proc_macro/src/once.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ pub fn once(args: TokenStream, input: TokenStream) -> TokenStream {
#set_cache_and_return
};

let return_cache_block = quote! {
let r_lock_return_cache_block = quote! {
{
#r_lock
if let Some(result) = &*cached {
Expand All @@ -216,7 +216,7 @@ pub fn once(args: TokenStream, input: TokenStream) -> TokenStream {

let do_set_return_block = if args.sync_writes {
quote! {
#return_cache_block
#r_lock_return_cache_block
#w_lock
if let Some(result) = &*cached {
#return_cache_block
Expand All @@ -226,7 +226,7 @@ pub fn once(args: TokenStream, input: TokenStream) -> TokenStream {
}
} else {
quote! {
#return_cache_block
#r_lock_return_cache_block
#function_call
#w_lock
#set_cache_and_return
Expand Down
27 changes: 27 additions & 0 deletions tests/cached.rs
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,33 @@ async fn test_cached_sync_writes_a() {
assert_eq!(a, c.await.unwrap());
}

#[cfg(feature = "async")]
#[once(sync_writes = true)]
async fn once_sync_writes_a(s: &tokio::sync::Mutex<String>) -> String {
let mut guard = s.lock().await;
let results: String = (*guard).clone().to_string();
*guard = "consumed".to_string();
results.to_string()
}
Comment on lines +903 to +908
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use mutex here to verify that sync_writes really has an effect and that following calls will not execute inner.


#[cfg(feature = "async")]
#[tokio::test]
async fn test_once_sync_writes_a() {
let a_mutex = tokio::sync::Mutex::new("a".to_string());
let b_mutex = tokio::sync::Mutex::new("b".to_string());
let fut_a = once_sync_writes_a(&a_mutex);
let fut_b = once_sync_writes_a(&b_mutex);
let a = fut_a.await;
let b = fut_b.await;
assert_eq!(a, b);
assert_eq!("a", a);

//check if cache function is executed for a
assert_eq!("consumed", a_mutex.lock().await.to_string());
//check if cache inner is not executed for b (not executed second time)
assert_eq!("b", b_mutex.lock().await.to_string());
}

#[cached(size = 2)]
fn cached_smartstring(s: smartstring::alias::String) -> smartstring::alias::String {
if s == "very stringy" {
Expand Down
Loading