Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kfMUF-0006ga-T3 for pgsql-hackers@arkaria.postgresql.org; Wed, 18 Nov 2020 12:22:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kfMUE-0001YB-GO for pgsql-hackers@arkaria.postgresql.org; Wed, 18 Nov 2020 12:22:58 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kfMUE-0001Y3-7q for pgsql-hackers@lists.postgresql.org; Wed, 18 Nov 2020 12:22:58 +0000 Received: from oss.nttdata.com ([49.212.34.109]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kfMUB-0000GB-B5 for pgsql-hackers@postgresql.org; Wed, 18 Nov 2020 12:22:57 +0000 Received: from hnk.local (p1295030-ipngn4902funabasi.chiba.ocn.ne.jp [153.166.85.30]) by oss.nttdata.com (Postfix) with ESMTPSA id 192E362589; Wed, 18 Nov 2020 21:22:51 +0900 (JST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.102.3 at oss.nttdata.com Subject: Re: Use standard SIGHUP and SIGTERM handlers in autoprewarm module To: Bharath Rupireddy , Craig Ringer Cc: PostgreSQL-development , Robert Haas References: <13156732-5189-af54-3d28-e2ee2a8568e0@oss.nttdata.com> <069fb3bc-3907-4d85-19fd-e4b2d2f004b4@oss.nttdata.com> From: Fujii Masao Message-ID: <1ae40b1b-2f42-4510-1514-40bb7d7255ab@oss.nttdata.com> Date: Wed, 18 Nov 2020 21:22:50 +0900 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0) Gecko/20100101 Thunderbird/78.4.3 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2020/11/17 21:18, Bharath Rupireddy wrote: > Thanks Craig! > > On Fri, Oct 23, 2020 at 9:37 AM Craig Ringer > wrote: >> >> src/test/modules/test_shm_mq/worker.c appears to do the right thing the wrong way - it has its own custom handler instead of using die() or SignalHandlerForShutdownRequest(). >> > > handle_sigterm() and die() are similar except that die() has extra > handling(below) for exiting immediately when waiting for input/command > from the client. > /* > * If we're in single user mode, we want to quit immediately - we can't > * rely on latches as they wouldn't work when stdin/stdout is a file. > * Rather ugly, but it's unlikely to be worthwhile to invest much more > * effort just for the benefit of single user mode. > */ > if (DoingCommandRead && whereToSendOutput != DestRemote) > ProcessInterrupts(); > > Having this extra handling is correct for normal backends as they can > connect directly to clients for reading input commands, the above if > condition may become true and ProcessInterrupts() may be called to > handle the SIGTERM immediately. > > Note that DoingCommandRead can never be true in bg workers as it's > being set to true only in normal backend PostgresMain(). If we use > die()(instead of custom SIGTERM handlers such as handle_sigterm()) in > a bg worker/non-backend process, there are no chances that the above > part of the code gets hit and the interrupts are processed > immediately. And also here are the bg worker process that use die() > instead of their own custom handlers: parallel workers, autoprewarm > worker, autovacuum worker, logical replication launcher and apply > worker, wal sender process > > I think we can also use die() instead of handle_sigterm() in > test_shm_mq/worker.c. > > I attached a patch for this change. Thanks for the patch! It looks good to me. BTW, I tried to find the past discussion about why die() was not used, but I could not yet. > >> >> In contrast src/test/modules/worker_spi/worker_spi.c looks plain wrong. Especially since it's quoted as an example of how to do things right. It won't respond to SIGTERM at all while it's executing a query from its queue, no matter how long that query takes or whether it blocks. It can inhibit even postmaster shutdown as a result. >> > > Postmaster sends SIGTERM to all children(backends and bgworkers) for > both smart shutdown(wait for children to end their work, then shut > down.) and fast shutdown(rollback active transactions and shutdown > when they are gone.) For immediate shutdown SIGQUIT is sent to > children.(see pmdie()). > > For each bg worker(so is for worker_spi.c), > SignalHandlerForCrashExit() is set as SIGQUIT handler in > InitPostmasterChild(), which does nothing but exits immediately. We > can not use quickdie() here because as a bg worker we don't have > to/can not send anything to client. We are good with > SignalHandlerForCrashExit() as with all other bg workers. > > Yes, if having worker_spi_sigterm/SignalHandlerForShutdownRequest, > sometimes(as explained above) the worker_spi worker may not respond to > SIGTERM. I think we should be having die() as SIGTERM handler here (as > with normal backend and parallel workers). > > Attaching another patch that has replaced custom SIGTERM handler > worker_spi_sigterm() with die() and custom SIGHUP handler > worker_spi_sighup() with standard SignalHandlerForConfigReload(). This patch also looks good to me. > >> >> I was going to lob off a quick patch to fix this by making both use quickdie() for SIGQUIT and die() for SIGTERM, but after reading for a bit I'm no longer sure what the right choice even is. I'd welcome some opinions. >> > > We can not use quickdie() here because as a bg worker we don't have > to/can not send anything to client. We are good with > SignalHandlerForCrashExit() as with all other bg workers. > > Thoughts? I think you're right. Regards, -- Fujii Masao Advanced Computing Technology Center Research and Development Headquarters NTT DATA CORPORATION