Re: Signals in a BGW

From: Craig Ringer <craig(at)2ndquadrant(dot)com>
To: Chapman Flack <chap(at)anastigmatix(dot)net>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Signals in a BGW
Date: 2017-12-05 01:03:48
Message-ID: CAMsr+YF6JOtWYc_vjLJm0KcEoNLuzaKjy4CY_9rswDKA94vhog@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 5 December 2017 at 00:40, Chapman Flack <chap(at)anastigmatix(dot)net> wrote:

>
> Is there a known, default behavior that some signals will
> have, if I simply BackgroundWorkerUnblockSignals() without customizing?
> Will SIGINT and SIGTERM, for example, have default handlers that
> interact with the volatile flags in miscadmin.h that are used by
> CHECK_FOR_INTERRUPTS, and set the process latch?
>
>
The default handler is bgworker_die ; see src/backend/postmaster/bgworker.c
. I don't know if elog() is safe in a signal handler, but I guess in the
absence of non-reentrant errcontext functions...

pglogical sets up its own handler 'handle_sigterm'. However, it now does
much the same as src/backend/tcop/postgres.c's 'die' function, just without
the single-user mode checks.

void
handle_sigterm(SIGNAL_ARGS)
{
int save_errno = errno;

SetLatch(MyLatch);

if (!proc_exit_inprogress)
{
InterruptPending = true;
ProcDiePending = true;
}

errno = save_errno;
}

so it's not significantly different. We used to have our own signal
handling but it gets seriously messy when you're calling into arbitrary
PostgreSQL code that expects CHECK_FOR_INTERRUPTS() to work.

> I notice that the worker_spi example code customizes SIGHUP
> and SIGTERM, giving them handlers that set a (different, local
> to the module) volatile flag, and set the process latch.
>

IMO it's silly to customise them, and a bad example.

Personally I'd rather change the default bgw handler to 'die', make the
sample bgworkers use CHECK_FOR_INTERRUPTS() properly, and be done with it.

--
Craig Ringer http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2017-12-05 01:11:18 Re: Signals in a BGW
Previous Message Nikita Glukhov 2017-12-05 00:02:32 Re: [HACKERS] compress method for spgist - 2