MengRao / fmtlog

fmtlog is a performant fmtlib-style logging library with latency in nanoseconds.
MIT License
807 stars 124 forks source link

Threading issue with startPollingThread (sanitizer report) #24

Closed matthijs closed 2 years ago

matthijs commented 2 years ago

Hello,

The following program (minimal reproducible example) is causing ThreadSanitizer warnings:

#include "fmtlog/fmtlog.h"

int main(int argc, char** argv)
{
  fmtlog::startPollingThread();
  fmtlog::setLogLevel(fmtlog::DBG);
  logd("test: reloading");
  fmtlog::stopPollingThread();

  return 0;
}

Compiled with the following line:

clang++-12 -DFMTLOG_HEADER_ONLY -DFMT_HEADER_ONLY -stdlib=libc++ -fno-omit-frame-pointer -fsanitize=thread -g -std=c++20 -o main main.cpp

Output:

% ./main
==================
WARNING: ThreadSanitizer: data race (pid=21198)
  Read of size 1 at 0x00000100d370 by thread T1:
    #0 fmtlogDetailT<0>::startPollingThread(long)::'lambda'()::operator()() const /home/matthijs/t5/./fmtlog/fmtlog-inl.h:331:14 (main+0x5146b9)
    #1 decltype(std::__1::forward<0>(fp)(std::__1::forward<fmtlogDetailT<0>::startPollingThread(long)::'lambda'()>(fp0)...)) std::__1::__invoke<fmtlogDetailT<0>::startPollingThread(long)::'lambda'()>(0&&, fmtlogDetailT<0>::startPollingThread(long)::'lambda'()&&...) /usr/lib/llvm-12/bin/../include/c++/v1/type_traits:3694:1 (main+0x5145cd)
    #2 void std::__1::__thread_execute<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct> >, fmtlogDetailT<0>::startPollingThread(long)::'lambda'()>(std::__1::tuple<0, fmtlogDetailT<0>::startPollingThread(long)::'lambda'()>&, std::__1::__tuple_indices<>) /usr/lib/llvm-12/bin/../include/c++/v1/thread:280:5 (main+0x5144e5)
    #3 void* std::__1::__thread_proxy<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct> >, fmtlogDetailT<0>::startPollingThread(long)::'lambda'()> >(void*) /usr/lib/llvm-12/bin/../include/c++/v1/thread:291:5 (main+0x513a35)

  Previous write of size 1 at 0x00000100d370 by main thread:
    #0 fmtlogDetailT<0>::stopPollingThread() /home/matthijs/t5/./fmtlog/fmtlog-inl.h:345:19 (main+0x4c23e7)
    #1 fmtlogT<0>::stopPollingThread() /home/matthijs/t5/./fmtlog/fmtlog-inl.h:607:31 (main+0x4c2376)
    #2 main /home/matthijs/t5/main.cpp:8:2 (main+0x4bf462)

  Location is global 'fmtlogDetailWrapper<0>::impl' of size 10496 at 0x00000100aa80 (main+0x00000100d370)

  Thread T1 (tid=21200, running) created by main thread at:
    #0 pthread_create <null> (main+0x44d02b)
    #1 std::__1::__libcpp_thread_create(unsigned long*, void* (*)(void*), void*) /usr/lib/llvm-12/bin/../include/c++/v1/__threading_support:509:10 (main+0x5139a9)
    #2 std::__1::thread::thread<fmtlogDetailT<0>::startPollingThread(long)::'lambda'(), void>(0&&, fmtlogDetailT<0>::startPollingThread(long)::'lambda'()&&...) /usr/lib/llvm-12/bin/../include/c++/v1/thread:307:16 (main+0x513581)
    #3 fmtlogDetailT<0>::startPollingThread(long) /home/matthijs/t5/./fmtlog/fmtlog-inl.h:330:11 (main+0x4c231e)
    #4 fmtlogT<0>::startPollingThread(long) /home/matthijs/t5/./fmtlog/fmtlog-inl.h:602:31 (main+0x4c228a)
    #5 main /home/matthijs/t5/main.cpp:5:2 (main+0x4bf456)

SUMMARY: ThreadSanitizer: data race /home/matthijs/t5/./fmtlog/fmtlog-inl.h:331:14 in fmtlogDetailT<0>::startPollingThread(long)::'lambda'()::operator()() const
==================
ThreadSanitizer: reported 1 warnings

Is this something to worry about?

Regards, Matthijs

MengRao commented 2 years ago

The reported data race should be on volatile bool threadRunning, this variable is used to notify the polling thread to exit so it's expected behavior, you can ignore the warning. Thanks.