pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Use SIGURG rather than SIGUSR1 for latches.
9+ messages / 3 participants
[nested] [flat]

Commits from this thread:
  2021-04-18 8e861eaae8  Explain postmaster's treatment of SIGURG.
* pgsql: Use SIGURG rather than SIGUSR1 for latches.
@ 2021-03-01 01:26  Thomas Munro <tmunro@postgresql.org>
  0 siblings, 1 reply; 9+ messages in thread

From: Thomas Munro @ 2021-03-01 01:26 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Use SIGURG rather than SIGUSR1 for latches.

Traditionally, SIGUSR1 has been overloaded for ad-hoc signals,
procsignal.c signals and latch.c wakeups.  Move that last use over to a
new dedicated signal.  SIGURG is normally used to report out-of-band
socket data, but PostgreSQL doesn't use that facility.

The signal handler is now installed in all postmaster children by
InitializeLatchSupport().  Those wishing to disconnect from it should
call ShutdownLatchSupport().

Future patches will use this separation of signals to avoid the need for
a signal handler on some operating systems.

Discussion: https://postgr.es/m/CA+hUKGJjxPDpzBE0a3hyUywBvaZuC89yx3jK9RFZgfv_KHU7gg@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/83709a0d5a46559db016c50ded1a95fd3b0d3be6

Modified Files
--------------
src/backend/postmaster/bgworker.c    | 19 ++------------
src/backend/postmaster/postmaster.c  |  4 +++
src/backend/storage/ipc/latch.c      | 50 ++++++++++++++++++++++++------------
src/backend/storage/ipc/procsignal.c |  2 --
src/include/storage/latch.h          | 11 +-------
5 files changed, 40 insertions(+), 46 deletions(-)



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: pgsql: Use SIGURG rather than SIGUSR1 for latches.
@ 2021-04-15 02:26  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Thomas Munro <tmunro@postgresql.org>
  0 siblings, 1 reply; 9+ messages in thread

From: Tom Lane @ 2021-04-15 02:26 UTC (permalink / raw)
  To: Thomas Munro <tmunro@postgresql.org>; +Cc: pgsql-committers@lists.postgresql.org

Thomas Munro <tmunro@postgresql.org> writes:
> Use SIGURG rather than SIGUSR1 for latches.

I notice that postmaster.c still does

#ifdef SIGURG
    pqsignal_pm(SIGURG, SIG_IGN);          /* ignored */
#endif

It appears to me that this should now read

    pqsignal_pm(SIGURG, dummy_handler);    /* unused, reserve for children */

for the reasons explained in the comment for dummy_handler.

It's possible that that argument doesn't apply to the way SIGURG is used
in this patch, but I don't see a good reason to ignore the convention of
setting up the handler this way.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: pgsql: Use SIGURG rather than SIGUSR1 for latches.
@ 2021-04-15 10:50  Thomas Munro <thomas.munro@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 9+ messages in thread

From: Thomas Munro @ 2021-04-15 10:50 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Thomas Munro <tmunro@postgresql.org>; pgsql-committers <pgsql-committers@lists.postgresql.org>

On Thu, Apr 15, 2021 at 2:26 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> It's possible that that argument doesn't apply to the way SIGURG is used
> in this patch, but I don't see a good reason to ignore the convention of
> setting up the handler this way.

Yeah, will fix.  I don't think there is a bug here given the way
latches use shared memory flags, but it might as well be consistent.





^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: pgsql: Use SIGURG rather than SIGUSR1 for latches.
@ 2021-04-16 04:03  Thomas Munro <thomas.munro@gmail.com>
  parent: Thomas Munro <thomas.munro@gmail.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Thomas Munro @ 2021-04-16 04:03 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-committers <pgsql-committers@lists.postgresql.org>

On Thu, Apr 15, 2021 at 10:50 PM Thomas Munro <thomas.munro@gmail.com> wrote:
> On Thu, Apr 15, 2021 at 2:26 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> > It's possible that that argument doesn't apply to the way SIGURG is used
> > in this patch, but I don't see a good reason to ignore the convention of
> > setting up the handler this way.
>
> Yeah, will fix.  I don't think there is a bug here given the way
> latches use shared memory flags, but it might as well be consistent.

Here's a patch to change that.  But... on second thoughts, and after
coming up with a commit message to explain the change, I'm not 100%
convinced it's worth committing.  You can't get SIGURG without
explicitly asking for it (by setting maybe_sleeping), which makes it a
bit more like SIGALRM than SIGUSR2.  I don't feel very strongly about
this though.  What do you think?

Attachments:

0001-Make-postmaster-s-SIGURG-setup-more-consistent.patch (text/x-patch, 2.0K) download From 74bc13551fcdc208d4a5e386a01776def602251e Mon Sep 17 00:00:00 2001 From: Thomas Munro <thomas.munro@gmail.com> Date: Fri, 16 Apr 2021 15:36:43 +1200 Subject: [PATCH] Make postmaster's SIGURG setup more consistent. Although the only current user of SIGURG isn't at risk of missing an important signal (latch.c doesn't expect SIGURG until it advertises in shmem that it's waiting for it and does even that only after checking that is_set hasn't been set), it's theoretically possible that we could use SIGURG for other purposes in the future. Therefore, give it the same treatment as SIGUSR2, just in case: install a dummy handler before forking, so that any signals are queued until it's unblocked by each child. Reported-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/3739816.1618453573%40sss.pgh.pa.us --- src/backend/postmaster/postmaster.c | 2 +- src/backend/storage/ipc/latch.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index 4a3ca78c1b..a739b461b7 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -660,7 +660,7 @@ PostmasterMain(int argc, char *argv[]) pqsignal_pm(SIGCHLD, reaper); /* handle child termination */ #ifdef SIGURG - pqsignal_pm(SIGURG, SIG_IGN); /* ignored */ + pqsignal_pm(SIGURG, dummy_handler); /* unused, reserve for children */ #endif /* diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index 5f3318fa8f..05b364fa9e 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -268,7 +268,8 @@ InitializeLatchSupport(void) #ifdef WAIT_USE_EPOLL sigset_t signalfd_mask; - /* Block SIGURG, because we'll receive it through a signalfd. */ + /* Ignore SIGURG, and keep it permanently blocked. */ + pqsignal(SIGURG, SIG_IGN); sigaddset(&UnBlockSig, SIGURG); /* Set up the signalfd to receive SIGURG notifications. */ -- 2.30.1
^ permalink raw reply [nested|flat] 9+ messages in thread

* Re: pgsql: Use SIGURG rather than SIGUSR1 for latches.
@ 2021-04-16 12:46  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Thomas Munro <thomas.munro@gmail.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Tom Lane @ 2021-04-16 12:46 UTC (permalink / raw)
  To: Thomas Munro <thomas.munro@gmail.com>; +Cc: pgsql-committers <pgsql-committers@lists.postgresql.org>

Thomas Munro <thomas.munro@gmail.com> writes:
> Here's a patch to change that.  But... on second thoughts, and after
> coming up with a commit message to explain the change, I'm not 100%
> convinced it's worth committing.  You can't get SIGURG without
> explicitly asking for it (by setting maybe_sleeping), which makes it a
> bit more like SIGALRM than SIGUSR2.  I don't feel very strongly about
> this though.  What do you think?

Hmm, yeah, after looking more closely at InitializeLatchSupport I agree.
There's so much platform variability in whether we use the signal handler
at all that it seems best to keep it SIGIGN'd until we reach that code.

It might be good to extend the comment in postmaster.c though, perhaps
along the lines of "Ignore SIGURG for now.  Child processes may change
this (see InitializeLatchSupport), but they will not receive any such
signals until they wait on a latch".

Is it really necessary to mess with UnBlockSig?

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: pgsql: Use SIGURG rather than SIGUSR1 for latches.
@ 2021-04-16 20:38  Thomas Munro <thomas.munro@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 9+ messages in thread

From: Thomas Munro @ 2021-04-16 20:38 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-committers <pgsql-committers@lists.postgresql.org>

On Sat, Apr 17, 2021 at 12:46 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> It might be good to extend the comment in postmaster.c though, perhaps
> along the lines of "Ignore SIGURG for now.  Child processes may change
> this (see InitializeLatchSupport), but they will not receive any such
> signals until they wait on a latch".

Ok, will do.

> Is it really necessary to mess with UnBlockSig?

It's necessary to keep it blocked, because, to quote signalfd(2):

       Normally,  the  set  of  signals to be received via the file descriptor
       should be blocked using sigprocmask(2), to prevent  the  signals  being
       handled according to their default dispositions.

It does seem a little strange to have a sigset_t called UnBlockSig
that leaves one signal blocked, but it still fits this description
(from which I removed the parenthetical question):

  * UnBlockSig is the set of signals to block when we don't want to block
- * signals (is this ever nonzero??)
+ * signals.

There is also clear warning near that:

+       /* Note: InitializeLatchSupport() modifies UnBlockSig. */

I admit that it's a little unpleasant that it does that, but I
couldn't find a better way, considering the dependency on build
options and details known only to latch.c.  In earlier versions I
posted of that patch set, I did consider an alternative where
pqsignal.c would ask latch.c if it should be blocked, but it seemed
uglier.

The kqueue designers made a different choice for EVFILT_SIGNAL:

                         ...  This coexists with the signal() and
                         sigaction() facilities, and has a lower precedence.
                         The filter will record all attempts to deliver a
                         signal to a process, even if the signal has been
                         marked as SIG_IGN, ...

So in kqueue builds, it's not necessary to block it, because SIG_IGN
is enough to redirect the signal to the kqueue (and blocking it would
prevent kqueue from receiving it IIRC).  All the calls to set the
disposition to SIG_IGN explicitly are probably unnecessary since
that's the default disposition, but I figured that was somehow useful
as documentation, and a place to hang a comment.





^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: pgsql: Use SIGURG rather than SIGUSR1 for latches.
@ 2021-04-16 20:49  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Thomas Munro <thomas.munro@gmail.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Tom Lane @ 2021-04-16 20:49 UTC (permalink / raw)
  To: Thomas Munro <thomas.munro@gmail.com>; +Cc: pgsql-committers <pgsql-committers@lists.postgresql.org>

Thomas Munro <thomas.munro@gmail.com> writes:
> On Sat, Apr 17, 2021 at 12:46 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> Is it really necessary to mess with UnBlockSig?

> It's necessary to keep it blocked, because, to quote signalfd(2):

>        Normally,  the  set  of  signals to be received via the file descriptor
>        should be blocked using sigprocmask(2), to prevent  the  signals  being
>        handled according to their default dispositions.

Meh.  OK.

(I would've thought that a SIG_IGN'd signal would be dropped
immediately even if blocked; that's the behavior that dummy_handler
is designed to prevent, and I'm pretty sure that that code is there
because we saw it actually behaving that way on some platforms.
But apparently not on Linux?)

> ... All the calls to set the
> disposition to SIG_IGN explicitly are probably unnecessary since
> that's the default disposition, but I figured that was somehow useful
> as documentation, and a place to hang a comment.

Agreed, I would not suggest removing those.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: pgsql: Use SIGURG rather than SIGUSR1 for latches.
@ 2021-04-17 13:28  Thomas Munro <thomas.munro@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 9+ messages in thread

From: Thomas Munro @ 2021-04-17 13:28 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-committers <pgsql-committers@lists.postgresql.org>

On Sat, Apr 17, 2021 at 8:49 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> (I would've thought that a SIG_IGN'd signal would be dropped
> immediately even if blocked; that's the behavior that dummy_handler
> is designed to prevent, and I'm pretty sure that that code is there
> because we saw it actually behaving that way on some platforms.
> But apparently not on Linux?)

Yeah, it's a point of variation between platforms.

POSIX: "If the action associated with a blocked signal is to ignore
the signal and if that signal is generated for the process, it is
unspecified whether the signal is discarded immediately upon
generation or remains pending."
Linux: "Blocked signals are never ignored, since the signal handler
may change by the time it is unblocked."
BSDs, Darwin: "If the signal is being ignored, then we forget about it
immediately."





^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: pgsql: Use SIGURG rather than SIGUSR1 for latches.
@ 2021-04-18 23:36  Thomas Munro <thomas.munro@gmail.com>
  parent: Thomas Munro <thomas.munro@gmail.com>
  0 siblings, 0 replies; 9+ messages in thread

From: Thomas Munro @ 2021-04-18 23:36 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-committers <pgsql-committers@lists.postgresql.org>

On Sun, Apr 18, 2021 at 1:28 AM Thomas Munro <thomas.munro@gmail.com> wrote:
> Linux: "Blocked signals are never ignored, since the signal handler
> may change by the time it is unblocked."

I should add, that's what the source says.  The man page for
sigpending(2) makes the opposite claim in its NOTES section on my
Debian system (it helpfully describes the BSD behaviour instead), but
the man page is demonstrably wrong.





^ permalink  raw  reply  [nested|flat] 9+ messages in thread


end of thread, other threads:[~2021-04-18 23:36 UTC | newest]

Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-03-01 01:26 pgsql: Use SIGURG rather than SIGUSR1 for latches. Thomas Munro <tmunro@postgresql.org>
2021-04-15 02:26 ` Tom Lane <tgl@sss.pgh.pa.us>
2021-04-15 10:50   ` Thomas Munro <thomas.munro@gmail.com>
2021-04-16 04:03     ` Thomas Munro <thomas.munro@gmail.com>
2021-04-16 12:46       ` Tom Lane <tgl@sss.pgh.pa.us>
2021-04-16 20:38         ` Thomas Munro <thomas.munro@gmail.com>
2021-04-16 20:49           ` Tom Lane <tgl@sss.pgh.pa.us>
2021-04-17 13:28             ` Thomas Munro <thomas.munro@gmail.com>
2021-04-18 23:36               ` Thomas Munro <thomas.munro@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox