Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7389aad

Browse files
committed
Use WaitEventSet API for postmaster's event loop.
Switch to a design similar to regular backends, instead of the previous arrangement where signal handlers did non-trivial state management and called fork(). The main changes are: * The postmaster now has its own local latch to wait on. (For now, we don't want other backends setting its latch directly, but that could probably be made to work with more research on robustness.) * The existing signal handlers are cut in two: a handle_pm_XXX() part that just sets pending_pm_XXX flags and the latch, and a process_pm_XXX() part that runs later when the latch is seen. * Signal handlers are now installed with the regular pqsignal() function rather than the special pqsignal_pm() function; historical portability concerns about the effect of SA_RESTART on select() are no longer relevant, and we don't need to block signals anymore. Reviewed-by: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/CA%2BhUKG%2BZ-HpOj1JsO9eWUP%2Bar7npSVinsC_npxSy%2BjdOMsx%3DGg%40mail.gmail.com
1 parent d93d68a commit 7389aad

File tree

8 files changed

+269
-228
lines changed

8 files changed

+269
-228
lines changed

src/backend/libpq/pqcomm.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -683,8 +683,7 @@ Setup_AF_UNIX(const char *sock_path)
683683
* server port. Set port->sock to the FD of the new connection.
684684
*
685685
* ASSUME: that this doesn't need to be non-blocking because
686-
* the Postmaster uses select() to tell when the socket is ready for
687-
* accept().
686+
* the Postmaster waits for the socket to be ready to accept().
688687
*
689688
* RETURNS: STATUS_OK or STATUS_ERROR
690689
*/

src/backend/libpq/pqsignal.c

-40
Original file line numberDiff line numberDiff line change
@@ -97,43 +97,3 @@ pqinitmask(void)
9797
sigdelset(&StartupBlockSig, SIGALRM);
9898
#endif
9999
}
100-
101-
/*
102-
* Set up a postmaster signal handler for signal "signo"
103-
*
104-
* Returns the previous handler.
105-
*
106-
* This is used only in the postmaster, which has its own odd approach to
107-
* signal handling. For signals with handlers, we block all signals for the
108-
* duration of signal handler execution. We also do not set the SA_RESTART
109-
* flag; this should be safe given the tiny range of code in which the
110-
* postmaster ever unblocks signals.
111-
*
112-
* pqinitmask() must have been invoked previously.
113-
*/
114-
pqsigfunc
115-
pqsignal_pm(int signo, pqsigfunc func)
116-
{
117-
struct sigaction act,
118-
oact;
119-
120-
act.sa_handler = func;
121-
if (func == SIG_IGN || func == SIG_DFL)
122-
{
123-
/* in these cases, act the same as pqsignal() */
124-
sigemptyset(&act.sa_mask);
125-
act.sa_flags = SA_RESTART;
126-
}
127-
else
128-
{
129-
act.sa_mask = BlockSig;
130-
act.sa_flags = 0;
131-
}
132-
#ifdef SA_NOCLDSTOP
133-
if (signo == SIGCHLD)
134-
act.sa_flags |= SA_NOCLDSTOP;
135-
#endif
136-
if (sigaction(signo, &act, &oact) < 0)
137-
return SIG_ERR;
138-
return oact.sa_handler;
139-
}

src/backend/postmaster/fork_process.c

+17-1
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,28 @@
1212
#include "postgres.h"
1313

1414
#include <fcntl.h>
15+
#include <signal.h>
1516
#include <time.h>
1617
#include <sys/stat.h>
1718
#include <sys/time.h>
1819
#include <unistd.h>
1920

21+
#include "libpq/pqsignal.h"
2022
#include "postmaster/fork_process.h"
2123

2224
#ifndef WIN32
2325
/*
2426
* Wrapper for fork(). Return values are the same as those for fork():
2527
* -1 if the fork failed, 0 in the child process, and the PID of the
26-
* child in the parent process.
28+
* child in the parent process. Signals are blocked while forking, so
29+
* the child must unblock.
2730
*/
2831
pid_t
2932
fork_process(void)
3033
{
3134
pid_t result;
3235
const char *oomfilename;
36+
sigset_t save_mask;
3337

3438
#ifdef LINUX_PROFILE
3539
struct itimerval prof_itimer;
@@ -51,6 +55,13 @@ fork_process(void)
5155
getitimer(ITIMER_PROF, &prof_itimer);
5256
#endif
5357

58+
/*
59+
* We start postmaster children with signals blocked. This allows them to
60+
* install their own handlers before unblocking, to avoid races where they
61+
* might run the postmaster's handler and miss an important control signal.
62+
* With more analysis this could potentially be relaxed.
63+
*/
64+
sigprocmask(SIG_SETMASK, &BlockSig, &save_mask);
5465
result = fork();
5566
if (result == 0)
5667
{
@@ -103,6 +114,11 @@ fork_process(void)
103114
/* do post-fork initialization for random number generation */
104115
pg_strong_random_init();
105116
}
117+
else
118+
{
119+
/* in parent, restore signal mask */
120+
sigprocmask(SIG_SETMASK, &save_mask, NULL);
121+
}
106122

107123
return result;
108124
}

0 commit comments

Comments
 (0)