diff --git a/src/bin/pg_basebackup/pg_basebackup.cpp b/src/bin/pg_basebackup/pg_basebackup.cpp index b811f51d0316c8e5a453de2a9ebb7b02bd78b609..d607f8bd5b4c38039f57422f85190651ee59c8d3 100644 --- a/src/bin/pg_basebackup/pg_basebackup.cpp +++ b/src/bin/pg_basebackup/pg_basebackup.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include "tool_common.h" #include "getopt_long.h" @@ -461,7 +462,11 @@ static void StartLogStreamer(const char *startpos, uint32 timeline, char *syside #ifndef WIN32 bgchild = fork(); if (bgchild == 0) { - /* in child process */ + /* + * In child process. + * Receive SIGKILL when main process exits. + */ + prctl(PR_SET_PDEATHSIG, SIGKILL); exit(LogStreamerMain(g_childParam)); } else if (bgchild < 0) { fprintf(stderr, _("%s: could not create background process: %s\n"), progname, strerror(errno)); diff --git a/src/bin/pg_basebackup/receivelog.cpp b/src/bin/pg_basebackup/receivelog.cpp index 2f970bff87229bfe147b8a8879795920178bb0d3..275bc2ec0c63a3ea52443a32a2221d3377ccb930 100644 --- a/src/bin/pg_basebackup/receivelog.cpp +++ b/src/bin/pg_basebackup/receivelog.cpp @@ -53,6 +53,10 @@ static XLogRecPtr lastFlushPosition = InvalidXLogRecPtr; extern char* basedir; extern int standby_message_timeout; +#define HEART_BEAT_INIT 0 +#define HEART_BEAT_RUN 1 +#define HEART_BEAT_STOP 2 + /* * The max size for single data file. copy from custorage.cpp. */ @@ -64,7 +68,7 @@ const int HEART_BEAT = 5; PGconn* xlogconn = NULL; pthread_t hearbeatTimerId; volatile uint32 timerFlag = 0; -volatile uint32 heartbeatRunning = 0; +volatile uint32 heartbeatRunning = HEART_BEAT_INIT; pthread_mutex_t heartbeatMutex; typedef enum { DO_WAL_DATA_WRITE_DONE, DO_WAL_DATA_WRITE_STOP, DO_WAL_DATA_WRITE_ERROR } DoWalDataWriteResult; @@ -221,8 +225,9 @@ void* heartbeatTimerHandler(void* data) if (xlogconn == NULL) { return NULL; } - heartbeatRunning = 1; - while (heartbeatRunning) { + uint32 expected = HEART_BEAT_INIT; + (void)pg_atomic_compare_exchange_u32(&heartbeatRunning, &expected, HEART_BEAT_RUN); + while (pg_atomic_read_u32(&heartbeatRunning) == HEART_BEAT_RUN) { pthread_mutex_lock(&heartbeatMutex); (void)checkForReceiveTimeout(xlogconn); ping_sent = false; @@ -266,7 +271,7 @@ void suspendHeartBeatTimer(void) void closeHearBeatTimer(void) { - heartbeatRunning = 0; + pg_atomic_write_u32(&heartbeatRunning, HEART_BEAT_STOP); pthread_mutex_unlock(&heartbeatMutex); (void)pthread_join(hearbeatTimerId, NULL); return; diff --git a/src/bin/pg_ctl/backup.cpp b/src/bin/pg_ctl/backup.cpp index 7f12e83a206b4c783847d04906b843dbca4d2a9b..7270af2448ef731e9a385557eb0e9fa92b884142 100755 --- a/src/bin/pg_ctl/backup.cpp +++ b/src/bin/pg_ctl/backup.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #ifdef HAVE_LIBZ #include "zlib.h" @@ -562,7 +563,11 @@ bool StartLogStreamer( fflush(stderr); bgchild = fork(); if (bgchild == 0) { - /* in child process */ + /* + * In child process. + * Receive SIGKILL when main process exits. + */ + prctl(PR_SET_PDEATHSIG, SIGKILL); exit(LogStreamerMain(param)); } else if (bgchild < 0) { pg_log(PG_WARNING, _(" could not create background process: %s.\n"), strerror(errno)); diff --git a/src/bin/pg_ctl/receivelog.cpp b/src/bin/pg_ctl/receivelog.cpp index 14b62847cc0f234547b3154c1422acb14844035a..a172d1154a49d748e4b9f4c6c1c789728bae823d 100644 --- a/src/bin/pg_ctl/receivelog.cpp +++ b/src/bin/pg_ctl/receivelog.cpp @@ -51,6 +51,10 @@ static bool reportFlushPosition = false; static XLogRecPtr lastFlushPosition = InvalidXLogRecPtr; extern char* basedir; +#define HEART_BEAT_INIT 0 +#define HEART_BEAT_RUN 1 +#define HEART_BEAT_STOP 2 + /* * The max size for single data file. copy from custorage.cpp. */ @@ -62,7 +66,7 @@ const int HEART_BEAT = 5; PGconn* xlogconn = NULL; pthread_t hearbeatTimerId; volatile uint32 timerFlag = 0; -volatile uint32 heartbeatRunning = 0; +volatile uint32 heartbeatRunning = HEART_BEAT_INIT; pthread_mutex_t heartbeatMutex; typedef enum { DO_WAL_DATA_WRITE_DONE, DO_WAL_DATA_WRITE_STOP, DO_WAL_DATA_WRITE_ERROR } DoWalDataWriteResult; @@ -211,8 +215,9 @@ void* heartbeatTimerHandler(void* data) if (xlogconn == NULL) { return NULL; } - heartbeatRunning = 1; - while (heartbeatRunning) { + uint32 expected = HEART_BEAT_INIT; + (void)pg_atomic_compare_exchange_u32(&heartbeatRunning, &expected, HEART_BEAT_RUN); + while (pg_atomic_read_u32(&heartbeatRunning) == HEART_BEAT_RUN) { pthread_mutex_lock(&heartbeatMutex); (void)checkForReceiveTimeout(xlogconn); ping_sent = false; @@ -256,7 +261,7 @@ void suspendHeartBeatTimer(void) void closeHearBeatTimer(void) { - heartbeatRunning = 0; + pg_atomic_write_u32(&heartbeatRunning, HEART_BEAT_STOP); pthread_mutex_unlock(&heartbeatMutex); (void)pthread_join(hearbeatTimerId, NULL); return;