Kill pg_basebackup background process when exiting
authorMagnus Hagander
Sun, 9 Feb 2014 12:10:14 +0000 (13:10 +0100)
committerMagnus Hagander
Wed, 12 Feb 2014 17:46:04 +0000 (18:46 +0100)
If an error occurs in the foreground (backup) process of pg_basebackup,
and we exit in a controlled way, the background process (streaming
xlog process) would stay around and keep streaming.

src/bin/pg_basebackup/pg_basebackup.c
src/bin/pg_basebackup/pg_receivexlog.c
src/bin/pg_basebackup/streamutil.h

index 34788122ccaae3e4db7433a08be8645f10ca1b9d..12ed6c8640e9977efbdb3c068d611da9dde2c63d 100644 (file)
@@ -73,6 +73,7 @@ static PQExpBuffer recoveryconfcontents = NULL;
 
 /* Function headers */
 static void usage(void);
+static void disconnect_and_exit(int code);
 static void verify_dir_is_empty_or_create(char *dirname);
 static void progress_report(int tablespacenum, const char *filename);
 
@@ -85,6 +86,26 @@ static void BaseBackup(void);
 static bool reached_end_position(XLogRecPtr segendpos, uint32 timeline,
                     bool segment_finished);
 
+
+static void disconnect_and_exit(int code)
+{
+   if (conn != NULL)
+       PQfinish(conn);
+
+#ifndef WIN32
+   /*
+    * On windows, our background thread dies along with the process.
+    * But on Unix, if we have started a subprocess, we want to kill
+    * it off so it doesn't remain running trying to stream data.
+    */
+   if (bgchild> 0)
+       kill(bgchild, SIGTERM);
+#endif
+
+   exit(code);
+}
+
+
 #ifdef HAVE_LIBZ
 static const char *
 get_gz_error(gzFile gzf)
index 252a7e08d6727e9e3aa08cf1fc3eb7e925638104..5738c0e6e6abe2a81d41644fae65a76e9494fd55 100644 (file)
@@ -45,6 +45,13 @@ static void StreamLog();
 static bool stop_streaming(XLogRecPtr segendpos, uint32 timeline,
               bool segment_finished);
 
+#define disconnect_and_exit(code)              \
+   {                                           \
+   if (conn != NULL) PQfinish(conn);           \
+   exit(code);                                 \
+   }
+
+
 static void
 usage(void)
 {
index 77d6b86ced3c4aed719f76d14654dfb12442e7d6..83c3087a0baa82aa82d5f53879f40992f30a38e5 100644 (file)
@@ -10,10 +10,4 @@ extern int   dbgetpassword;
 /* Connection kept global so we can disconnect easily */
 extern PGconn *conn;
 
-#define disconnect_and_exit(code)              \
-   {                                           \
-   if (conn != NULL) PQfinish(conn);           \
-   exit(code);                                 \
-   }
-
 extern PGconn *GetConnection(void);