Skip to content

Commit 53463e2

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 bdc3e95 commit 53463e2

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
@@ -528,6 +528,35 @@ PostmasterMain(int argc, char *argv[])
528528
/* Initialize paths to installation files */
529529
getInstallationPaths(argv[0]);
530530

531+
/*
532+
* Set up signal handlers for the postmaster process.
533+
*
534+
* CAUTION: when changing this list, check for side-effects on the signal
535+
* handling setup of child processes. See tcop/postgres.c,
536+
* bootstrap/bootstrap.c, postmaster/bgwriter.c, postmaster/walwriter.c,
537+
* postmaster/autovacuum.c, postmaster/pgarch.c, postmaster/pgstat.c,
538+
* postmaster/syslogger.c and postmaster/checkpointer.c.
539+
*/
540+
pqinitmask();
541+
PG_SETMASK(&BlockSig);
542+
543+
pqsignal(SIGHUP, SIGHUP_handler); /* reread config file and have
544+
* children do same */
545+
pqsignal(SIGINT, pmdie); /* send SIGTERM and shut down */
546+
pqsignal(SIGQUIT, pmdie); /* send SIGQUIT and die */
547+
pqsignal(SIGTERM, pmdie); /* wait for children and shut down */
548+
pqsignal(SIGALRM, SIG_IGN); /* ignored */
549+
pqsignal(SIGPIPE, SIG_IGN); /* ignored */
550+
pqsignal(SIGUSR1, sigusr1_handler); /* message from child process */
551+
pqsignal(SIGUSR2, dummy_handler); /* unused, reserve for children */
552+
pqsignal(SIGCHLD, reaper); /* handle child termination */
553+
pqsignal(SIGTTIN, SIG_IGN); /* ignored */
554+
pqsignal(SIGTTOU, SIG_IGN); /* ignored */
555+
/* ignore SIGXFSZ, so that ulimit violations work like disk full */
556+
#ifdef SIGXFSZ
557+
pqsignal(SIGXFSZ, SIG_IGN); /* ignored */
558+
#endif
559+
531560
/*
532561
* Options setup
533562
*/
@@ -1031,35 +1060,6 @@ PostmasterMain(int argc, char *argv[])
10311060
progname, external_pid_file, strerror(errno));
10321061
}
10331062

1034-
/*
1035-
* Set up signal handlers for the postmaster process.
1036-
*
1037-
* CAUTION: when changing this list, check for side-effects on the signal
1038-
* handling setup of child processes. See tcop/postgres.c,
1039-
* bootstrap/bootstrap.c, postmaster/bgwriter.c, postmaster/walwriter.c,
1040-
* postmaster/autovacuum.c, postmaster/pgarch.c, postmaster/pgstat.c,
1041-
* postmaster/syslogger.c and postmaster/checkpointer.c.
1042-
*/
1043-
pqinitmask();
1044-
PG_SETMASK(&BlockSig);
1045-
1046-
pqsignal(SIGHUP, SIGHUP_handler); /* reread config file and have
1047-
* children do same */
1048-
pqsignal(SIGINT, pmdie); /* send SIGTERM and shut down */
1049-
pqsignal(SIGQUIT, pmdie); /* send SIGQUIT and die */
1050-
pqsignal(SIGTERM, pmdie); /* wait for children and shut down */
1051-
pqsignal(SIGALRM, SIG_IGN); /* ignored */
1052-
pqsignal(SIGPIPE, SIG_IGN); /* ignored */
1053-
pqsignal(SIGUSR1, sigusr1_handler); /* message from child process */
1054-
pqsignal(SIGUSR2, dummy_handler); /* unused, reserve for children */
1055-
pqsignal(SIGCHLD, reaper); /* handle child termination */
1056-
pqsignal(SIGTTIN, SIG_IGN); /* ignored */
1057-
pqsignal(SIGTTOU, SIG_IGN); /* ignored */
1058-
/* ignore SIGXFSZ, so that ulimit violations work like disk full */
1059-
#ifdef SIGXFSZ
1060-
pqsignal(SIGXFSZ, SIG_IGN); /* ignored */
1061-
#endif
1062-
10631063
/*
10641064
* If enabled, start up syslogger collection subprocess
10651065
*/

0 commit comments

Comments
 (0)