terminate called without an active exception when calling pthread_exit in segmentation fault handler

273 views Asked by At

How are you?
I am going to fix the segmentation fault in a worker thread on Ubuntu 18.04.
My code is the following.

#include <thread>
#include <signal.h>
#include <string.h>
#include <pthread.h>
#include <opencv2/opencv.hpp>

void sigsegv_handler(int signum, siginfo_t *info, void *data)
{
    printf("The thread was crashed\n");
    pthread_exit(NULL);
}

void sleep_ms(int milliseconds)
{
#ifdef WIN32
    Sleep(milliseconds);
#elif _POSIX_C_SOURCE >= 199309L
    struct timespec ts;
    ts.tv_sec = milliseconds / 1000;
    ts.tv_nsec = (milliseconds % 1000) * 1000000;
    nanosleep(&ts, NULL);
#else
    usleep(milliseconds * 1000);
#endif
}

void thread_func(int i)
{
    if(i == 3)
    {
        int *p = 0;
        *p = 10;
    }
    printf("A new thread ran successfully\n");
}

int main()
{
    /* Set SIGSEGV handler. */
    struct sigaction  handler;
    sigemptyset(&handler.sa_mask);
    handler.sa_sigaction = &sigsegv_handler;
    handler.sa_flags = SA_SIGINFO;
    if (sigaction(SIGSEGV, &handler, NULL) == -1) 
        fprintf(stderr, "Cannot set SIGSEGV handler: %s.\n", strerror(errno));
    int i = 0;
    while(1)
    {
        std::thread writer_thread(thread_func, i);
        writer_thread.detach();
        sleep_ms(1000);
        printf("%d\n", i++);
    }
    return 0;
}

The code works well.
The output of this code are following.
A new thread ran successfully
0
A new thread ran successfully
1
A new thread ran successfully
2
The thread was crashed
3
A new thread ran successfully
4
A new thread ran successfully
5
A new thread ran successfully
6
A new thread ran successfully
7

But if I change the function "thread_func" as the following, the program is crashed.

void thread_func(int i)
{
    if(i == 3)
    {
        int *p = 0;
        *p = 10;
    }
    cv::Mat img(100, 100, CV_8UC3); // newly inserted
    cv::resize(img, img, cv::Size(200, 200)); //newly inserted
    printf("A new thread ran successfully\n");
}

The error messages are the following.

A new thread ran successfully
0
A new thread ran successfully
1
A new thread ran successfully
2
The thread was crashed
terminate called without an active exception
Aborted (core dumped)

Of course, I am sure there is no issue in OpenCV module.
Could u help me to fix this issue?
Thanks

1

There are 1 answers

2
Andrew Henle On BEST ANSWER

The simple answer is you can't do this:

void sigsegv_handler(int signum, siginfo_t *info, void *data)
{
    printf("The thread was crashed\n");
    pthread_exit(NULL);
}

First, per 7.1.4 Use of library functions, paragraph 4 of the C 11 standard:

The functions in the standard library are not guaranteed to be reentrant and may modify objects with static or thread storage duration.

Or, as summarized by footnote 188:

Thus, a signal handler cannot, in general, call standard library functions.

So, absent specific guarantees from your platform about what functions you can safely call from a signal handler, you can not make any function calls from within a signal handler.

But since you are calling pthread_exit(), assuming you're using a POSIX system, POSIX does provide some guarantees about what functions you can call, termed "async-signal-safe, at https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_04_03. The Linux-specific list can be found at https://man7.org/linux/man-pages/man7/signal-safety.7.html

Note that neither printf() nor pthread_exit() are on either list.

Calling printf() from within a SIGSEGV signal handler is going to be dangerous - most implementations of printf() will use some form of malloc()/free(), and SIGSEGV is often a result of a malloc()/new/free()/delete operation encountering that corrupted heap. Heap operations tend to happen under a lock of some sort to protect against simultaneous modification of heap state, so calling printf() in a SIGSEGV handler of all things creates a huge deadlock risk.

And pthread_exit() will also cause huge problems - it's not only trying to change process state in the process's address space, it's trying to make changes to the process state in kernel space. From within a signal handler, that's simply not going to work.