Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 63ab2be

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 1c9acd5 commit 63ab2be

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

src/bin/pg_basebackup/pg_basebackup.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ static PQExpBuffer recoveryconfcontents = NULL;
7676

7777
/* Function headers */
7878
static void usage(void);
79+
static void disconnect_and_exit(int code);
7980
static void verify_dir_is_empty_or_create(char *dirname);
8081
static void progress_report(int tablespacenum, const char *filename, bool force);
8182

@@ -88,6 +89,26 @@ static void BaseBackup(void);
8889
static bool reached_end_position(XLogRecPtr segendpos, uint32 timeline,
8990
bool segment_finished);
9091

92+
93+
static void disconnect_and_exit(int code)
94+
{
95+
if (conn != NULL)
96+
PQfinish(conn);
97+
98+
#ifndef WIN32
99+
/*
100+
* On windows, our background thread dies along with the process.
101+
* But on Unix, if we have started a subprocess, we want to kill
102+
* it off so it doesn't remain running trying to stream data.
103+
*/
104+
if (bgchild> 0)
105+
kill(bgchild, SIGTERM);
106+
#endif
107+
108+
exit(code);
109+
}
110+
111+
91112
#ifdef HAVE_LIBZ
92113
static const char *
93114
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
@@ -45,6 +45,13 @@ static void StreamLog();
4545
static bool stop_streaming(XLogRecPtr segendpos, uint32 timeline,
4646
bool segment_finished);
4747

48+
#define disconnect_and_exit(code) \
49+
{ \
50+
if (conn != NULL) PQfinish(conn); \
51+
exit(code); \
52+
}
53+
54+
4855
static void
4956
usage(void)
5057
{

src/bin/pg_basebackup/streamutil.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,4 @@ extern char *replication_slot;
1111
/* Connection kept global so we can disconnect easily */
1212
extern PGconn *conn;
1313

14-
#define disconnect_and_exit(code) \
15-
{ \
16-
if (conn != NULL) PQfinish(conn); \
17-
exit(code); \
18-
}
19-
2014
extern PGconn *GetConnection(void);

0 commit comments

Comments
 (0)