paulfloyd / freebsd_valgrind

Git repo used to Upstream the FreeBSD Port of Valgrind
GNU General Public License v2.0
15 stars 4 forks source link

DRD issue? #176

Closed paulfloyd closed 2 years ago

paulfloyd commented 2 years ago

Testcase

#include <iostream>
#include <atomic>
#include <condition_variable>
#include <thread>
#include <chrono>
using namespace std::chrono_literals;

std::condition_variable cv;
std::mutex cv_m;
std::atomic<int> i{0};

void waits(int idx)
{
    std::unique_lock<std::mutex> lk(cv_m);
    auto now = std::chrono::system_clock::now();
    if(cv.wait_until(lk, now + idx*100ms, [](){return i == 1;}))
        std::cerr << "Thread " << idx << " finished waiting. i == " << i << '\n';
    else
        std::cerr << "Thread " << idx << " timed out. i == " << i << '\n';
}

void signals()
{
    std::this_thread::sleep_for(120ms);
    std::cerr << "Notifying...\n";
    cv.notify_all();
    std::this_thread::sleep_for(100ms);
    i = 1;
    std::cerr << "Notifying again...\n";
    cv.notify_all();
}

int main()
{
    std::thread t1(waits, 2), t4(signals);
    t1.join();
    t4.join();
}

../../vg-in-place --tool=drd --trace-mutex=yes --trace-cond=yes ./um

==50371== [2] mutex_trylock error checking mutex 0x7fc000138 rc 0 owner 0 ==50371== [2] post_mutex_lock error checking mutex 0x7fc000138 rc 0 owner 0 ==50371== [2] cond_signal cond 0x7fc000140 ==50371== [2] mutex_unlock error checking mutex 0x7fc000138 rc 1 ==50371== [2] mutex_trylock mutex 0x20a3c8 rc 0 owner 0 ==50371== [2] post_mutex_lock mutex 0x20a3c8 rc 0 owner 0 ==50371== [2] mutex_unlock mutex 0x20a3c8 rc 1 ==50371== [2] cond_pre_wait cond 0x20a3c0 ==50371== [1] mutex_trylock error checking mutex 0x7fc000138 rc 0 owner 2 ==50371== [1] post_mutex_lock error checking mutex 0x7fc000138 rc 0 owner 2 ==50371== [1] mutex_unlock error checking mutex 0x7fc000138 rc 1 ==50371== [1] mutex_destroy error checking mutex 0x7fc000138 rc 0 owner 1 ==50371== [1] cond_destroy cond 0x7fc000140 ==50371== [1] mutex_init error checking mutex 0x7fc000148 ==50371== [1] mutex_ignore_ordering error checking mutex 0x7fc000148 ==50371== [1] cond_init cond 0x7fc000150 ==50371== [3] mutex_trylock error checking mutex 0x7fc000148 rc 0 owner 0 ==50371== [3] post_mutex_lock error checking mutex 0x7fc000148 rc 0 owner 0 ==50371== [3] cond_signal cond 0x7fc000150 ==50371== [3] mutex_unlock error checking mutex 0x7fc000148 rc 1 ==50371== [1] mutex_trylock error checking mutex 0x7fc000148 rc 0 owner 3 ==50371== [1] post_mutex_lock error checking mutex 0x7fc000148 rc 0 owner 3 ==50371== [1] mutex_unlock error checking mutex 0x7fc000148 rc 1 ==50371== [1] mutex_destroy error checking mutex 0x7fc000148 rc 0 owner 1 ==50371== [1] cond_destroy cond 0x7fc000150 ==50371== [3] mutex_trylock mutex 0x4bb9650 rc 0 owner 0 ==50371== [3] post_mutex_lock mutex 0x4bb9650 rc 0 owner 0 ==50371== [3] mutex_unlock mutex 0x4bb9650 rc 1 ==50371== [3] mutex_trylock mutex 0x4bb9650 rc 0 owner 3 ==50371== [3] post_mutex_lock mutex 0x4bb9650 rc 0 owner 3 ==50371== [3] mutex_unlock mutex 0x4bb9650 rc 1 ==50371== [3] mutex_trylock mutex 0x4bb9788 rc 0 owner 0 ==50371== [3] post_mutex_lock mutex 0x4bb9788 rc 0 owner 0 Notifying... ==50371== [3] mutex_unlock mutex 0x4bb9788 rc 1 ==50371== [3] mutex_trylock mutex 0x4bb9788 rc 0 owner 3 ==50371== [3] post_mutex_lock mutex 0x4bb9788 rc 0 owner 3 ==50371== [3] mutex_unlock mutex 0x4bb9788 rc 1 ==50371== [3] mutex_trylock mutex 0x4bb9788 rc 0 owner 3 ==50371== [3] post_mutex_lock mutex 0x4bb9788 rc 0 owner 3 ==50371== [3] mutex_unlock mutex 0x4bb9788 rc 1 ==50371== [3] cond_broadcast cond 0x20a3c0 ==50371== Thread 3: ==50371== Probably a race condition: condition variable 0x20a3c0 has been signaled but the associated mutex 0x20a3c8 is not locked by the signalling thread. ==50371== at 0x485BAD0: pthread_cond_broadcast_intercept (drd_pthread_intercepts.c:1257) ==50371== by 0x485BAD0: pthread_cond_broadcast (drd_pthread_intercepts.c:1270) ==50371== by 0x48CFB18: std::1::condition_variable::notify_all() (in /usr/lib/libc++.so.1) ==50371== by 0x204D7A: signals() (um.cpp:26) ==50371== by 0x207BE6: decltype ((std::1::forward<void ()()>({parm#1}))()) std::1::invoke<void ()()>(void (&&)()) (include/c++/v1/type_traits:3899) ==50371== by 0x207B94: void std::1::thread_execute<std::1::unique_ptr<std::1::thread_struct, std::1::default_delete >, void ()()>(std::1::tuple<std::1::unique_ptr<std::1::thread_struct, std::1::default_delete >, void (*)()>&, std::1::tuple_indices<>) (include/c++/v1/thread:280) ==50371== by 0x2078F5: void* std::1::thread_proxy<std::1::tuple<std::1::unique_ptr<std::1::thread_struct, std::1::default_delete >, void ()()> >(void) (include/c++/v1/thread:291) ==50371== by 0x48609F8: vgDrd_thread_wrapper (drd_pthread_intercepts.c:491) ==50371== by 0x49BA82A: ??? (in /lib/libthr.so.3) ==50371== cond 0x20a3c0 was first observed at: ==50371== at 0x485AC55: pthread_cond_timedwait_intercept (drd_pthread_intercepts.c:1175) ==50371== by 0x485AC55: pthread_cond_timedwait (drd_pthread_intercepts.c:1183) ==50371== by 0x48CFBE9: std::1::condition_variable::do_timed_wait(std::1::unique_lock<std::1::mutex>&, std::1::chrono::time_point<std::__1::chrono::system_clock, std::1::chrono::duration<long long, std::1::ratio<1l, 1000000000l> > >) (in /usr/lib/libc++.so.1) ==50371== by 0x20584A: std::1::cv_status std::1::condition_variable::wait_until<std::1::chrono::system_clock, std::1::chrono::duration<long long, std::1::ratio<1l, 1000000l> > >(std::1::unique_lock&, std::__1::chrono::time_point<std::1::chrono::system_clock, std::1::chrono::duration<long long, std::1::ratio<1l, 1000000l> > > const&) (include/c++/v1/mutex_base:426) ==50371== by 0x204CF4: bool std::1::condition_variable::wait_until<std::1::chrono::system_clock, std::1::chrono::duration<long long, std::1::ratio<1l, 1000000l> >, waits(int)::$_0>(std::__1::unique_lock<std::1::mutex>&, std::1::chrono::time_point<std::__1::chrono::system_clock, std::1::chrono::duration<long long, std::1::ratio<1l, 1000000l> > > const&, waits(int)::$_0) (include/c++/v1/__mutex_base:438) ==50371== by 0x204B4D: waits(int) (um.cpp:16) ==50371== by 0x2074F0: decltype ((std::1::forward<void ()(int)>({parm#1}))(std::1::forward({parm#2}))) std::1::__invoke<void ()(int), int>(void (&&)(int), int&&) (include/c++/v1/type_traits:3899) ==50371== by 0x20743D: void std::1::thread_execute<std::1::unique_ptr<std::1::thread_struct, std::1::default_delete >, void ()(int), int, 2ul>(std::1::tuple<std::1::unique_ptr<std::1::thread_struct, std::1::default_delete >, void (*)(int), int>&, std::1::tuple_indices<2ul>) (include/c++/v1/thread:280) ==50371== by 0x206DD5: void* std::1::thread_proxy<std::1::tuple<std::1::unique_ptr<std::1::thread_struct, std::1::default_delete >, void ()(int), int> >(void) (include/c++/v1/thread:291) ==50371== by 0x48609F8: vgDrd_thread_wrapper (drd_pthread_intercepts.c:491) ==50371== by 0x49BA82A: ??? (in /lib/libthr.so.3) ==50371== mutex 0x20a3c8 was first observed at: ==50371== at 0x485839B: pthread_mutex_lock_intercept (drd_pthread_intercepts.c:932) ==50371== by 0x485839B: pthread_mutex_lock (drd_pthread_intercepts.c:945) ==50371== by 0x491E388: std::1::mutex::lock() (in /usr/lib/libc++.so.1) ==50371== by 0x204EF3: std::1::unique_lock::unique_lock(std::1::mutex&) (include/c++/v1/mutex_base:119) ==50371== by 0x204ADB: waits(int) (um.cpp:14) ==50371== by 0x2074F0: decltype ((std::1::forward<void (*)(int)>({parm#1}))(std::1::forward({parm#2}))) std::1::invoke<void ()(int), int>(void (&&)(int), int&&) (include/c++/v1/type_traits:3899) ==50371== by 0x20743D: void std::1::thread_execute<std::1::unique_ptr<std::1::thread_struct, std::1::default_delete >, void ()(int), int, 2ul>(std::1::tuple<std::1::unique_ptr<std::1::thread_struct, std::__1::default_delete >, void ()(int), int>&, std::1::tuple_indices<2ul>) (include/c++/v1/thread:280) ==50371== by 0x206DD5: void std::1::thread_proxy<std::1::tuple<std::1::unique_ptr<std::1::thread_struct, std::__1::default_delete >, void ()(int), int> >(void*) (include/c++/v1/thread:291) ==50371== by 0x48609F8: vgDrd_thread_wrapper (drd_pthread_intercepts.c:491) ==50371== by 0x49BA82A: ??? (in /lib/libthr.so.3) ==50371== ==50371== [2] cond_post_wait cond 0x20a3c0 ==50371== [2] cond_post_wait mutex 0x20a3c8 rc 0 owner 2 ==50371== [2] mutex_unlock mutex 0x20a3c8 rc 1 ==50371== [2] cond_pre_wait cond 0x20a3c0 ==50371== [2] cond_post_wait cond 0x20a3c0 ==50371== [2] cond_post_wait mutex 0x20a3c8 rc 0 owner 2

paulfloyd commented 2 years ago

Also on Linux, so this is a Valgrind problem.