Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r7jub-006Z06-MB for pgsql-hackers@arkaria.postgresql.org; Mon, 27 Nov 2023 22:17:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1r7jua-00H8wC-1g for pgsql-hackers@arkaria.postgresql.org; Mon, 27 Nov 2023 22:17:04 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r7ju3-00H6RC-4W for pgsql-hackers@lists.postgresql.org; Mon, 27 Nov 2023 22:16:31 +0000 Received: from mail-il1-x12e.google.com ([2607:f8b0:4864:20::12e]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1r7ju0-007fZL-Cq for pgsql-hackers@postgresql.org; Mon, 27 Nov 2023 22:16:29 +0000 Received: by mail-il1-x12e.google.com with SMTP id e9e14a558f8ab-35942cb9ef4so16309235ab.3 for ; Mon, 27 Nov 2023 14:16:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701123387; x=1701728187; darn=postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=G7yzP5dUOS0o61Rbx/14eNkcu9Sp+ocKfT8yfrs+UDc=; b=fbDSP7IrEqq43N5uQaIfOKPRBJB/WNxWDXIT85rPA5NUGZ3PQZ1wdKok66r03kfr+0 HJE5rVmAqV0A1C8YOUPGhDa43Z3Pd47JAiloXB3LunQfvukbsxEplQRS4V9pjXl3gpUN bREUYRvMQmrvr7e4TH4kU+423p8mp2JjCqu6LdtkCkZUkvHmlALkKU4VgqcEXztXGgMx JlGmGwoFfHqp7Ry0thKSW2F5lM8PfDboXuitigzN8jFGPtzh5JsQhsQ5OMy7zj3ChvcO qz6TvaRfmfM6A4Ke6SawZoNKL1cMKktwN7DPtHlHk6FTvl+LqVwfYoqz2TBZk1vuH0++ SgEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701123387; x=1701728187; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=G7yzP5dUOS0o61Rbx/14eNkcu9Sp+ocKfT8yfrs+UDc=; b=h5pL7nDD6b3fm61R97o6xudLQnhLWHD7aXMz/YDDJ279Xep/mlhxaQ41XOBrc/cV8m yc+Uz9zrMEAkQZ+5wlAwLxofvg9tjv/NhXWoIp57rSaBTddCbktYMADMWA4xuL1z+tTF PRlb/fEs+GR2TddVLzfI5rmULaOcS/u5xcgxCWwJ17aKJyQOIfLeWHMqEV9yQE069bS1 rJVQTVjsNuq+ViSEfzsN4KfoQ9trGKAM+wDKZw87FpOgomq9lqH0u/iARJ3EwfBcFzkV r6TkDD6JKY3d5xXTVaJ00JTUlzJH/GkRGsWGZHFeFhY5/O+tLri8WR7fbHtncbmyFOcs C8fQ== X-Gm-Message-State: AOJu0YyvVivgDG9PaMxOs3LydoiSAFRZaNRTFv2Lv9+IGojVwkefFeTa ceUhHz9ehw5OvptqK3H/BEuY+yHUX0k= X-Google-Smtp-Source: AGHT+IEt1LE96H50slRipd6EjLR5oAUSBIj4enKwQQlV6I5pPvh8dKF62J6guLmdolNFfd23JHIw7A== X-Received: by 2002:a92:c603:0:b0:35c:debc:94f8 with SMTP id p3-20020a92c603000000b0035cdebc94f8mr4399447ilm.10.1701123387569; Mon, 27 Nov 2023 14:16:27 -0800 (PST) Received: from nathanxps13 (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id y11-20020a056e02118b00b0035c86473e60sm1965239ili.49.2023.11.27.14.16.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 14:16:27 -0800 (PST) Date: Mon, 27 Nov 2023 16:16:25 -0600 From: Nathan Bossart To: Andres Freund Cc: pgsql-hackers@postgresql.org, noah@leadboat.com Subject: Re: common signal handler protection Message-ID: <20231127221625.GB140989@nathanxps13> References: <20231121212008.GA3742740@nathanxps13> <20231121224006.GA3747740@nathanxps13> <20231122215944.GA3797360@nathanxps13> <20231122225945.3kgclsgz5lqmtnan@awork3.anarazel.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231122225945.3kgclsgz5lqmtnan@awork3.anarazel.de> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Thanks for taking a look. On Wed, Nov 22, 2023 at 02:59:45PM -0800, Andres Freund wrote: > On 2023-11-22 15:59:44 -0600, Nathan Bossart wrote: >> +/* >> + * Except when called with SIG_IGN or SIG_DFL, pqsignal() sets up this function >> + * as the handler for all signals. This wrapper handler function checks that >> + * it is called within a process that the server knows about, and not a >> + * grandchild process forked by system(3), etc. > > Similar comment to earlier - the grandchildren bit seems like a dangling > reference. And also too specific - I think we could encounter this in single > user mode as well? > > Perhaps it could be reframed to "postgres processes, as determined by having > called InitProcessGlobals()"? Eh, apparently my attempt at being clever didn't pan out. I like your idea of specifying InitProcessGlobals(), but I might also include "e.g., client backends", too. > Hm. I wonder if this indicates an issue. Do the preceding changes perhaps > make it more likely that a child process of the startup process could hang > around for longer, because the signal we're delivering doesn't terminate child > processes, because we'd just reset the signal handler? I think it's fine for > the startup process, because we ask the startup process to shut down with > SIGTERM, which defaults to exiting. > > But we do have a few processes that we do ask to shut down with other > signals, wich do not trigger an exit by default, e.g. Checkpointer, archiver, > walsender are asked to shut down using SIGUSR2 IIRC. The only one where that > could be an issue is archiver, I guess? This did cross my mind. AFAICT most default handlers already trigger an exit (including SIGUSR2), and for the ones that don't, we'd just end up in the same situation as if the signal arrived a moment later after the child process has installed its own handlers. And postmaster doesn't send certain signals (e.g., SIGHUP) to the whole process group, so we don't have the opposite problem where things like reloading configuration files terminates these child processes. So, I didn't notice any potential issues. Did you have anything else in mind? >> - /* not safe if forked by system(), etc. */ >> - if (MyProc->pid != (int) getpid()) >> - elog(PANIC, "AuxiliaryProcKill() called in child process"); >> - >> auxproc = &AuxiliaryProcs[proctype]; >> >> Assert(MyProc == auxproc); > > I think we should leave these checks. It's awful to debug situations where a > proc gets reused and the cost of the checks is irrelevant. The checks don't > just protect against child processes, they also protect e.g. against calling > those functions twice (we IIRC had cases of that). Sure, that's no problem. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com