libc++abi.dylib: terminating with uncaught exception of type std::__1::system_error: mutex lock failed: Invalid argument

Viewed 6529

I had some code for a thread pool that has been working on my Ubuntu machine for quite some time. The project is a CMake project, and I believe it was using a recent version of g++ as the compiler.

I just tried to run the code on a Mac, CMake tries to use AppleClang 12.0.0.12000032, and I'm hit with

libc++abi.dylib: terminating with uncaught exception of type std::__1::system_error: mutex lock failed: Invalid argument

Is AppleClang missing std::shared_mutex or std::mutex? These are c++17 features. Is there something I need to do to enable c++17 on this Mac? I do mention that it is required in my CMakeLists.txt file.

Here is the full build output:

(base) clare.obrien.brown@Clares-Air build % cmake ..
-- The C compiler identification is AppleClang 12.0.0.12000032
-- The CXX compiler identification is AppleClang 12.0.0.12000032
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE  
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/clare.obrien.brown/ssme/build
(base) clare.obrien.brown@Clares-Air build % make
Scanning dependencies of target ssme_test
[ 16%] Building CXX object test/CMakeFiles/ssme_test.dir/test-main.cpp.o
[ 33%] Building CXX object test/CMakeFiles/ssme_test.dir/test_ada_pmmh_mvn.cpp.o
[ 50%] Building CXX object test/CMakeFiles/ssme_test.dir/test_parameters.cpp.o
In file included from /Users/clare.obrien.brown/ssme/test/test_parameters.cpp:5:
/Users/clare.obrien.brown/ssme/include/ssme/parameters.h:333:58: warning: unused parameter 'trans_p' [-Wunused-parameter]
float_t null_trans<float_t>::log_jacobian(const float_t& trans_p)
                                                         ^
1 warning generated.
[ 66%] Building CXX object test/CMakeFiles/ssme_test.dir/test_thread_pool.cpp.o
/Users/clare.obrien.brown/ssme/test/test_thread_pool.cpp:13:46: warning: unused parameter 'obs_data' [-Wunused-parameter]
    static double d(param_t nums, obs_data_t obs_data) {
                                             ^
/Users/clare.obrien.brown/ssme/test/test_thread_pool.cpp:54:45: warning: unused parameter 'od' [-Wunused-parameter]
                [](param_t nums, obs_data_t od) -> double{
                                            ^
/Users/clare.obrien.brown/ssme/test/test_thread_pool.cpp:82:40: warning: unused parameter 'ydata' [-Wunused-parameter]
    double d(param_t theta, obs_data_t ydata) {
                                       ^
3 warnings generated.
[ 83%] Building CXX object test/CMakeFiles/ssme_test.dir/test_utils.cpp.o
[100%] Linking CXX executable ssme_test
[100%] Built target ssme_test
(base) clare.obrien.brown@Clares-Air build % cd ../test 
(base) clare.obrien.brown@Clares-Air test % ../build/test/ssme_test 
libc++abi.dylib: terminating with uncaught exception of type std::__1::system_error: mutex lock failed: Invalid argument
libc++abi.dylib: terminating with uncaught exception of type std::__1::system_error: mutex lock failed: Invalid argument
zsh: abort      ../build/test/ssme_test
2 Answers

Here is where that exception is being thrown from: https://github.com/llvm/llvm-project/blob/main/libcxx/src/mutex.cpp#L35

This typically happens when .lock() is called on a mutex that is not yet constructed, or has already been destructed. This can happen for mutexes with static storage duration when they are accessed prior to main(), or after main() returns. You'll need to review your start up or shutdown logic to ensure that the lifetime of the mutex exceeds the lifetime of the clients trying to use it.

/Applications/Utilities/Console.app can provide you with a stack trace of the application when the uncaught exception was thrown. This may aid in your debugging efforts. Compiling with -g may provide better debug info for the stack trace.

I had this crash error after some button clicks when my phone is connected to Xcode. After unplugging the phone from the computer, pressing the same buttons didn't cause any crashes.

Related