Lock Dropping Behavior Question

⚓ Rust    📅 2025-11-25    👤 surdeus    👁️ 8      

surdeus

use tokio::sync::Mutex;
use std::sync::Arc;

#[tokio::main]
async fn main() {
    let a = Arc::new(Mutex::new(String::from("Hello")));
    foo(a.clone(), a.lock().await.clone()).await;
}

async fn foo(l: Arc<Mutex<String>>, s: String) {
    let guard = l.lock().await;
    println!("{guard} {s}")
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.75s
     Running `target/debug/playground`

Hello!

When I run this code, it seems to deadlock and no output is emitted. It seems that the lock should be dropped once the inner String is cloned. What am I missing here?

Thanks!

2 posts - 2 participants

Read full topic

🏷️ Rust_feed