Skip to content

Commit 0ae288d

Browse files
committed
Kill pg_basebackup background process when exiting
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.
1 parent dd56051 commit 0ae288d

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

src/bin/pg_basebackup/pg_basebackup.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ static volatile LONG has_xlogendptr = 0;
7272

7373
/* Function headers */
7474
static void usage(void);
75+
static void disconnect_and_exit(int code);
7576
static void verify_dir_is_empty_or_create(char *dirname);
7677
static void progress_report(int tablespacenum, const char *filename);
7778

@@ -82,6 +83,26 @@ static void BaseBackup(void);
8283
static bool reached_end_position(XLogRecPtr segendpos, uint32 timeline,
8384
bool segment_finished);
8485

86+
87+
static void disconnect_and_exit(int code)
88+
{
89+
if (conn != NULL)
90+
PQfinish(conn);
91+
92+
#ifndef WIN32
93+
/*
94+
* On windows, our background thread dies along with the process.
95+
* But on Unix, if we have started a subprocess, we want to kill
96+
* it off so it doesn't remain running trying to stream data.
97+
*/
98+
if (bgchild> 0)
99+
kill(bgchild, SIGTERM);
100+
#endif
101+
102+
exit(code);
103+
}
104+
105+
85106
#ifdef HAVE_LIBZ
86107
static const char *
87108
get_gz_error(gzFile gzf)

src/bin/pg_basebackup/pg_receivexlog.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ static void StreamLog();
5151
static bool stop_streaming(XLogRecPtr segendpos, uint32 timeline,
5252
bool segment_finished);
5353

54+
#define disconnect_and_exit(code) \
55+
{ \
56+
if (conn != NULL) PQfinish(conn); \
57+
exit(code); \
58+
}
59+
60+
5461
static void
5562
usage(void)
5663
{

src/bin/pg_basebackup/streamutil.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@ extern int dbgetpassword;
99
/* Connection kept global so we can disconnect easily */
1010
extern PGconn *conn;
1111

12-
#define disconnect_and_exit(code) \
13-
{ \
14-
if (conn != NULL) PQfinish(conn); \
15-
exit(code); \
16-
}
17-
18-
1912
char *xstrdup(const char *s);
2013
void *xmalloc0(int size);
2114

0 commit comments

Comments
 (0)