Issue |
104892
|
Summary |
[libc++] ThreadSanitizer report data race when future & promise destroy in different threads after insert a harmless sleep
|
Labels |
libc++
|
Assignees |
|
Reporter |
oathdruid
|
source:
```c++
#include <future>
#include <thread>
int main() {
std::promise<void> finish_promise;
std::future<void> finish_future = finish_promise.get_future();
std::promise<std::promise<void>> task_promise;
std::future<std::promise<void>> task_future = task_promise.get_future();
std::thread worker([&] {
std::promise<void> task = task_future.get();
task.set_value();
////////////////////////////////////////
// ThreadSanitizer will happy if this line is removed
std::this_thread::sleep_for(std::chrono::microseconds(1));
////////////////////////////////////////
});
task_promise.set_value(std::move(finish_promise));
finish_future.get();
worker.join();
return 0;
}
```
compile command: `clang++-14 -stdlib=libc++ -fsanitize=thread a.cc`
execution result:
```
==================
WARNING: ThreadSanitizer: data race (pid=169934)
Write of size 8 at 0x7b2000000018 by thread T1:
#0 operator delete(void*) <null> (a.out+0xcfc2e) (BuildId: d8e5e885fdc3ca955ed9ff706f21493b2f89f363)
#1 std::__1::__shared_count::__release_shared() <null> (libc++.so.1+0x4aae8) (BuildId: 7a7941120da0f2706a412c817aff5d2cc0cdb51b)
#2 decltype(static_cast<main::$_0>(fp)()) std::__1::__invoke<main::$_0>(main::$_0&&) a.cc (a.out+0xd0e45) (BuildId: d8e5e885fdc3ca955ed9ff706f21493b2f89f363)
#3 void std::__1::__thread_execute<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct> >, main::$_0>(std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct> >, main::$_0>&, std::__1::__tuple_indices<>) a.cc (a.out+0xd0df5) (BuildId: d8e5e885fdc3ca955ed9ff706f21493b2f89f363)
#4 void* std::__1::__thread_proxy<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct> >, main::$_0> >(void*) a.cc (a.out+0xd0a22) (BuildId: d8e5e885fdc3ca955ed9ff706f21493b2f89f363)
Previous atomic read of size 1 at 0x7b2000000018 by main thread:
#0 pthread_cond_wait <null> (a.out+0x5016c) (BuildId: d8e5e885fdc3ca955ed9ff706f21493b2f89f363)
#1 std::__1::condition_variable::wait(std::__1::unique_lock<std::__1::mutex>&) <null> (libc++.so.1+0x48b2e) (BuildId: 7a7941120da0f2706a412c817aff5d2cc0cdb51b)
#2 __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16 (libc.so.6+0x29d8f) (BuildId: 490fef8403240c91833978d494d39e537409b92e)
Thread T1 (tid=169936, running) created by main thread at:
#0 pthread_create <null> (a.out+0x4f5dd) (BuildId: d8e5e885fdc3ca955ed9ff706f21493b2f89f363)
#1 std::__1::__libcpp_thread_create(unsigned long*, void* (*)(void*), void*) <null> (a.out+0xd23d9) (BuildId: d8e5e885fdc3ca955ed9ff706f21493b2f89f363)
#2 std::__1::thread::thread<main::$_0, void>(main::$_0&&) a.cc (a.out+0xd07de) (BuildId: d8e5e885fdc3ca955ed9ff706f21493b2f89f363)
#3 main <null> (a.out+0xd05cc) (BuildId: d8e5e885fdc3ca955ed9ff706f21493b2f89f363)
SUMMARY: ThreadSanitizer: data race (/home/oathdruid/a.out+0xcfc2e) (BuildId: d8e5e885fdc3ca955ed9ff706f21493b2f89f363) in operator delete(void*)
==================
ThreadSanitizer: reported 1 warnings
```
But works fine if compile with:
- `clang++-14 -stdlib=libstdc++ -fsanitize=thread a.cc`
- `g++-12 -fsanitize=thread a.cc`
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs