From dd13d356c242828a5022d16fd16f2362720a12ee Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Fri, 8 Aug 2014 15:03:32 +1000 Subject: [PATCH] Check threads exist before trying to cancel or join them --- src/ckpool.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/ckpool.c b/src/ckpool.c index 538ed647..61293754 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -871,8 +871,10 @@ static void __shutdown_children(ckpool_t *ckp, int sig) { int i; - pthread_cancel(ckp->pth_watchdog); - join_pthread(ckp->pth_watchdog); + if (ckp->pth_watchdog) { + pthread_cancel(ckp->pth_watchdog); + join_pthread(ckp->pth_watchdog); + } for (i = 0; i < ckp->proc_instances; i++) { pid_t pid = ckp->children[i]->pid; @@ -883,8 +885,10 @@ static void __shutdown_children(ckpool_t *ckp, int sig) static void shutdown_children(ckpool_t *ckp, int sig) { - pthread_cancel(ckp->pth_watchdog); - join_pthread(ckp->pth_watchdog); + if (ckp->pth_watchdog) { + pthread_cancel(ckp->pth_watchdog); + join_pthread(ckp->pth_watchdog); + } __shutdown_children(ckp, sig); } @@ -895,14 +899,17 @@ static void sighandler(int sig) LOGWARNING("Parent process %s received signal %d, shutting down", ckp->name, sig); - pthread_cancel(ckp->pth_watchdog); - join_pthread(ckp->pth_watchdog); + if (ckp->pth_watchdog) { + pthread_cancel(ckp->pth_watchdog); + join_pthread(ckp->pth_watchdog); + } __shutdown_children(ckp, SIGTERM); /* Wait a second, then send SIGKILL */ sleep(1); __shutdown_children(ckp, SIGKILL); - pthread_cancel(ckp->pth_listener); + if (ckp->pth_listener) + pthread_cancel(ckp->pth_listener); exit(0); } @@ -1310,7 +1317,8 @@ int main(int argc, char **argv) sigaction(SIGINT, &handler, NULL); /* Shutdown from here if the listener is sent a shutdown message */ - join_pthread(ckp.pth_listener); + if (ckp.pth_listener) + join_pthread(ckp.pth_listener); shutdown_children(&ckp, SIGTERM); clean_up(&ckp);