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

Commit c90204c

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 8247236 commit c90204c

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
@@ -73,6 +73,7 @@ static PQExpBuffer recoveryconfcontents = NULL;
7373

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

@@ -85,6 +86,26 @@ static void BaseBackup(void);
8586
static bool reached_end_position(XLogRecPtr segendpos, uint32 timeline,
8687
bool segment_finished);
8788

89+
90+
static void disconnect_and_exit(int code)
91+
{
92+
if (conn != NULL)
93+
PQfinish(conn);
94+
95+
#ifndef WIN32
96+
/*
97+
* On windows, our background thread dies along with the process.
98+
* But on Unix, if we have started a subprocess, we want to kill
99+
* it off so it doesn't remain running trying to stream data.
100+
*/
101+
if (bgchild> 0)
102+
kill(bgchild, SIGTERM);
103+
#endif
104+
105+
exit(code);
106+
}
107+
108+
88109
#ifdef HAVE_LIBZ
89110
static const char *
90111
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
@@ -10,10 +10,4 @@ extern int dbgetpassword;
1010
/* Connection kept global so we can disconnect easily */
1111
extern PGconn *conn;
1212

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

0 commit comments

Comments
 (0)