Skip to content

while let doesn't work as expected with mutex #78390

Closed
@hicqu

Description

@hicqu

I tried this code:

use std::sync::Mutex;

fn main() {
    let lock = Mutex::new(vec![1; 10]);
    while let Some(x) = lock.lock().unwrap().pop() {
        lock.lock().unwrap().push(x);
        println!("the lock works as expected");
    }
}

I expected to see this happen: It can print the message, which means lock it again successfully

Instead, this happened: Nothing is print, which means dead lock

Meta

rustc --version --verbose:

rustc 1.49.0-nightly (b1496c6e6 2020-10-18)
binary: rustc
commit-hash: b1496c6e606dd908dd651ac2cce89815e10d7fc5
commit-date: 2020-10-18
host: x86_64-unknown-linux-gnu
release: 1.49.0-nightly
LLVM version: 11.0
Backtrace

[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
0x00007f3cac6e954d in __lll_lock_wait () from /lib64/libpthread.so.0

Thread 1 (Thread 0x7f3cacf2c800 (LWP 6625)):
#0  0x00007f3cac6e954d in __lll_lock_wait () from /lib64/libpthread.so.0
#1  0x00007f3cac6e4e9b in _L_lock_883 () from /lib64/libpthread.so.0
#2  0x00007f3cac6e4d68 in pthread_mutex_lock () from /lib64/libpthread.so.0
#3  0x0000561e98eb4457 in rw_dead_test::main::hf73ccab8a36ff1e0 ()
#4  0x0000561e98eb4743 in std::sys_common::backtrace::__rust_begin_short_backtrace::h2355815e7a8047a2 ()
#5  0x0000561e98eb4759 in std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::h5b53088bf8366de1 ()
#6  0x0000561e98ec9d87 in call_once<(),Fn<()>> () at /rustc/b1496c6e606dd908dd651ac2cce89815e10d7fc5/library/core/src/ops/function.rs:259
#7  do_call<&Fn<()>,i32> () at library/std/src/panicking.rs:381
#8  try<i32,&Fn<()>> () at library/std/src/panicking.rs:345
#9  catch_unwind<&Fn<()>,i32> () at library/std/src/panic.rs:382
#10 std::rt::lang_start_internal::h2c157bf657c6892a () at library/std/src/rt.rs:51
#11 0x0000561e98eb4732 in main ()
[Inferior 1 (process 6625) detached]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions