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

pd: fix a deadlock #1768

Merged
merged 3 commits into from Apr 14, 2017
Merged

pd: fix a deadlock #1768

merged 3 commits into from Apr 14, 2017

Conversation

overvenus
Copy link
Member

This PR fix deadlock pitfall. Reading then writing a RwLock in a match block will cause a deadlock.

Demo:

use std::sync::RwLock;

fn main() {
    let mu = RwLock::new(Some(1));
    {
        let clone = foo(&mu.read().unwrap());
        let mut data = mu.write().unwrap();
        *data = Some(2);
        println!("none match: {:?}", clone);
    }

    {
        match foo(&mu.read().unwrap()) {
            Some(_) | None => {
                let mut data = mu.write().unwrap();
                *data = Some(2);
                println!("in match");
            }
        }
    }

    println!("done");
}

fn foo(a: &Option<usize>) -> Option<usize> {
    a.clone()
}

Playground

@siddontang
Copy link
Contributor

can we add a test for it?

For the next write, we use try_write to check the lock is still locked.

@hhkbp2
Copy link
Contributor

hhkbp2 commented Apr 13, 2017

LGTM
This issue looks like a compiler impl bug to me.

@BusyJay
Copy link
Member

BusyJay commented Apr 13, 2017

I think compiler will translate it to

let read_lock = lock.read().unwrap();
match func(read_lock) {
    ...
}
...
drop(read_lock)

.

src/util/mod.rs Outdated
@@ -553,4 +553,30 @@ mod tests {
defer!(assert!(!sp.load(Ordering::SeqCst)));
should_panic.store(false, Ordering::SeqCst);
}

#[test]
fn test_rwlock() {
Copy link
Contributor

Choose a reason for hiding this comment

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

test_rwlock_deadlock

@siddontang
Copy link
Contributor

LGTM

@siddontang
Copy link
Contributor

PTAL @BusyJay

@BusyJay
Copy link
Member

BusyJay commented Apr 14, 2017

LGTM

@BusyJay
Copy link
Member

BusyJay commented Apr 14, 2017

Though I don't think the test case is related to the bug.

@siddontang siddontang merged commit bcb7745 into tikv:master Apr 14, 2017
@overvenus overvenus deleted the match-dead-lock branch May 12, 2017 08:03
@overvenus
Copy link
Member Author

FYI, may be solved in RFC#2094(non-lexical lifetimes)

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

Successfully merging this pull request may close these issues.

None yet

4 participants