Skip to content

Poisoned Mutex at runtime with nightly #15851

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

Closed
daschl opened this issue Jul 21, 2014 · 3 comments
Closed

Poisoned Mutex at runtime with nightly #15851

daschl opened this issue Jul 21, 2014 · 3 comments

Comments

@daschl
Copy link

daschl commented Jul 21, 2014

Hi folks,

I'm not sure if I messed up here, but since this looks suspicious at runtime and works sometime I thought I gotta raise it here.

The code is as follows:

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

fn main() {
    let num_threads = 4;

    let vec_arc = Arc::new(Mutex::new(Vec::with_capacity(num_threads)));

    for i in range(0u, num_threads) {
        let mutex = vec_arc.clone();

        spawn(proc() {
            let mut vec = mutex.lock();
            vec.insert(i, i);
            println!("vec is now {}", *vec);
        })
    }
}

When I run it on the command line, most of the time it works correctly:

vec is now [0]
vec is now [0, 1]
vec is now [0, 1, 2]
vec is now [0, 1, 2, 3]

But sometimes this comes up:

task '<unnamed>' failed at 'assertion failed: index <= len', /Users/rustbuild/src/rust-buildbot/slave/nightly-mac/build/src/libcollections/vec.rs:1000
task '<unnamed>' failed at 'Poisoned Mutex - another task failed inside!', /Users/rustbuild/src/rust-buildbot/slave/nightly-mac/build/src/libsync/lock.rs:46
task '<unnamed>' failed at 'Poisoned Mutex - another task failed inside!', /Users/rustbuild/src/rust-buildbot/slave/nightly-mac/build/src/libsync/lock.rs:46
task '<unnamed>' failed at 'Poisoned Mutex - another task failed inside!', /Users/rustbuild/src/rust-buildbot/slave/nightly-mac/build/src/libsync/lock.rs:46

Is this expected to happen? I'm on rustc 0.12.0-pre-nightly (f15d6d2 2014-07-20 22:46:29 +0000)

@chris-morgan
Copy link
Member

This is a bug in your own code; Vec.insert(&mut self, index: uint, element: T) is defined as something that will fail “if index is not between 0 and the vector's length (both bounds inclusive).” This means that your code will only run successfully if the four tasks are run in sequence; if they are not, the insert call fails and the mutex is poisoned—once the task has failed, the mutex’s integrity cannot be guaranteed.

Everything is behaving as specified.

@daschl
Copy link
Author

daschl commented Jul 21, 2014

@chris-morgan thanks much :)

@daschl daschl closed this as completed Jul 21, 2014
@chris-morgan
Copy link
Member

It works if the four tasks are run in sequence, because that boils down to being equivalent to this code:

fn main() {
    let mut vec = Vec::with_capacity(4);
    vec.insert(0, 0u);
    vec.insert(1, 1);
    vec.insert(2, 2);
    vec.insert(3, 3);
    println!("{}", vec);
}

[0, 1, 2, 3]

(Remember that Vec::with_capacity is just setting the capacity of the vector, not inserting any items—it’s an efficiency measure only.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants