From bcba6f83fbc3e8a1a57956acdd79ce3db011ba12 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Fri, 9 Sep 2011 21:04:16 +1000 Subject: [PATCH] Exit cleanly if we abort before various threads are set up or if they no longer exist. --- main.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/main.c b/main.c index 1074480e..166b39c9 100644 --- a/main.c +++ b/main.c @@ -2255,25 +2255,28 @@ void kill_work(void) /* Kill the watchdog thread */ thr = &thr_info[watchdog_thr_id]; - if (thr->pth) + if (thr && thr->pth) pthread_cancel(*thr->pth); /* Stop the mining threads*/ for (i = 0; i < mining_threads; i++) { thr = &thr_info[i]; + if (!thr) + continue; if (!thr->pth) continue; - tq_freeze(thr->q); + if (thr->q) + tq_freeze(thr->q); /* No need to check if this succeeds or not */ pthread_cancel(*thr->pth); } /* Stop the others */ thr = &thr_info[stage_thr_id]; - if (thr->pth) + if (thr && thr->pth) pthread_cancel(*thr->pth); thr = &thr_info[longpoll_thr_id]; - if (thr->pth) + if (thr && thr->pth) pthread_cancel(*thr->pth); wc = calloc(1, sizeof(*wc)); @@ -2289,14 +2292,16 @@ void kill_work(void) if (opt_debug) applog(LOG_DEBUG, "Pushing die request to work thread"); - if (unlikely(!tq_push(thr_info[work_thr_id].q, wc))) { - applog(LOG_ERR, "Failed to tq_push work in kill_work"); - exit (1); - } - thr = &thr_info[work_thr_id]; - if (thr->pth) - pthread_cancel(*thr->pth); + if (thr) { + if (unlikely(!tq_push(thr->q, wc))) { + applog(LOG_ERR, "Failed to tq_push work in kill_work"); + exit (1); + } + + if (thr->pth) + pthread_cancel(*thr->pth); + } } void quit(int status, const char *format, ...);