Skip to content

ReentrantLockGuard's Sync impl is unsound #125526

Closed
@programmerjake

Description

@programmerjake

I tried running this code in miri:

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=4b45c8a022255be4404228c01922b632

#![feature(reentrant_lock)]
use std::cell::Cell;
use std::sync::ReentrantLock;
use std::thread::scope;

fn main() {
    let l = ReentrantLock::new(Cell::new(0u8));
    let lg = l.lock();
    scope(|s| {
        s.spawn(|| dbg!(lg.get()));
        lg.set(1);
    });
}

I expected to see this happen: compile error

Instead, this happened: compiled successfully and miri reported a data race

this is because the automatic impl<T: Send> Sync for ReentrantLockGuard<T> is incorrect:
https://doc.rust-lang.org/1.78.0/std/sync/struct.ReentrantLockGuard.html#impl-Sync-for-ReentrantLockGuard%3C'a,+T%3E

ReentrantLock's tracking issue: #121440

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessT-libsRelevant to the library team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions