From 647cac5bb469171a5b97eade276335ab9c552edc Mon Sep 17 00:00:00 2001 From: WuYunlong Date: Fri, 18 Sep 2020 16:08:52 +0800 Subject: [PATCH] Make main thread killable so that it can be canceled at any time. Refine comment of makeThreadKillable(). This commit can be backported to 5.0, only if we also backport 8b70cb0. Co-authored-by: Oran Agra --- src/bio.c | 5 +---- src/server.c | 9 +++++++++ src/server.h | 1 + 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/bio.c b/src/bio.c index dd99442ae..a512def0a 100644 --- a/src/bio.c +++ b/src/bio.c @@ -168,10 +168,7 @@ void *bioProcessBackgroundJobs(void *arg) { redisSetCpuAffinity(server.bio_cpulist); - /* Make the thread killable at any time, so that bioKillThreads() - * can work reliably. */ - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); - pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); + makeThreadKillable(); pthread_mutex_lock(&bio_mutex[type]); /* Block SIGALRM so we are sure that only the main thread will diff --git a/src/server.c b/src/server.c index 3363d476f..8ae5c64d9 100644 --- a/src/server.c +++ b/src/server.c @@ -2870,12 +2870,21 @@ void resetServerStats(void) { server.blocked_last_cron = 0; } +/* Make the thread killable at any time, so that kill threads functions + * can work reliably (default cancelability type is PTHREAD_CANCEL_DEFERRED). + * Needed for pthread_cancel used by the fast memory test used by the crash report. */ +void makeThreadKillable(void) { + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); + pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); +} + void initServer(void) { int j; signal(SIGHUP, SIG_IGN); signal(SIGPIPE, SIG_IGN); setupSignalHandlers(); + makeThreadKillable(); if (server.syslog_enabled) { openlog(server.syslog_ident, LOG_PID | LOG_NDELAY | LOG_NOWAIT, diff --git a/src/server.h b/src/server.h index 7d27626a7..c0ca89b8b 100644 --- a/src/server.h +++ b/src/server.h @@ -2492,6 +2492,7 @@ int populateCommandTableParseFlags(struct redisCommand *c, char *strflags); void debugDelay(int usec); void killIOThreads(void); void killThreads(void); +void makeThreadKillable(void); /* TLS stuff */ void tlsInit(void);