我假设您已经考虑过这一点,并且有充分的理由相信您的程序将通过尝试在 SIGSEGV 之后重试而具有更多弹性 - 请记住段错误突出显示悬空指针和其他滥用问题,这些问题也可能会破坏进程地址空间中不可预测的位置,而不会出现段错误。
由于您已经非常仔细地考虑过这一点,并且您已经确定(以某种方式)您的应用程序段错误的特定方式不可能掩盖用于取消和重新启动线程的会计数据的损坏,并且您可以完美取消这些线程的逻辑(也非常罕见),让我们继续解决问题。
Linux 上的 SIGSEGV 处理程序在失败指令的线程中执行(man 7 信号)。我们不能调用 pthread_self() 因为它不是异步信号安全的,但是互联网上似乎普遍认为 syscall (man 2 syscall) 是安全的,所以我们可以通过 syscall SYS_gettid 获取线程 ID。因此,我们将维护 pthread_t (pthread_self) 到 pid (gettid()) 的映射。由于 write() 也是安全的,我们可以捕获 SEGV,将当前线程 ID 写入管道,然后暂停直到 pthread_cancel 终止我们。
我们还需要一个监控线程来监视事情何时变成梨形。监视器线程监视管道的读取端以获取有关已终止线程的信息,并可能重新启动它。
因为我认为假装处理 SIGSEGV 是愚蠢的,所以我将在这里调用执行此操作的结构 daft_thread_t 等。someone_please_fix_me 代表您损坏的代码。监控线程是 main()。当一个线程出现段错误时,它被信号处理程序捕获,将其 ID 写入管道;监视器读取管道,使用 pthread_cancel 和 pthread_join 取消线程,然后重新启动它。
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/syscall.h>
#define MAX_DAFT_THREADS (1024) // arbitrary
#define CHECK_OSCALL(call, onfail) { \
if ((call) == -1) { \
char buf[512]; \
strerror_r(errno, buf, sizeof(buf)); \
fprintf(stderr, "%s@%d failed: %s\n", __FILE__, __LINE__, buf); \
onfail; \
} \
}
/*********************** daft thread accounting *****************/
typedef void* (*threadproc_t)(void* arg);
struct daft_thread_t {
threadproc_t start_routine;
void* start_routine_arg;
pthread_t pthread;
pid_t tid;
};
struct daft_thread_accounting_info_t {
int monitor_pipe[2];
pthread_mutex_t info_lock;
size_t daft_thread_count;
struct daft_thread_t daft_threads[MAX_DAFT_THREADS];
};
static struct daft_thread_accounting_info_t g_thread_accounting;
void daft_thread_accounting_info_init(struct daft_thread_accounting_info_t* inf)
{
memset(inf, 0, sizeof(*inf));
pthread_mutex_init(&inf->info_lock, NULL);
CHECK_OSCALL(pipe(inf->monitor_pipe), abort());
}
struct daft_thread_wrapper_data_t {
struct daft_thread_t* thread_info;
};
static void* daft_thread_wrapper(void* arg)
{
struct daft_thread_t* wrapper = arg;
wrapper->tid = gettid();
return (*wrapper->start_routine)(wrapper->start_routine_arg);
}
static void start_daft_thread(threadproc_t proc, void* arg)
{
struct daft_thread_t* info;
pthread_mutex_lock(&g_thread_accounting.info_lock);
assert (g_thread_accounting.daft_thread_count < MAX_DAFT_THREADS);
info = &g_thread_accounting.daft_threads[g_thread_accounting.daft_thread_count++];
pthread_mutex_unlock(&g_thread_accounting.info_lock);
info->start_routine = proc;
info->start_routine_arg = arg;
CHECK_OSCALL(pthread_create(&info->pthread, NULL, daft_thread_wrapper, info), abort());
}
static struct daft_thread_t* find_thread_by_tid(pid_t thread_id)
{
int k;
struct daft_thread_t* info = NULL;
pthread_mutex_lock(&g_thread_accounting.info_lock);
for (k = 0; k < g_thread_accounting.daft_thread_count; ++k) {
if (g_thread_accounting.daft_threads[k].tid == thread_id) {
info = &g_thread_accounting.daft_threads[k];
break;
}
}
pthread_mutex_unlock(&g_thread_accounting.info_lock);
return info;
}
static void restart_daft_thread(struct daft_thread_t* info)
{
void* unused;
CHECK_OSCALL(pthread_cancel(info->pthread), abort());
CHECK_OSCALL(pthread_join(info->pthread, &unused), abort());
info->tid = 0;
CHECK_OSCALL(pthread_create(&info->pthread, NULL, daft_thread_wrapper, info), abort());
}
/************* signal handling stuff **************/
struct sigdeath_notify_info {
int signum;
pid_t tid;
};
static void sigdeath_handler(int signum, siginfo_t* info, void* ctx)
{
int z;
struct sigdeath_notify_info inf = {
.signum = signum,
.tid = gettid()
};
z = write(g_thread_accounting.monitor_pipe[1], &inf, sizeof(inf));
assert (z == sizeof(inf)); // or else SIGABRT. Are we handling that too? Hope not.
pause(); // returning doesn't do us any good.
}
static void register_signal_handlers()
{
struct sigaction sa = {};
sa.sa_sigaction = sigdeath_handler;
sa.sa_flags = SA_SIGINFO;
CHECK_OSCALL(sigaction(SIGSEGV, &sa, NULL), abort());
CHECK_OSCALL(sigaction(SIGBUS, &sa, NULL), abort());
}
pid_t gettid() { return (pid_t) syscall(SYS_gettid); }
/** This is the code that segfaults randomly. Kwality with a 'k'. */
static void* someone_please_fix_me(void* arg)
{
char* i_think_this_address_looks_nice = (char*) 42;
sleep(1 + rand() % 200);
i_think_this_address_looks_nice[0] = 'q'; // ugh
return NULL;
}
// main() will serve as the monitor thread here
int main()
{
int k;
struct sigdeath_notify_info death;
daft_thread_accounting_info_init(&g_thread_accounting);
register_signal_handlers();
for (k = 0; k < 200; ++k) {
start_daft_thread(someone_please_fix_me, (void*) k);
}
while (read(g_thread_accounting.monitor_pipe[0], &death, sizeof(death)) == sizeof(death)) {
struct daft_thread_t* info = find_thread_by_tid(death.tid);
if (info == NULL) {
fprintf(stderr, "*** thread_id %u not found\n", death.tid);
continue;
}
fprintf(stderr, "Thread %u (%d) died of %d, restarting.\n",
death.tid, (int) info->start_routine_arg, death.signum);
restart_daft_thread(info);
}
fprintf(stderr, "Shouldn't get here.\n");
return 0;
}
如果您没有考虑过:尝试从 SIGSEGV 中恢复是非常危险的 - 我强烈建议您不要这样做。线程共享一个地址空间。发生段错误的线程也可能损坏了其他线程数据或全局记帐数据,例如 malloc() 的记帐。一个更安全的方法 - 假设失败的代码被不可挽回地破坏但必须使用 - 是将失败的代码隔离在进程边界后面,例如在调用破坏的代码之前通过 fork()ing。然后,您必须捕获 SIGCLD 并处理进程正常崩溃或终止,以及许多其他陷阱,但至少您不必担心随机损坏。当然,最好的选择是修复该死的代码,这样您就不会观察到段错误。