Skip to content

Commit 093d3da

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 cb11f4d commit 093d3da

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

517+
/*
518+
* Set up signal handlers for the postmaster process.
519+
*
520+
* CAUTION: when changing this list, check for side-effects on the signal
521+
* handling setup of child processes. See tcop/postgres.c,
522+
* bootstrap/bootstrap.c, postmaster/bgwriter.c, postmaster/walwriter.c,
523+
* postmaster/autovacuum.c, postmaster/pgarch.c, postmaster/pgstat.c, and
524+
* postmaster/syslogger.c.
525+
*/
526+
pqinitmask();
527+
PG_SETMASK(&BlockSig);
528+
529+
pqsignal(SIGHUP, SIGHUP_handler); /* reread config file and have
530+
* children do same */
531+
pqsignal(SIGINT, pmdie); /* send SIGTERM and shut down */
532+
pqsignal(SIGQUIT, pmdie); /* send SIGQUIT and die */
533+
pqsignal(SIGTERM, pmdie); /* wait for children and shut down */
534+
pqsignal(SIGALRM, SIG_IGN); /* ignored */
535+
pqsignal(SIGPIPE, SIG_IGN); /* ignored */
536+
pqsignal(SIGUSR1, sigusr1_handler); /* message from child process */
537+
pqsignal(SIGUSR2, dummy_handler); /* unused, reserve for children */
538+
pqsignal(SIGCHLD, reaper); /* handle child termination */
539+
pqsignal(SIGTTIN, SIG_IGN); /* ignored */
540+
pqsignal(SIGTTOU, SIG_IGN); /* ignored */
541+
/* ignore SIGXFSZ, so that ulimit violations work like disk full */
542+
#ifdef SIGXFSZ
543+
pqsignal(SIGXFSZ, SIG_IGN); /* ignored */
544+
#endif
545+
517546
/*
518547
* Options setup
519548
*/
@@ -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, and
1041-
* postmaster/syslogger.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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy