<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/104892>104892</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[libc++] ThreadSanitizer report data race when future & promise destroy in different threads after insert a harmless sleep
</td>
</tr>
<tr>
<th>Labels</th>
<td>
libc++
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
oathdruid
</td>
</tr>
</table>
<pre>
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`
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUWEtv6zYW_jX0hohBkXouvHDi8aCLFoP2DopZCRR5FLOhRQ1JOUl__YCU5VeUTNpJMe2F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