Skip to content

Commit 47fec42

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 fc4da1f commit 47fec42

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
@@ -351,6 +351,9 @@ static volatile sig_atomic_t start_autovac_launcher = false;
351351
/* the launcher needs to be signalled to communicate some condition */
352352
static volatile bool avlauncher_needs_signal = false;
353353

354+
/* received START_WALRECEIVER signal */
355+
static volatile sig_atomic_t WalReceiverRequested = false;
356+
354357
/* set when there's a worker that needs to be started up */
355358
static volatile bool StartWorkerNeeded = true;
356359
static volatile bool HaveCrashedWorker = false;
@@ -416,6 +419,7 @@ static void maybe_start_bgworker(void);
416419
static bool CreateOptsFile(int argc, char *argv[], char *fullprogname);
417420
static pid_t StartChildProcess(AuxProcType type);
418421
static void StartAutovacuumWorker(void);
422+
static void MaybeStartWalReceiver(void);
419423
static void InitPostmasterDeathWatchHandle(void);
420424

421425
#ifdef EXEC_BACKEND
@@ -1762,6 +1766,10 @@ ServerLoop(void)
17621766
kill(AutoVacPID, SIGUSR2);
17631767
}
17641768

1769+
/* If we need to start a WAL receiver, try to do that now */
1770+
if (WalReceiverRequested)
1771+
MaybeStartWalReceiver();
1772+
17651773
/* Get other worker processes running, if needed */
17661774
if (StartWorkerNeeded || HaveCrashedWorker)
17671775
maybe_start_bgworker();
@@ -2876,7 +2884,8 @@ reaper(SIGNAL_ARGS)
28762884
/*
28772885
* Was it the wal receiver? If exit status is zero (normal) or one
28782886
* (FATAL exit), we assume everything is all right just like normal
2879-
* backends.
2887+
* backends. (If we need a new wal receiver, we'll start one at the
2888+
* next iteration of the postmaster's main loop.)
28802889
*/
28812890
if (pid == WalReceiverPID)
28822891
{
@@ -5022,14 +5031,12 @@ sigusr1_handler(SIGNAL_ARGS)
50225031
StartAutovacuumWorker();
50235032
}
50245033

5025-
if (CheckPostmasterSignal(PMSIGNAL_START_WALRECEIVER) &&
5026-
WalReceiverPID == 0 &&
5027-
(pmState == PM_STARTUP || pmState == PM_RECOVERY ||
5028-
pmState == PM_HOT_STANDBY || pmState == PM_WAIT_READONLY) &&
5029-
Shutdown == NoShutdown)
5034+
if (CheckPostmasterSignal(PMSIGNAL_START_WALRECEIVER))
50305035
{
50315036
/* Startup Process wants us to start the walreceiver process. */
5032-
WalReceiverPID = StartWalReceiver();
5037+
/* Start immediately if possible, else remember request for later. */
5038+
WalReceiverRequested = true;
5039+
MaybeStartWalReceiver();
50335040
}
50345041

50355042
if (CheckPostmasterSignal(PMSIGNAL_ADVANCE_STATE_MACHINE) &&
@@ -5410,6 +5417,24 @@ StartAutovacuumWorker(void)
54105417
}
54115418
}
54125419

5420+
/*
5421+
* MaybeStartWalReceiver
5422+
* Start the WAL receiver process, if not running and our state allows.
5423+
*/
5424+
static void
5425+
MaybeStartWalReceiver(void)
5426+
{
5427+
if (WalReceiverPID == 0 &&
5428+
(pmState == PM_STARTUP || pmState == PM_RECOVERY ||
5429+
pmState == PM_HOT_STANDBY || pmState == PM_WAIT_READONLY) &&
5430+
Shutdown == NoShutdown)
5431+
{
5432+
WalReceiverPID = StartWalReceiver();
5433+
WalReceiverRequested = false;
5434+
}
5435+
}
5436+
5437+
54135438
/*
54145439
* Create the opts file
54155440
*/

0 commit comments

Comments
 (0)