Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 45d067d

Browse files
committed
Unify SIGHUP handling between normal and walsender backends.
Because walsender and normal backends share the same main loop it's problematic to have two different flag variables, set in signal handlers, indicating a pending configuration reload. Only certain walsender commands reach code paths checking for the variable (START_[LOGICAL_]REPLICATION, CREATE_REPLICATION_SLOT ... LOGICAL, notably not base backups). This is a bug present since the introduction of walsender, but has gotten worse in releases since then which allow walsender to do more. A later patch, not slated for v10, will similarly unify SIGHUP handling in other types of processes as well. Author: Petr Jelinek, Andres Freund Reviewed-By: Michael Paquier Discussion: https://postgr.es/m/[email protected] Backpatch: 9.2-, bug is present since 9.0
1 parent da30fa6 commit 45d067d

File tree

4 files changed

+23
-33
lines changed

4 files changed

+23
-33
lines changed

src/backend/replication/walsender.c

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ static volatile sig_atomic_t walsender_ready_to_stop = false;
166166
static volatile sig_atomic_t replication_active = false;
167167

168168
/* Signal handlers */
169-
static void WalSndSigHupHandler(SIGNAL_ARGS);
170169
static void WalSndXLogSendHandler(SIGNAL_ARGS);
171170
static void WalSndLastCycleHandler(SIGNAL_ARGS);
172171

@@ -979,9 +978,9 @@ WalSndLoop(void)
979978
exit(1);
980979

981980
/* Process any requests or signals received recently */
982-
if (got_SIGHUP)
981+
if (ConfigReloadPending)
983982
{
984-
got_SIGHUP = false;
983+
ConfigReloadPending = false;
985984
ProcessConfigFile(PGC_SIGHUP);
986985
SyncRepInitConfig();
987986
}
@@ -1712,19 +1711,6 @@ WalSndRqstFileReload(void)
17121711
}
17131712
}
17141713

1715-
/* SIGHUP: set flag to re-read config file at next convenient time */
1716-
static void
1717-
WalSndSigHupHandler(SIGNAL_ARGS)
1718-
{
1719-
int save_errno = errno;
1720-
1721-
got_SIGHUP = true;
1722-
if (MyWalSnd)
1723-
SetLatch(&MyWalSnd->latch);
1724-
1725-
errno = save_errno;
1726-
}
1727-
17281714
/* SIGUSR1: set flag to send WAL records */
17291715
static void
17301716
WalSndXLogSendHandler(SIGNAL_ARGS)
@@ -1763,7 +1749,7 @@ void
17631749
WalSndSignals(void)
17641750
{
17651751
/* Set up signal handlers */
1766-
pqsignal(SIGHUP, WalSndSigHupHandler); /* set flag to read config
1752+
pqsignal(SIGHUP, PostgresSigHupHandler); /* set flag to read config
17671753
* file */
17681754
pqsignal(SIGINT, SIG_IGN); /* not used */
17691755
pqsignal(SIGTERM, die); /* request shutdown */

src/backend/tcop/postgres.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,6 @@ char *stack_base_ptr = NULL;
130130
char *register_stack_base_ptr = NULL;
131131
#endif
132132

133-
/*
134-
* Flag to mark SIGHUP. Whenever the main loop comes around it
135-
* will reread the configuration file. (Better than doing the
136-
* reading in the signal handler, ey?)
137-
*/
138-
static volatile sig_atomic_t got_SIGHUP = false;
139-
140133
/*
141134
* Flag to keep track of whether we have started a transaction.
142135
* For extended query protocol this has to be remembered across messages.
@@ -205,7 +198,6 @@ static bool IsTransactionExitStmt(Node *parsetree);
205198
static bool IsTransactionExitStmtList(List *parseTrees);
206199
static bool IsTransactionStmtList(List *parseTrees);
207200
static void drop_unnamed_stmt(void);
208-
static void SigHupHandler(SIGNAL_ARGS);
209201
static void log_disconnections(int code, Datum arg);
210202

211203

@@ -2685,13 +2677,19 @@ FloatExceptionHandler(SIGNAL_ARGS)
26852677
"invalid operation, such as division by zero.")));
26862678
}
26872679

2688-
/* SIGHUP: set flag to re-read config file at next convenient time */
2689-
static void
2690-
SigHupHandler(SIGNAL_ARGS)
2680+
/*
2681+
* SIGHUP: set flag to re-read config file at next convenient time.
2682+
*
2683+
* Sets the ConfigReloadPending flag, which should be checked at convenient
2684+
* places inside main loops. (Better than doing the reading in the signal
2685+
* handler, ey?)
2686+
*/
2687+
void
2688+
PostgresSigHupHandler(SIGNAL_ARGS)
26912689
{
26922690
int save_errno = errno;
26932691

2694-
got_SIGHUP = true;
2692+
ConfigReloadPending = true;
26952693
if (MyProc)
26962694
SetLatch(&MyProc->procLatch);
26972695

@@ -3683,8 +3681,8 @@ PostgresMain(int argc, char *argv[],
36833681
WalSndSignals();
36843682
else
36853683
{
3686-
pqsignal(SIGHUP, SigHupHandler); /* set flag to read config
3687-
* file */
3684+
pqsignal(SIGHUP, PostgresSigHupHandler); /* set flag to read config
3685+
* file */
36883686
pqsignal(SIGINT, StatementCancelHandler); /* cancel current query */
36893687
pqsignal(SIGTERM, die); /* cancel current query and exit */
36903688

@@ -4062,9 +4060,9 @@ PostgresMain(int argc, char *argv[],
40624060
* (5) check for any other interesting events that happened while we
40634061
* slept.
40644062
*/
4065-
if (got_SIGHUP)
4063+
if (ConfigReloadPending)
40664064
{
4067-
got_SIGHUP = false;
4065+
ConfigReloadPending = false;
40684066
ProcessConfigFile(PGC_SIGHUP);
40694067
}
40704068

src/backend/utils/init/globals.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ volatile bool QueryCancelPending = false;
3030
volatile bool ProcDiePending = false;
3131
volatile bool ClientConnectionLost = false;
3232
volatile bool ImmediateInterruptOK = false;
33+
volatile sig_atomic_t ConfigReloadPending = false;
3334
volatile uint32 InterruptHoldoffCount = 0;
3435
volatile uint32 QueryCancelHoldoffCount = 0;
3536
volatile uint32 CritSectionCount = 0;

src/include/miscadmin.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#ifndef MISCADMIN_H
2424
#define MISCADMIN_H
2525

26+
#include <signal.h>
27+
2628
#include "pgtime.h" /* for pg_time_t */
2729

2830

@@ -78,6 +80,7 @@
7880
extern PGDLLIMPORT volatile bool InterruptPending;
7981
extern volatile bool QueryCancelPending;
8082
extern volatile bool ProcDiePending;
83+
extern PGDLLIMPORT volatile sig_atomic_t ConfigReloadPending;
8184

8285
extern volatile bool ClientConnectionLost;
8386

@@ -276,6 +279,8 @@ extern void restore_stack_base(pg_stack_base_t base);
276279
extern void check_stack_depth(void);
277280
extern bool stack_is_too_deep(void);
278281

282+
extern void PostgresSigHupHandler(SIGNAL_ARGS);
283+
279284
/* in tcop/utility.c */
280285
extern void PreventCommandIfReadOnly(const char *cmdname);
281286
extern void PreventCommandDuringRecovery(const char *cmdname);

0 commit comments

Comments
 (0)