Skip to content

data race with example_hyper #98

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
bbigras opened this issue Feb 20, 2017 · 6 comments
Closed

data race with example_hyper #98

bbigras opened this issue Feb 20, 2017 · 6 comments
Assignees
Labels

Comments

@bbigras
Copy link

bbigras commented Feb 20, 2017

To reproduce, run example_hyper with RUSTFLAGS="-Z sanitizer=thread" cargo run on a recent nightly. Then run curl localhost:9898/metrics twice.

The sanitizer is a new cool feature. See [HowTo] Sanitize your Rust code!

listening addr "127.0.0.1:9898"
==================
WARNING: ThreadSanitizer: data race (pid=18085)
  Read of size 8 at 0x55e64c39ac08 by thread T3:
    #0 lazy_static::lazy::{{impl}}::get<prometheus::counter::Counter,fn() -> prometheus::counter::Counter> /home/bbigras/.cargo/registry/src/github.heygears.com-1ecc6299db9ec823/lazy_static-0.2.2/src/lazy.rs:19 (prometheus-race+0x00000010a744)
    #1 prometheus_race::{{impl}}::deref::__stability /home/bbigras/dev/rust/bugs/prometheus-race/<lazy_static macros>:21 (prometheus-race+0x00000010a744)
    #2 prometheus_race::{{impl}}::deref /home/bbigras/dev/rust/bugs/prometheus-race/<lazy_static macros>:22 (prometheus-race+0x00000010a744)
    #3 prometheus_race::main::{{closure}} /home/bbigras/dev/rust/bugs/prometheus-race/src/main.rs:44 (prometheus-race+0x00000010a212)
    #4 prometheus_race::main::{{closure}} /home/bbigras/dev/rust/bugs/prometheus-race/src/main.rs:44 (prometheus-race+0x00000010a212)
    #5 prometheus_race::main::{{closure}} /home/bbigras/dev/rust/bugs/prometheus-race/src/main.rs:44 (prometheus-race+0x00000010a212)
    #6 hyper::server::{{impl}}::handle<closure> /home/bbigras/.cargo/registry/src/github.heygears.com-1ecc6299db9ec823/hyper-0.10.4/src/server/mod.rs:434 (prometheus-race+0x0000000956a0)
    #7 hyper::server::{{impl}}::keep_alive_loop<closure,std::io::buffered::BufWriter<&mut hyper::net::HttpStream>> /home/bbigras/.cargo/registry/src/github.heygears.com-1ecc6299db9ec823/hyper-0.10.4/src/server/mod.rs:341 (prometheus-race+0x00000007c3a5)
    #8 hyper::server::{{impl}}::handle_connection<closure,hyper::net::HttpStream> /home/bbigras/.cargo/registry/src/github.heygears.com-1ecc6299db9ec823/hyper-0.10.4/src/server/mod.rs:287 (prometheus-race+0x00000007d25e)
    #9 hyper::server::handle::{{closure}}<closure,hyper::net::HttpListener> /home/bbigras/.cargo/registry/src/github.heygears.com-1ecc6299db9ec823/hyper-0.10.4/src/server/mod.rs:246 (prometheus-race+0x0000000d10cf)
    #10 hyper::server::listener::spawn_with::{{closure}}<hyper::net::HttpListener,closure> /home/bbigras/.cargo/registry/src/github.heygears.com-1ecc6299db9ec823/hyper-0.10.4/src/server/listener.rs:50 (prometheus-race+0x0000000d1814)
    #11 std::panic::{{impl}}::call_once<(),closure> /checkout/src/libstd/panic.rs:296 (prometheus-race+0x000000103fb8)
    #12 std::panicking::try::do_call<std::panic::AssertUnwindSafe<closure>,()> /checkout/src/libstd/panicking.rs:450 (prometheus-race+0x00000008deb5)
    #13 panic_unwind::__rust_maybe_catch_panic /checkout/src/libpanic_unwind/lib.rs:98 (prometheus-race+0x000000584c4a)
    #14 std::panic::catch_unwind<std::panic::AssertUnwindSafe<closure>,()> /checkout/src/libstd/panic.rs:361 (prometheus-race+0x00000008b253)
    #15 std::thread::{{impl}}::spawn::{{closure}}<closure,()> /checkout/src/libstd/thread/mod.rs:357 (prometheus-race+0x00000008d2ca)
    #16 alloc::boxed::{{impl}}::call_box<(),closure> /checkout/src/liballoc/boxed.rs:638 (prometheus-race+0x0000000c16b6)
    #17 alloc::boxed::{{impl}}::call_once<(),()> /checkout/src/liballoc/boxed.rs:648 (prometheus-race+0x00000057cc14)
    #18 std::sys_common::thread::start_thread /checkout/src/libstd/sys_common/thread.rs:21 (prometheus-race+0x00000057cc14)
    #19 std::sys::imp::thread::{{impl}}::new::thread_start /checkout/src/libstd/sys/unix/thread.rs:84 (prometheus-race+0x00000057cc14)

  Previous write of size 8 at 0x55e64c39ac08 by thread T2:
    [failed to restore the stack]

  Location is global '<null>' at 0x000000000000 (prometheus-race+0x0000009b9c08)

  Thread T3 (tid=18279, running) created by thread T1 at:
    #0 pthread_create /checkout/src/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc:902 (prometheus-race+0x0000004903c7)
    #1 std::sys::imp::thread::{{impl}}::new /checkout/src/libstd/sys/unix/thread.rs:72 (prometheus-race+0x00000057c915)
    #2 std::thread::spawn<closure,()> /checkout/src/libstd/thread/mod.rs:412 (prometheus-race+0x00000008b372)
    #3 hyper::server::listener::spawn_with<hyper::net::HttpListener,closure> /home/bbigras/.cargo/registry/src/github.heygears.com-1ecc6299db9ec823/hyper-0.10.4/src/server/listener.rs:45 (prometheus-race+0x0000000d15a8)
    #4 hyper::server::listener::{{impl}}::accept<hyper::net::HttpListener,closure> /home/bbigras/.cargo/registry/src/github.heygears.com-1ecc6299db9ec823/hyper-0.10.4/src/server/listener.rs:31 (prometheus-race+0x0000000c9258)
    #5 hyper::server::handle::{{closure}}<closure,hyper::net::HttpListener> /home/bbigras/.cargo/registry/src/github.heygears.com-1ecc6299db9ec823/hyper-0.10.4/src/server/mod.rs:248 (prometheus-race+0x0000000d1026)
    #6 std::panic::{{impl}}::call_once<(),closure> /checkout/src/libstd/panic.rs:296 (prometheus-race+0x0000001040ab)
    #7 std::panicking::try::do_call<std::panic::AssertUnwindSafe<closure>,()> /checkout/src/libstd/panicking.rs:450 (prometheus-race+0x00000008dccb)
    #8 panic_unwind::__rust_maybe_catch_panic /checkout/src/libpanic_unwind/lib.rs:98 (prometheus-race+0x000000584c4a)
    #9 std::panic::catch_unwind<std::panic::AssertUnwindSafe<closure>,()> /checkout/src/libstd/panic.rs:361 (prometheus-race+0x00000008b153)
    #10 std::thread::{{impl}}::spawn::{{closure}}<closure,()> /checkout/src/libstd/thread/mod.rs:357 (prometheus-race+0x00000008cf4a)
    #11 alloc::boxed::{{impl}}::call_box<(),closure> /checkout/src/liballoc/boxed.rs:638 (prometheus-race+0x0000000c176c)
    #12 alloc::boxed::{{impl}}::call_once<(),()> /checkout/src/liballoc/boxed.rs:648 (prometheus-race+0x00000057cc14)
    #13 std::sys_common::thread::start_thread /checkout/src/libstd/sys_common/thread.rs:21 (prometheus-race+0x00000057cc14)
    #14 std::sys::imp::thread::{{impl}}::new::thread_start /checkout/src/libstd/sys/unix/thread.rs:84 (prometheus-race+0x00000057cc14)

  Thread T2 (tid=18278, running) created by thread T1 at:
    #0 pthread_create /checkout/src/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc:902 (prometheus-race+0x0000004903c7)
    #1 std::sys::imp::thread::{{impl}}::new /checkout/src/libstd/sys/unix/thread.rs:72 (prometheus-race+0x00000057c915)
    #2 std::thread::spawn<closure,()> /checkout/src/libstd/thread/mod.rs:412 (prometheus-race+0x00000008b372)
    #3 hyper::server::listener::spawn_with<hyper::net::HttpListener,closure> /home/bbigras/.cargo/registry/src/github.heygears.com-1ecc6299db9ec823/hyper-0.10.4/src/server/listener.rs:45 (prometheus-race+0x0000000d15a8)
    #4 hyper::server::listener::{{impl}}::accept<hyper::net::HttpListener,closure> /home/bbigras/.cargo/registry/src/github.heygears.com-1ecc6299db9ec823/hyper-0.10.4/src/server/listener.rs:31 (prometheus-race+0x0000000c9258)
    #5 hyper::server::handle::{{closure}}<closure,hyper::net::HttpListener> /home/bbigras/.cargo/registry/src/github.heygears.com-1ecc6299db9ec823/hyper-0.10.4/src/server/mod.rs:248 (prometheus-race+0x0000000d1026)
    #6 std::panic::{{impl}}::call_once<(),closure> /checkout/src/libstd/panic.rs:296 (prometheus-race+0x0000001040ab)
    #7 std::panicking::try::do_call<std::panic::AssertUnwindSafe<closure>,()> /checkout/src/libstd/panicking.rs:450 (prometheus-race+0x00000008dccb)
    #8 panic_unwind::__rust_maybe_catch_panic /checkout/src/libpanic_unwind/lib.rs:98 (prometheus-race+0x000000584c4a)
    #9 std::panic::catch_unwind<std::panic::AssertUnwindSafe<closure>,()> /checkout/src/libstd/panic.rs:361 (prometheus-race+0x00000008b153)
    #10 std::thread::{{impl}}::spawn::{{closure}}<closure,()> /checkout/src/libstd/thread/mod.rs:357 (prometheus-race+0x00000008cf4a)
    #11 alloc::boxed::{{impl}}::call_box<(),closure> /checkout/src/liballoc/boxed.rs:638 (prometheus-race+0x0000000c176c)
    #12 alloc::boxed::{{impl}}::call_once<(),()> /checkout/src/liballoc/boxed.rs:648 (prometheus-race+0x00000057cc14)
    #13 std::sys_common::thread::start_thread /checkout/src/libstd/sys_common/thread.rs:21 (prometheus-race+0x00000057cc14)
    #14 std::sys::imp::thread::{{impl}}::new::thread_start /checkout/src/libstd/sys/unix/thread.rs:84 (prometheus-race+0x00000057cc14)

SUMMARY: ThreadSanitizer: data race /home/bbigras/.cargo/registry/src/github.heygears.com-1ecc6299db9ec823/lazy_static-0.2.2/src/lazy.rs:19 in lazy_static::lazy::{{impl}}::get<prometheus::counter::Counter,fn() -> prometheus::counter::Counter>
==================
[...]
@siddontang
Copy link
Contributor

/cc @overvenus

@bbigras
Copy link
Author

bbigras commented Feb 21, 2017

Be aware that there could be false positives. See https://users.rust-lang.org/t/howto-sanitize-your-rust-code/9378/22. And https://github.com/japaric/rust-san#better-backtraces could be useful too.

@siddontang
Copy link
Contributor

Thanks @BrunoQC

We will fix it later, or could you send us a PR to fix it?

@overvenus
Copy link
Member

Hi @BrunoQC ,

Sorry for the super late reply.

The SUMMARY indicates that the data race happens in crate lazy-static.

        unsafe {
            let r = &mut self.0;
            self.1.call_once(|| {
                *r = Box::into_raw(Box::new(f())); // write
            });

            &*self.0 // read
        }

@parched said that

This will be a false positive because drop_slow uses a fence, and tsan doesn't work with fences.

rust-lang/rust#39608 (comment)


I am not so familiar with atomic and fence, not sure if this is false positive. I will investigate further. 😄

@parched
Copy link

parched commented Mar 9, 2017

Looks like is probably call_once. I had a look at the source though and couldn't see anything obvious, it certainly doesn't use fences that I could see. It would be good to run it with better backtraces (link earlier) to see exactly where it is happening.

@breezewish
Copy link
Member

Looks to be a false positive. Closing.

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

No branches or pull requests

5 participants