Search code examples
c++c++11pthreadsstd-system-error

Undeterministic std::system_error: what(): Operation not permitted


I am trying to run my program and once in a few runs I get an error:

terminate called after throwing an instance of 'std::system_error'
  what():  Operation not permitted

My code is available here: https://github.com/Qabrt/turnstiles

gdb outputs:

Thread 31 "trivial_test" received signal SIGABRT, Aborted.
[Switching to thread 31 (Thread 0x7fff8a7fc700 (LWP 8716))]
#0  0x00007ffff6e7f83b in raise () from /lib64/libc.so.6
(gdb) bt
#0  0x00007ffff6e7f83b in raise () from /lib64/libc.so.6
#1  0x00007ffff6e81081 in abort () from /lib64/libc.so.6
#2  0x00007ffff78670e5 in __gnu_cxx::__verbose_terminate_handler ()
    at /var/tmp/portage/sys-devel/gcc-7.3.0-r3/work/gcc-7.3.0/libstdc++-v3/libsupc++/vterminate.cc:95
#3  0x00007ffff7864cb6 in __cxxabiv1::__terminate (handler=<optimized out>)
    at /var/tmp/portage/sys-devel/gcc-7.3.0-r3/work/gcc-7.3.0/libstdc++-v3/libsupc++/eh_terminate.cc:47
#4  0x00007ffff7864d01 in std::terminate ()
    at /var/tmp/portage/sys-devel/gcc-7.3.0-r3/work/gcc-7.3.0/libstdc++-v3/libsupc++/eh_terminate.cc:57
#5  0x00007ffff789243f in std::execute_native_thread_routine (__p=0x555555790f80)
    at /var/tmp/portage/sys-devel/gcc-7.3.0-r3/work/gcc-7.3.0/libstdc++-v3/src/c++11/thread.cc:91
#6  0x00007ffff7bbd96a in start_thread () from /lib64/libpthread.so.0
#7  0x00007ffff6f4d11f in clone () from /lib64/libc.so.6

g++ --version

g++ (Gentoo 7.3.0-r3 p1.4) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

I am compiling with -lpthread flag:

/usr/bin/c++    -Wall -Wunused-function -Wwrite-strings -Wformat -Wformat-security -Wparentheses -Wsequence-point -Wno-system-headers -Werror -Winit-self  -g -O0 -fstack-protector-all -D_GLIBXX_DEBUG -D_GLIBXX_DEBUG_PEDANTIC  -rdynamic CMakeFiles/trivial_test.dir/trivial_test.cpp.o  -o trivial_test ../libturnstile_lib.a -lpthread

How can I get more info about the problem?


Solution

  • First you need to find out if the error is coming from your function that is being run by std::thread or from std::thread itself. If your function throws an exception it will be caught by the std::thread launcher function, and terminate will be called. If you make your function noexcept then terminate will get called before it's caught, and you'll see where it's thrown from in the stack trace (later versions of GCC don't catch the exception, so this happens automatically).

    If the exception is coming from std::thread itself it means that your program has linked to the dummy definition of pthread_create in libc.so.6 instead of the real one in libpthread.so or libpthread.a

    Use ldd to see if your program is linking to the shared libpthread.so or not. If it is, something's wrong with your toolchain (the definition in libpthread.so should get used instead of the weak symbol in libc.so). If you're linking statically, you might need to ensure that all symbols from libpthread.a are included in your program, e.g. by using:

    -Wl,--whole-archive -pthread -Wl,--no-whole-archive
    

    N.B. you should use -pthread not -lpthread as this allows GCC to ensure it is placed at the right place in the link command.