c++sanitizerthread-sanitizer

Force ThreadSanitizer to crash after finding a problem


I'm running ThreadSanitizer with -02 and g compiler flags and without gdb. It detects the race condition and prints the stacktraces but it continues running. I was fortunate to be monitoring.

Is there a way to force ThreadSanitizer to crash when it detects a problem?


Solution

  • You can use the halt_on_error option:

    $ g++ -o foo foo.cpp -fsanitize=thread
    $ TSAN_OPTIONS=halt_on_error=1 ./foo
    
    Flag name Default value Description
    halt_on_error 0 Exit after first reported error

    If you'd rather have this turned on by default. Add a __tsan_default_options function to the program:

    #ifdef __SANITIZE_THREAD__
    extern "C" const char* __tsan_default_options() {
        return "halt_on_error=1";
    }
    #endif