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

RwLockReadGuard and RwLockWriteGuard should only be Sync when RawRwLock is #259

Closed
ammaraskar opened this issue Nov 8, 2020 · 3 comments · Fixed by #262
Closed

RwLockReadGuard and RwLockWriteGuard should only be Sync when RawRwLock is #259

ammaraskar opened this issue Nov 8, 2020 · 3 comments · Fixed by #262

Comments

@ammaraskar
Copy link

Currently RwLockReadGuards are marked as Sync so long as the contained type is, without having the same bound on the RawRwLock type:

unsafe impl<'a, R: RawRwLock + 'a, T: ?Sized + Sync + 'a> Sync for RwLockReadGuard<'a, R, T> {}

This means that even if an implementer marks their guard as GuardNoSend, it's actually possible to send guard objects across threads using references to them (references are marked as Send so long as the underlying type is Sync).

Much like MappedMutexGuard this should probably only be Sync when the raw lock is.

An example of sending parking_lot's non sendable guard objects across threads:

use parking_lot::RwLock;
use crossbeam_utils::thread;

fn is_send<T: Send>(_: &T) {}

fn main() {
    let lock = RwLock::new(42);
    let read_guard = lock.read();
    // RwLockReadGuard itself is not Send-able, uncommenting
    // the line below causes a compilation failure.
    //is_send(&read_guard);
    
    thread::scope(|s| {
        let guard_ref = &read_guard;
        
        s.spawn(move |_| {
            // But they can be sent as a reference.
            let sent_lock_guard = guard_ref;
            println!("In thread: {:p}", sent_lock_guard);
        });
        println!("In main:   {:p}", &read_guard);
    });
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=625dd71fdcd34658f0c2d2752a86ca0e

(Issue found by @sslab-gatech's Rust group)

@Amanieu
Copy link
Owner

Amanieu commented Nov 8, 2020

I believe this is not a problem since the only thing you can do with a &RwLockReadGuard is dereference it to access the underlying data. There is no interaction with the RawRwLock itself so it should still be sound.

@ammaraskar
Copy link
Author

ammaraskar commented Nov 8, 2020

Oh right, the problem is the:

pub fn rwlock(s: &Self) -> &'a RwLock<R, T>

method which would allow you to get a reference to the underlying RwLock object, which might not itself be safe across threads.

@Amanieu
Copy link
Owner

Amanieu commented Nov 8, 2020

Ah good point, I forgot about that one. I will adjust the trait bounds.

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 a pull request may close this issue.

2 participants