Skip to content

Deadlock caused by lock not being released in this case. #143224

Closed
@623637646

Description

@623637646

I tried this code:

use std::sync::{Arc, Mutex};

fn main() {
    let lock = Arc::new(Mutex::new(Some(S)));
    let lock_cloned = lock.clone();

    {
        // This code works fine
        // let mut lock = lock.lock().unwrap();
        // lock.take()

        // This code deadlocks
        lock.lock().unwrap().take()
    }
    .unwrap()
    .foo(lock_cloned);

    println!("end")
}

struct S;

impl S {
    fn foo(self, lock: Arc<Mutex<Option<S>>>) {
        let _l = lock.lock().unwrap();
    }
}

I expected to see this happen: print end and the finish

Instead, this happened: deadlock

Meta

rustc --version --verbose:

rustc 1.88.0-nightly (934880f58 2025-04-09)
binary: rustc
commit-hash: 934880f586f6ac1f952c7090e2a943fcd7775e7b
commit-date: 2025-04-09
host: aarch64-apple-darwin
release: 1.88.0-nightly
LLVM version: 20.1.2
Backtrace

<backtrace>

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-discussionCategory: Discussion or questions that doesn't represent real issues.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions