]> granicus.if.org Git - postgresql/commitdiff
Kill pg_basebackup background process when exiting
authorMagnus Hagander <magnus@hagander.net>
Sun, 9 Feb 2014 12:10:14 +0000 (13:10 +0100)
committerMagnus Hagander <magnus@hagander.net>
Wed, 12 Feb 2014 17:45:18 +0000 (18:45 +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 a6e320c0f2266325703449890e6d5706dd5cb3de..c27c6332ce77c798df098cb54d5ee29066a0df36 100644 (file)
@@ -76,6 +76,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, bool force);
 
@@ -88,6 +89,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 8a702e3388015e0174f6ede850ced57b0e2caed3..0f191ce6bb389d4f930979c97392c64cabeeee20 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 bb3c34db07f831a1e0b9aee6e22e3c5c53e8952b..7c7d0228897780dae2f12328501401e327050f52 100644 (file)
@@ -11,10 +11,4 @@ extern char *replication_slot;
 /* 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);