Skip to content

Commit 37dc228

Browse files
committed
Fix autovacuum launcher shutdown sequence
It was previously possible to have the launcher re-execute its main loop before shutting down if some other signal was received or an error occurred after getting SIGTERM, as reported by Qingqing Zhou. While investigating, Tom Lane further noticed that if autovacuum had been disabled in the config file, it would misbehave by trying to start a new worker instead of bailing out immediately -- it would consider itself as invoked in emergency mode. Fix both problems by checking the shutdown flag in a few more places. These problems have existed since autovacuum was introduced, so backpatch all the way back.
1 parent 6a560f5 commit 37dc228

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/backend/postmaster/autovacuum.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,10 @@ AutoVacLauncherMain(int argc, char *argv[])
518518
/* Now we can allow interrupts again */
519519
RESUME_INTERRUPTS();
520520

521+
/* if in shutdown mode, no need for anything further; just go away */
522+
if (got_SIGTERM)
523+
goto shutdown;
524+
521525
/*
522526
* Sleep at least 1 second after any error. We don't want to be
523527
* filling the error logs as fast as we can.
@@ -552,10 +556,14 @@ AutoVacLauncherMain(int argc, char *argv[])
552556
SetConfigOption("default_transaction_isolation", "read committed",
553557
PGC_SUSET, PGC_S_OVERRIDE);
554558

555-
/* in emergency mode, just start a worker and go away */
559+
/*
560+
* In emergency mode, just start a worker (unless shutdown was requested)
561+
* and go away.
562+
*/
556563
if (!AutoVacuumingActive())
557564
{
558-
do_start_worker();
565+
if (!got_SIGTERM)
566+
do_start_worker();
559567
proc_exit(0); /* done */
560568
}
561569

@@ -570,7 +578,8 @@ AutoVacLauncherMain(int argc, char *argv[])
570578
*/
571579
rebuild_database_list(InvalidOid);
572580

573-
for (;;)
581+
/* loop until shutdown request */
582+
while (!got_SIGTERM)
574583
{
575584
struct timeval nap;
576585
TimestampTz current_time = 0;
@@ -765,6 +774,7 @@ AutoVacLauncherMain(int argc, char *argv[])
765774
}
766775

767776
/* Normal exit from the autovac launcher is here */
777+
shutdown:
768778
ereport(LOG,
769779
(errmsg("autovacuum launcher shutting down")));
770780
AutoVacuumShmem->av_launcherpid = 0;

0 commit comments

Comments
 (0)