Skip to content

Commit 2a18b3e

Browse files
committed
Don't launch new child processes after we've been told to shut down.
Once we've received a shutdown signal (SIGINT or SIGTERM), we should not launch any more child processes, even if we get signals requesting such. The normal code path for spawning backends has always understood that, but the postmaster's infrastructure for hot standby and autovacuum didn't get the memo. As reported by Hari Babu in bug #7643, this could lead to failure to shut down at all in some cases, such as when SIGINT is received just before the startup process sends PMSIGNAL_RECOVERY_STARTED: we'd launch a bgwriter and checkpointer, and then those processes would have no idea that they ought to quit. Similarly, launching a new autovacuum worker would result in waiting till it finished before shutting down. Also, switch the order of the code blocks in reaper() that detect startup process crash versus shutdown termination. Once we've sent it a signal, we should not consider that exit(1) is surprising. This is just a cosmetic fix since shutdown occurs correctly anyway, but better not to log a phony complaint about startup process crash. Back-patch to 9.0. Some parts of this might be applicable before that, but given the lack of prior complaints I'm not going to worry too much about older branches.
1 parent eb865db commit 2a18b3e

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,6 +2346,18 @@ reaper(SIGNAL_ARGS)
23462346
{
23472347
StartupPID = 0;
23482348

2349+
/*
2350+
* Startup process exited in response to a shutdown request (or it
2351+
* completed normally regardless of the shutdown request).
2352+
*/
2353+
if (Shutdown > NoShutdown &&
2354+
(EXIT_STATUS_0(exitstatus) || EXIT_STATUS_1(exitstatus)))
2355+
{
2356+
pmState = PM_WAIT_BACKENDS;
2357+
/* PostmasterStateMachine logic does the rest */
2358+
continue;
2359+
}
2360+
23492361
/*
23502362
* Unexpected exit of startup process (including FATAL exit)
23512363
* during PM_STARTUP is treated as catastrophic. There are no
@@ -2360,18 +2372,6 @@ reaper(SIGNAL_ARGS)
23602372
ExitPostmaster(1);
23612373
}
23622374

2363-
/*
2364-
* Startup process exited in response to a shutdown request (or it
2365-
* completed normally regardless of the shutdown request).
2366-
*/
2367-
if (Shutdown > NoShutdown &&
2368-
(EXIT_STATUS_0(exitstatus) || EXIT_STATUS_1(exitstatus)))
2369-
{
2370-
pmState = PM_WAIT_BACKENDS;
2371-
/* PostmasterStateMachine logic does the rest */
2372-
continue;
2373-
}
2374-
23752375
/*
23762376
* After PM_STARTUP, any unexpected exit (including FATAL exit) of
23772377
* the startup process is catastrophic, so kill other children,
@@ -4195,7 +4195,7 @@ sigusr1_handler(SIGNAL_ARGS)
41954195
* first. We don't want to go back to recovery in that case.
41964196
*/
41974197
if (CheckPostmasterSignal(PMSIGNAL_RECOVERY_STARTED) &&
4198-
pmState == PM_STARTUP)
4198+
pmState == PM_STARTUP && Shutdown == NoShutdown)
41994199
{
42004200
/* WAL redo has started. We're out of reinitialization. */
42014201
FatalError = false;
@@ -4210,7 +4210,7 @@ sigusr1_handler(SIGNAL_ARGS)
42104210
pmState = PM_RECOVERY;
42114211
}
42124212
if (CheckPostmasterSignal(PMSIGNAL_BEGIN_HOT_STANDBY) &&
4213-
pmState == PM_RECOVERY)
4213+
pmState == PM_RECOVERY && Shutdown == NoShutdown)
42144214
{
42154215
/*
42164216
* Likewise, start other special children as needed.
@@ -4241,7 +4241,8 @@ sigusr1_handler(SIGNAL_ARGS)
42414241
signal_child(SysLoggerPID, SIGUSR1);
42424242
}
42434243

4244-
if (CheckPostmasterSignal(PMSIGNAL_START_AUTOVAC_LAUNCHER))
4244+
if (CheckPostmasterSignal(PMSIGNAL_START_AUTOVAC_LAUNCHER) &&
4245+
Shutdown == NoShutdown)
42454246
{
42464247
/*
42474248
* Start one iteration of the autovacuum daemon, even if autovacuuming
@@ -4255,7 +4256,8 @@ sigusr1_handler(SIGNAL_ARGS)
42554256
start_autovac_launcher = true;
42564257
}
42574258

4258-
if (CheckPostmasterSignal(PMSIGNAL_START_AUTOVAC_WORKER))
4259+
if (CheckPostmasterSignal(PMSIGNAL_START_AUTOVAC_WORKER) &&
4260+
Shutdown == NoShutdown)
42594261
{
42604262
/* The autovacuum launcher wants us to start a worker process. */
42614263
StartAutovacuumWorker();
@@ -4264,7 +4266,8 @@ sigusr1_handler(SIGNAL_ARGS)
42644266
if (CheckPostmasterSignal(PMSIGNAL_START_WALRECEIVER) &&
42654267
WalReceiverPID == 0 &&
42664268
(pmState == PM_STARTUP || pmState == PM_RECOVERY ||
4267-
pmState == PM_HOT_STANDBY || pmState == PM_WAIT_READONLY))
4269+
pmState == PM_HOT_STANDBY || pmState == PM_WAIT_READONLY) &&
4270+
Shutdown == NoShutdown)
42684271
{
42694272
/* Startup Process wants us to start the walreceiver process. */
42704273
WalReceiverPID = StartWalReceiver();

0 commit comments

Comments
 (0)