Skip to content

Commit 969735c

Browse files
committed
Block signals earlier during postmaster startup.
Formerly, we set up the postmaster's signal handling only when we were about to start launching subprocesses. This is a bad idea though, as it means that for example a SIGINT arriving before that will kill the postmaster instantly, perhaps leaving lockfiles, socket files, shared memory, etc laying about. We'd rather that such a signal caused orderly postmaster termination including releasing of those resources. A simple fix is to move the PostmasterMain stanza that initializes signal handling to an earlier point, before we've created any such resources. Then, an early-arriving signal will be blocked until we're ready to deal with it in the usual way. (The only part that really needs to be moved up is blocking of signals, but it seems best to keep the signal handler installation calls together with that; for one thing this ensures the kernel won't drop any signals we wished to get. The handlers won't get invoked in any case until we unblock signals in ServerLoop.) Per a report from MauMau. He proposed changing the way "pg_ctl stop" works to deal with this, but that'd just be masking one symptom not fixing the core issue. It's been like this since forever, so back-patch to all supported branches.
1 parent 30e434b commit 969735c

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,35 @@ PostmasterMain(int argc, char *argv[])
490490
/* Initialize paths to installation files */
491491
getInstallationPaths(argv[0]);
492492

493+
/*
494+
* Set up signal handlers for the postmaster process.
495+
*
496+
* CAUTION: when changing this list, check for side-effects on the signal
497+
* handling setup of child processes. See tcop/postgres.c,
498+
* bootstrap/bootstrap.c, postmaster/bgwriter.c, postmaster/walwriter.c,
499+
* postmaster/autovacuum.c, postmaster/pgarch.c, postmaster/pgstat.c, and
500+
* postmaster/syslogger.c.
501+
*/
502+
pqinitmask();
503+
PG_SETMASK(&BlockSig);
504+
505+
pqsignal(SIGHUP, SIGHUP_handler); /* reread config file and have
506+
* children do same */
507+
pqsignal(SIGINT, pmdie); /* send SIGTERM and shut down */
508+
pqsignal(SIGQUIT, pmdie); /* send SIGQUIT and die */
509+
pqsignal(SIGTERM, pmdie); /* wait for children and shut down */
510+
pqsignal(SIGALRM, SIG_IGN); /* ignored */
511+
pqsignal(SIGPIPE, SIG_IGN); /* ignored */
512+
pqsignal(SIGUSR1, sigusr1_handler); /* message from child process */
513+
pqsignal(SIGUSR2, dummy_handler); /* unused, reserve for children */
514+
pqsignal(SIGCHLD, reaper); /* handle child termination */
515+
pqsignal(SIGTTIN, SIG_IGN); /* ignored */
516+
pqsignal(SIGTTOU, SIG_IGN); /* ignored */
517+
/* ignore SIGXFSZ, so that ulimit violations work like disk full */
518+
#ifdef SIGXFSZ
519+
pqsignal(SIGXFSZ, SIG_IGN); /* ignored */
520+
#endif
521+
493522
/*
494523
* Options setup
495524
*/
@@ -956,35 +985,6 @@ PostmasterMain(int argc, char *argv[])
956985
progname, external_pid_file, strerror(errno));
957986
}
958987

959-
/*
960-
* Set up signal handlers for the postmaster process.
961-
*
962-
* CAUTION: when changing this list, check for side-effects on the signal
963-
* handling setup of child processes. See tcop/postgres.c,
964-
* bootstrap/bootstrap.c, postmaster/bgwriter.c, postmaster/walwriter.c,
965-
* postmaster/autovacuum.c, postmaster/pgarch.c, postmaster/pgstat.c, and
966-
* postmaster/syslogger.c.
967-
*/
968-
pqinitmask();
969-
PG_SETMASK(&BlockSig);
970-
971-
pqsignal(SIGHUP, SIGHUP_handler); /* reread config file and have
972-
* children do same */
973-
pqsignal(SIGINT, pmdie); /* send SIGTERM and shut down */
974-
pqsignal(SIGQUIT, pmdie); /* send SIGQUIT and die */
975-
pqsignal(SIGTERM, pmdie); /* wait for children and shut down */
976-
pqsignal(SIGALRM, SIG_IGN); /* ignored */
977-
pqsignal(SIGPIPE, SIG_IGN); /* ignored */
978-
pqsignal(SIGUSR1, sigusr1_handler); /* message from child process */
979-
pqsignal(SIGUSR2, dummy_handler); /* unused, reserve for children */
980-
pqsignal(SIGCHLD, reaper); /* handle child termination */
981-
pqsignal(SIGTTIN, SIG_IGN); /* ignored */
982-
pqsignal(SIGTTOU, SIG_IGN); /* ignored */
983-
/* ignore SIGXFSZ, so that ulimit violations work like disk full */
984-
#ifdef SIGXFSZ
985-
pqsignal(SIGXFSZ, SIG_IGN); /* ignored */
986-
#endif
987-
988988
/*
989989
* If enabled, start up syslogger collection subprocess
990990
*/

0 commit comments

Comments
 (0)