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

Data race in dropping something around oneshot channel #3620

Closed
etam opened this issue Mar 19, 2021 · 4 comments
Closed

Data race in dropping something around oneshot channel #3620

etam opened this issue Mar 19, 2021 · 4 comments
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-sync Module: tokio/sync

Comments

@etam
Copy link

etam commented Mar 19, 2021

Version
1.3.0

Platform
Linux etam-librem.lan 5.11.4-1-default #1 SMP Mon Mar 8 05:16:55 UTC 2021 (be77cd2) x86_64 x86_64 x86_64 GNU/Linux

Description
ThreadSanitizer shows data races when using oneshot::channel.

Example code (taken from example):

use tokio::sync::oneshot;

async fn some_computation() -> String {
    "represents the result of the computation".to_string()
}

#[tokio::main]
async fn main() {
    let (tx, rx) = oneshot::channel();

    tokio::spawn(async move {
        let res = some_computation().await;
        tx.send(res).unwrap();
    });

    // Do other work while the computation is happening in the background

    // Wait for the computation result
    let res = rx.await.unwrap();
    println!("{:?}", res);
}

compiled and run with this command:

RUST_LOG=trace RUSTFLAGS="-Z sanitizer=thread" cargo run --target x86_64-unknown-linux-gnu

using rust rustc 1.52.0-nightly (107896c32 2021-03-15)

gave this output: tsan.txt.gz (it's too big to paste here)

Interesting places pointed in backtraces:
https://github.com/tokio-rs/tokio/blob/tokio-1.3.0/tokio/src/sync/oneshot.rs#L702
https://github.com/tokio-rs/tokio/blob/tokio-1.3.0/tokio/src/sync/oneshot.rs#L324
https://github.com/tokio-rs/tokio/blob/tokio-1.3.0/tokio/src/runtime/blocking/pool.rs#L259-L260

@etam etam added A-tokio Area: The main tokio crate C-bug Category: This is a bug. labels Mar 19, 2021
@Darksonn Darksonn added the M-sync Module: tokio/sync label Mar 19, 2021
@Darksonn
Copy link
Contributor

I run into rust#83045 when I try to compile it with both the latest nightly and the one you specified.

@carllerche
Copy link
Member

I am pretty confident this is due to the thread santizer not understanding Arc's memory barriers. It's also a reason we wrote loom to verify Tokio's concurrency primitives.

@carllerche
Copy link
Member

I.e. you can get thread-santizer to complain w/ an Arc being dropped from multiple threads.

eg. rust-lang/rust#39608 (comment)

@tmiasko
Copy link
Contributor

tmiasko commented Mar 21, 2021

The Arc is fully compatible with ThreadSanitizer when rebuilding standard library with env RUSTFLAGS=-Zsanitizer=thread cargo -Zbuild-std.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-sync Module: tokio/sync
Projects
None yet
Development

No branches or pull requests

4 participants