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 1pNoju-0004zU-AU for pgsql-hackers@arkaria.postgresql.org; Fri, 03 Feb 2023 05:35:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pNojr-0008Vw-Ry for pgsql-hackers@arkaria.postgresql.org; Fri, 03 Feb 2023 05:35:55 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pNojr-0008U2-FQ for pgsql-hackers@lists.postgresql.org; Fri, 03 Feb 2023 05:35:55 +0000 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pNojo-0008Si-V5 for pgsql-hackers@lists.postgresql.org; Fri, 03 Feb 2023 05:35:55 +0000 Received: by mail-pl1-x636.google.com with SMTP id m2so4225281plg.4 for ; Thu, 02 Feb 2023 21:35:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=nEzeT73gvShkAOhFYHqSQXhTBWzK3zT0TtAC0nidja8=; b=l7T/GUsxvZUBOfjVBHxv07BXlEWSDsauq0ABHdGZUF03MP4Cg49PdOxGBT4H4sGaGg xvqZiRQJMzaryoLi5Y8XJSWf/RSzQxMuHAO5tG2+aIEn3rlNOEkEsAX29m7Wj3qqW+PV vuktw0Wk5XFOpfwKi0XUL0LRoQgwHPKPGRGrd7zv/yii421oLLxhxYx2RcidWUhilsqs Dtrcr7ZHXJcilj+m4j3yw1lH+coJpk5jzPrUbhttQDiHcTP1VlK+yx/feR4NMYYpDUOF pW5gxQCw4QurpAD7JzsFxvAptoCpCAiJbbIEr8Rzv9j23hFWHhiH2Ly8ctxVp+sxwCEi o4ZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-transfer-encoding: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=nEzeT73gvShkAOhFYHqSQXhTBWzK3zT0TtAC0nidja8=; b=OW9JaeajC71Rrz2kIKyHyjj3CGQhLpq+IdQL53+poMXrxo8RMZkoTD4hulemSfoYiD Z2G4oCtQ8kF5sic7dJdtk6Lte2ahWKqy2F9B6PT8qw7Jp0qmesnpKnbUIGd6lrvWVCOz B7cHFpD2rZnc35WruNZue+njw1fOFsBrbCDcB79gwFP1v0cbKT+5z+vREefWGCv8Xvg1 gWdJnxBZjne2nv7WRdZrkRpluRcR4oab0pd6cTVpv4OKlAED5N4Ggb1HiqMYumiFSry6 M79t3F6pDqbjMWyxG8hLWFjsxmqlJciuV/yCGScz94pG9HGmRuWE1J8ONgkm7owh+rU/ 3XpQ== X-Gm-Message-State: AO0yUKU82iOqLW0jYFBM/KHY3P8ZvAC/fgsgdrN+DlN3rMGdDKWCmqUW wvu4FUZ0gaHpisS8CehNnF8= X-Google-Smtp-Source: AK7set+I4kTl85coK1JRw5wXrY6rtNk5GTPndZdvFv2cONfHwgbqA0BCrGmX4gNAcGFBXdpLt2r0bg== X-Received: by 2002:a17:903:1390:b0:196:7df6:2d3b with SMTP id jx16-20020a170903139000b001967df62d3bmr7498897plb.2.1675402551002; Thu, 02 Feb 2023 21:35:51 -0800 (PST) Received: from nathanxps13 ([50.47.162.83]) by smtp.gmail.com with ESMTPSA id q16-20020a170902dad000b001948af092d0sm617515plx.152.2023.02.02.21.35.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Feb 2023 21:35:49 -0800 (PST) Date: Thu, 2 Feb 2023 21:35:48 -0800 From: Nathan Bossart To: Robert Haas Cc: Michael Paquier , Tom Lane , Andres Freund , Thomas Munro , Fujii Masao , Postgres hackers Subject: Re: Weird failure with latches in curculio on v15 Message-ID: <20230203053548.GA27055@nathanxps13> References: <20230201165801.33ydbxvjdbomjqa7@alap3.anarazel.de> <20230201175806.GA3199959@nathanxps13> <20230201223555.GA3721373@nathanxps13> <1449633.1675305284@sss.pgh.pa.us> <20230202200957.GA3944544@nathanxps13> <20230202220113.GA3945808@nathanxps13> <20230202223919.GA3947443@nathanxps13> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="nFreZHaLTZJo0R7j" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20230202223919.GA3947443@nathanxps13> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --nFreZHaLTZJo0R7j Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit On Thu, Feb 02, 2023 at 02:39:19PM -0800, Nathan Bossart wrote: > Maybe we could just > remove this exit-in-SIGTERM-handler business... I've spent some time testing this. It seems to work pretty well, but only if I keep the exit-on-SIGTERM logic in shell_restore(). Without that, I'm seeing delayed shutdowns, which I assume means HandleStartupProcInterrupts() isn't getting called (I'm still investigating this). Іn any case, the fact that shell_restore() exits if the command fails due to SIGTERM seems like an implementation detail that we won't necessarily want to rely on once recovery modules are available. In short, we seem to depend on the SIGTERM handling in RestoreArchivedFile() in order to be responsive to shutdown requests. One idea I have is to approximate the current behavior by simply checking for the shutdown_requested flag before before and after executing restore_command. This seems to work as desired even if the exit-on-SIGTERM logic is removed from shell_restore(). Unless there is some reason to break out of system() (versus just waiting for the command to fail after it receives SIGTERM), I think this approach should suffice. I've attached a draft patch. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --nFreZHaLTZJo0R7j Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="adjust_sigterm_handling.patch" diff --git a/src/backend/access/transam/xlogarchive.c b/src/backend/access/transam/xlogarchive.c index 4b89addf97..56c8bf8c18 100644 --- a/src/backend/access/transam/xlogarchive.c +++ b/src/backend/access/transam/xlogarchive.c @@ -148,16 +148,17 @@ RestoreArchivedFile(char *path, const char *xlogfname, XLogFileName(lastRestartPointFname, 0, 0L, wal_segment_size); /* - * Check signals before restore command and reset afterwards. + * Check for pending shutdown requests before and after executing + * restore_command and exit if there is one. */ - PreRestoreCommand(); + HandleStartupProcShutdownRequest(); /* * Copy xlog from archival storage to XLOGDIR */ ret = shell_restore(xlogfname, xlogpath, lastRestartPointFname); - PostRestoreCommand(); + HandleStartupProcShutdownRequest(); if (ret) { diff --git a/src/backend/postmaster/startup.c b/src/backend/postmaster/startup.c index bcd23542f1..30c67a670c 100644 --- a/src/backend/postmaster/startup.c +++ b/src/backend/postmaster/startup.c @@ -55,12 +55,6 @@ static volatile sig_atomic_t got_SIGHUP = false; static volatile sig_atomic_t shutdown_requested = false; static volatile sig_atomic_t promote_signaled = false; -/* - * Flag set when executing a restore command, to tell SIGTERM signal handler - * that it's safe to just proc_exit. - */ -static volatile sig_atomic_t in_restore_command = false; - /* * Time at which the most recent startup operation started. */ @@ -120,10 +114,7 @@ StartupProcShutdownHandler(SIGNAL_ARGS) { int save_errno = errno; - if (in_restore_command) - proc_exit(1); - else - shutdown_requested = true; + shutdown_requested = true; WakeupRecovery(); errno = save_errno; @@ -183,8 +174,7 @@ HandleStartupProcInterrupts(void) /* * Check if we were requested to exit without finishing recovery. */ - if (shutdown_requested) - proc_exit(1); + HandleStartupProcShutdownRequest(); /* * Emergency bailout if postmaster has died. This is to avoid the @@ -273,26 +263,16 @@ StartupProcessMain(void) proc_exit(0); } +/* + * Exit if there is a pending shutdown request. + */ void -PreRestoreCommand(void) +HandleStartupProcShutdownRequest(void) { - /* - * Set in_restore_command to tell the signal handler that we should exit - * right away on SIGTERM. We know that we're at a safe point to do that. - * Check if we had already received the signal, so that we don't miss a - * shutdown request received just before this. - */ - in_restore_command = true; if (shutdown_requested) proc_exit(1); } -void -PostRestoreCommand(void) -{ - in_restore_command = false; -} - bool IsPromoteSignaled(void) { diff --git a/src/include/postmaster/startup.h b/src/include/postmaster/startup.h index dd957f9291..fc20eea1d4 100644 --- a/src/include/postmaster/startup.h +++ b/src/include/postmaster/startup.h @@ -27,8 +27,7 @@ extern PGDLLIMPORT int log_startup_progress_interval; extern void HandleStartupProcInterrupts(void); extern void StartupProcessMain(void) pg_attribute_noreturn(); -extern void PreRestoreCommand(void); -extern void PostRestoreCommand(void); +extern void HandleStartupProcShutdownRequest(void); extern bool IsPromoteSignaled(void); extern void ResetPromoteSignaled(void); --nFreZHaLTZJo0R7j--