Pthread exit
lastmod 2025-02-14created 2019-12-197 edits
Terminates the currently running thread.
Synopsis
Troubleshooting
FATAL: exception not rethrown
An error like the following may occur.
FATAL: exception not rethrown
A 2019-12-20T12:39:03.230677552 @140313816094464 Abort signal. (SIGABRT):
#00 0x00007F9D626EDF20 +257824 [/lib/x86_64-linux-gnu/libc.so.6]
#01 0x00007F9D626EDE97 gsignal+199 [/lib/x86_64-linux-gnu/libc.so.6]
#02 0x00007F9D626EF801 abort+321 [/lib/x86_64-linux-gnu/libc.so.6]
#03 0x00007F9D627385E5 +562661 [/lib/x86_64-linux-gnu/libc.so.6]
#04 0x00007F9D6273892A +563498 [/lib/x86_64-linux-gnu/libc.so.6]
#05 0x00007F9D66078ED0 __pthread_unwind+0 [/lib/x86_64-linux-gnu/libpthread.so.0]
#06 0x00007F9D63B732FD libtbag::thread::__global_uv_thread_cb__(void*)+223 [/home/user/Project/tbag/cmake-build-debug/libtbag.so.0]
#07 0x00007F9D6606F6DB +30427 [/lib/x86_64-linux-gnu/libpthread.so.0]
#08 0x00007F9D627D088F clone+63 [/lib/x86_64-linux-gnu/libc.so.6]
pthread is terrible at releasing resources related to stack unwinding. For example, problems occur when using a catch statement in a thread function. The original text is below.
Sorry to be the bearer of bad news but it is very hard to get pthread_cancel to work properly in C++ in Linux.
The reason is that the pthread_cancel method implementation in Linux is absolutely horrid. In particular it causes an exception (abi::__forced_unwind as of the time I'm writing this) to be thrown. This probably is fine in a C program, but in C++ it makes it very difficult to handle the thread cancellation properly. In particular if you have any code that eats exceptions (such as catch (...)) this internal exception will handled and not passed up the stack the way it needs to be. In this case you will see the error message that you are seeing.
That said, I don't see what in your code would be "eating" this exception, but I suspect it is something of that nature that is causing the problem.
In your particular example this is easy to work around by using a flag or something similar to note when you want to exit and not call pthread_cancel. Unfortunately that makes sleep more complex (you need to call it multiple times with small intervals in order to check the flag) and calls to accept nearly impossible (you have to make a connection to the port from another thread or process to force accept to exit so you can check the variable). There is a long discussion on the problems with thread cancellation in general ([https://lwn.net/Articles/683118/ This is why we can't have safe cancellation points]) but given that macOS (and presumably other non-Linux "unixes") seem to handle it well, I really think that the Linux implementation could be done better. But until that is done, pthread_cancel will remain very difficult to use.
For details, I referred to the post Thread: pthread_exit, c++ and FATAL: exception not rethrown. The key points are below.
- pthread_exit doesn't cope with catch (...)
- we need catch (abi::__forced_unwind&) throw; to get it work
- pthread_exit does stack unwinding and deletes the objects on the stack!
- bug: there isn't even one word about all this in man pthread_exit
Separately, code using abi::__forced_unwind looks like this.
#if defined(__linux__) && !defined(__ANDROID__)
#include <cxxabi.h>
#ifdef _CXXABI_FORCED_H
#define SPDLOG_CATCH_ALL catch (abi::__forced_unwind&) { _err_handler("Unknown exception"); throw; } catch (...)
#else // _CXXABI_FORCED_H
#define SPDLOG_CATCH_ALL catch (...)
#endif // _CXXABI_FORCED_H
#else // __linux__
#define SPDLOG_CATCH_ALL catch (...)
#endif // __linux
See also
Favorite site
Pages that link here