Skip to content

Instantly share code, notes, and snippets.

@vors
Created June 25, 2018 02:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vors/83d78aa5e4b0cc0f730f54d99eafd692 to your computer and use it in GitHub Desktop.
Save vors/83d78aa5e4b0cc0f730f54d99eafd692 to your computer and use it in GitHub Desktop.
Rust toy example for Arc where tsan reports a data race
use std::sync::Arc;
use std::thread;
struct Foo;
impl Drop for Foo {
fn drop(&mut self) {
println!("dropped!");
}
}
fn main() {
let foo = Arc::new(Foo);
let foo2 = Arc::clone(&foo);
let handler = thread::spawn(|| {
println!("Hello from thread!");
drop(foo); // Is there a race between two drops?
});
println!("Hello from main!");
drop(foo2); // Is there a race between two drops?
handler.join().unwrap();
}
Hello from main!
Hello from thread!
dropped!
==================
WARNING: ThreadSanitizer: data race (pid=7910)
Write of size 8 at 0x7b0400000000 by thread T1:
#0 free /checkout/src/libcompiler_builtins/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc:709 (arc+0x1fbd9)
#1 _$LT$alloc..alloc..Global$u20$as$u20$core..alloc..GlobalAlloc$GT$::dealloc::hacfd3d18013b7995 /checkout/src/liballoc/alloc.rs:86 (arc+0xea3b)
#2 _$LT$alloc..alloc..Global$u20$as$u20$core..alloc..Alloc$GT$::dealloc::h1c5509fcfc9739bd /checkout/src/liballoc/alloc.rs:117 (arc+0xe821)
#3 _$LT$alloc..arc..Arc$LT$T$GT$$GT$::drop_slow::h46f87febfd53023e /checkout/src/liballoc/arc.rs:521 (arc+0xe0b3)
#4 _$LT$alloc..arc..Arc$LT$T$GT$$u20$as$u20$core..ops..drop..Drop$GT$::drop::h5f8d2d3a2f9afc45 /checkout/src/liballoc/arc.rs:969 (arc+0xcb14)
#5 core::ptr::drop_in_place::h8cb02e34599f41df /checkout/src/libcore/ptr.rs:59 (arc+0xca19)
#6 core::mem::drop::hd3bd8c19de48611c /checkout/src/libcore/mem.rs:787 (arc+0xc9de)
#7 arc::main::_$u7b$$u7b$closure$u7d$$u7d$::h8faff09ee6b28d78 /home/vagrant/arc/arc/src/main.rs:18 (arc+0xcb87)
#8 std::sys_common::backtrace::__rust_begin_short_backtrace::hcdb941e607a2a717 /checkout/src/libstd/sys_common/backtrace.rs:136 (arc+0xc8c9)
#9 std::thread::Builder::spawn::_$u7b$$u7b$closure$u7d$$u7d$::_$u7b$$u7b$closure$u7d$$u7d$::hac7af434684f5590 /checkout/src/libstd/thread/mod.rs:409 (arc+0xc769)
#10 _$LT$std..panic..AssertUnwindSafe$LT$F$GT$$u20$as$u20$core..ops..function..FnOnce$LT$$LP$$RP$$GT$$GT$::call_once::haa1d845337c9378c /checkout/src/libstd/panic.rs:296 (arc+0xc6d9)
#11 std::panicking::try::do_call::h06b082ada6e471a4 /checkout/src/libstd/panicking.rs:304 (arc+0xc0d1)
#12 __rust_maybe_catch_panic /checkout/src/libpanic_unwind/lib.rs:105 (arc+0x956fe)
#13 std::panic::catch_unwind::h56e3dd8b06729224 /checkout/src/libstd/panic.rs:361 (arc+0xc71a)
#14 std::thread::Builder::spawn::_$u7b$$u7b$closure$u7d$$u7d$::h8ca3058b671fad4e /checkout/src/libstd/thread/mod.rs:408 (arc+0xa86f)
#15 _$LT$F$u20$as$u20$alloc..boxed..FnBox$LT$A$GT$$GT$::call_box::hb8ac0712f43515ea /checkout/src/liballoc/boxed.rs:638 (arc+0xb87e)
#16 _$LT$alloc..boxed..Box$LT$alloc..boxed..FnBox$LT$A$C$$u20$Output$u3d$R$GT$$u20$$u2b$$u20$$u27$a$GT$$u20$as$u20$core..ops..function..FnOnce$LT$A$GT$$GT$::call_once::h39f938fdb6b37033 /checkout/src/liballoc/boxed.rs:648 (arc+0x8add7)
#17 std::sys_common::thread::start_thread::h51b09df476d0a15d /checkout/src/libstd/sys_common/thread.rs:24 (arc+0x8add7)
Previous atomic write of size 8 at 0x7b0400000000 by main thread:
#0 __tsan_atomic64_fetch_sub /checkout/src/libcompiler_builtins/compiler-rt/lib/tsan/rtl/tsan_interface_atomic.cc:648 (discriminator 1) (arc+0x54211)
#1 core::sync::atomic::atomic_sub::h408c9bb93bc62def /checkout/src/libcore/sync/atomic.rs:1745 (arc+0xd993)
#2 core::sync::atomic::AtomicUsize::fetch_sub::h8fbaf25edea73943 /checkout/src/libcore/sync/atomic.rs:1322 (arc+0xda31)
#3 _$LT$alloc..arc..Arc$LT$T$GT$$u20$as$u20$core..ops..drop..Drop$GT$::drop::h5f8d2d3a2f9afc45 /checkout/src/liballoc/arc.rs:934 (arc+0xdc21)
#4 core::ptr::drop_in_place::h8cb02e34599f41df /checkout/src/libcore/ptr.rs:59 (arc+0xd819)
#5 core::mem::drop::hd3bd8c19de48611c /checkout/src/libcore/mem.rs:787 (arc+0xd62e)
#6 arc::main::h6c3c17d08e1e03c1 /home/vagrant/arc/arc/src/main.rs:22 (arc+0xddfc)
#7 std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::h8c8fda58f00a4f90 /checkout/src/libstd/rt.rs:74 (arc+0xc55b)
#8 std::rt::lang_start_internal::_$u7b$$u7b$closure$u7d$$u7d$::h8cef3565489c669c /checkout/src/libstd/rt.rs:59 (arc+0x80717)
#9 std::panicking::try::do_call::hb54fdb9400e560a4 /checkout/src/libstd/panicking.rs:304 (arc+0x80717)
#10 main ??:? (arc+0xdeb8)
Thread T1 (tid=7942, running) created by main thread at:
#0 pthread_create /checkout/src/libcompiler_builtins/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc:992 (arc+0x168c6)
#1 std::sys::unix::thread::Thread::new::hc1db8826f0aca1d4 /checkout/src/libstd/sys/unix/thread.rs:78 (arc+0x80dd2)
#2 std::thread::spawn::hcc2453d5e23055c0 /checkout/src/libstd/thread/mod.rs:539 (arc+0xa4e9)
#3 arc::main::h6c3c17d08e1e03c1 /home/vagrant/arc/arc/src/main.rs:16 (arc+0xddc9)
#4 std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::h8c8fda58f00a4f90 /checkout/src/libstd/rt.rs:74 (arc+0xc55b)
#5 std::rt::lang_start_internal::_$u7b$$u7b$closure$u7d$$u7d$::h8cef3565489c669c /checkout/src/libstd/rt.rs:59 (arc+0x80717)
#6 std::panicking::try::do_call::hb54fdb9400e560a4 /checkout/src/libstd/panicking.rs:304 (arc+0x80717)
#7 main ??:? (arc+0xdeb8)
SUMMARY: ThreadSanitizer: data race /checkout/src/liballoc/alloc.rs:86 in _$LT$alloc..alloc..Global$u20$as$u20$core..alloc..GlobalAlloc$GT$::dealloc::hacfd3d18013b7995
==================
ThreadSanitizer: reported 1 warnings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment