Skip to content

Commit cb59949

Browse files
committed
Don't lose walreceiver start requests due to race condition in postmaster.
When a walreceiver dies, the startup process will notice that and send a PMSIGNAL_START_WALRECEIVER signal to the postmaster, asking for a new walreceiver to be launched. There's a race condition, which at least in HEAD is very easy to hit, whereby the postmaster might see that signal before it processes the SIGCHLD from the walreceiver process. In that situation, sigusr1_handler() just dropped the start request on the floor, reasoning that it must be redundant. Eventually, after 10 seconds (WALRCV_STARTUP_TIMEOUT), the startup process would make a fresh request --- but that's a long time if the connection could have been re-established almost immediately. Fix it by setting a state flag inside the postmaster that we won't clear until we do launch a walreceiver. In cases where that results in an extra walreceiver launch, it's up to the walreceiver to realize it's unwanted and go away --- but we have, and need, that logic anyway for the opposite race case. I came across this through investigating unexpected delays in the src/test/recovery TAP tests: it manifests there in test cases where a master server is stopped and restarted while leaving streaming slaves active. This logic has been broken all along, so back-patch to all supported branches. Discussion: https://postgr.es/m/21344.1498494720@sss.pgh.pa.us
1 parent 456bf26 commit cb59949

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,9 @@ static volatile sig_atomic_t start_autovac_launcher = false;
370370
/* the launcher needs to be signalled to communicate some condition */
371371
static volatile bool avlauncher_needs_signal = false;
372372

373+
/* received START_WALRECEIVER signal */
374+
static volatile sig_atomic_t WalReceiverRequested = false;
375+
373376
/* set when there's a worker that needs to be started up */
374377
static volatile bool StartWorkerNeeded = true;
375378
static volatile bool HaveCrashedWorker = false;
@@ -443,6 +446,7 @@ static void maybe_start_bgworker(void);
443446
static bool CreateOptsFile(int argc, char *argv[], char *fullprogname);
444447
static pid_t StartChildProcess(AuxProcType type);
445448
static void StartAutovacuumWorker(void);
449+
static void MaybeStartWalReceiver(void);
446450
static void InitPostmasterDeathWatchHandle(void);
447451

448452
#ifdef EXEC_BACKEND
@@ -1766,6 +1770,10 @@ ServerLoop(void)
17661770
kill(AutoVacPID, SIGUSR2);
17671771
}
17681772

1773+
/* If we need to start a WAL receiver, try to do that now */
1774+
if (WalReceiverRequested)
1775+
MaybeStartWalReceiver();
1776+
17691777
/* Get other worker processes running, if needed */
17701778
if (StartWorkerNeeded || HaveCrashedWorker)
17711779
maybe_start_bgworker();
@@ -2848,7 +2856,8 @@ reaper(SIGNAL_ARGS)
28482856
/*
28492857
* Was it the wal receiver? If exit status is zero (normal) or one
28502858
* (FATAL exit), we assume everything is all right just like normal
2851-
* backends.
2859+
* backends. (If we need a new wal receiver, we'll start one at the
2860+
* next iteration of the postmaster's main loop.)
28522861
*/
28532862
if (pid == WalReceiverPID)
28542863
{
@@ -4896,14 +4905,12 @@ sigusr1_handler(SIGNAL_ARGS)
48964905
StartAutovacuumWorker();
48974906
}
48984907

4899-
if (CheckPostmasterSignal(PMSIGNAL_START_WALRECEIVER) &&
4900-
WalReceiverPID == 0 &&
4901-
(pmState == PM_STARTUP || pmState == PM_RECOVERY ||
4902-
pmState == PM_HOT_STANDBY || pmState == PM_WAIT_READONLY) &&
4903-
Shutdown == NoShutdown)
4908+
if (CheckPostmasterSignal(PMSIGNAL_START_WALRECEIVER))
49044909
{
49054910
/* Startup Process wants us to start the walreceiver process. */
4906-
WalReceiverPID = StartWalReceiver();
4911+
/* Start immediately if possible, else remember request for later. */
4912+
WalReceiverRequested = true;
4913+
MaybeStartWalReceiver();
49074914
}
49084915

49094916
if (CheckPostmasterSignal(PMSIGNAL_ADVANCE_STATE_MACHINE) &&
@@ -5279,6 +5286,24 @@ StartAutovacuumWorker(void)
52795286
}
52805287
}
52815288

5289+
/*
5290+
* MaybeStartWalReceiver
5291+
* Start the WAL receiver process, if not running and our state allows.
5292+
*/
5293+
static void
5294+
MaybeStartWalReceiver(void)
5295+
{
5296+
if (WalReceiverPID == 0 &&
5297+
(pmState == PM_STARTUP || pmState == PM_RECOVERY ||
5298+
pmState == PM_HOT_STANDBY || pmState == PM_WAIT_READONLY) &&
5299+
Shutdown == NoShutdown)
5300+
{
5301+
WalReceiverPID = StartWalReceiver();
5302+
WalReceiverRequested = false;
5303+
}
5304+
}
5305+
5306+
52825307
/*
52835308
* Create the opts file
52845309
*/

0 commit comments

Comments
 (0)