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 1pRzPU-0006Y8-GY for pgsql-hackers@arkaria.postgresql.org; Tue, 14 Feb 2023 17:48:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pRzPT-0002Iq-29 for pgsql-hackers@arkaria.postgresql.org; Tue, 14 Feb 2023 17:48:07 +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 1pRzPS-0002Ig-Ie for pgsql-hackers@lists.postgresql.org; Tue, 14 Feb 2023 17:48:06 +0000 Received: from mail-pg1-x530.google.com ([2607:f8b0:4864:20::530]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pRzPL-0000Lz-RS for pgsql-hackers@lists.postgresql.org; Tue, 14 Feb 2023 17:48:05 +0000 Received: by mail-pg1-x530.google.com with SMTP id r18so10727128pgr.12 for ; Tue, 14 Feb 2023 09:47:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=YSlciWXVQrClJFCdwVR9kprZSOtIy77hpT0oNXcOcF8=; b=dKydoK9s9nif3k3ogbYNrCL1kXPe5nKaVfTA63GR53Ekj5/8eZutITf/cFjDiDzbff wihKLFgsHAoHW+NRwQmApT8B6aLOfq5Ua4hmXuTpPclgZ0syz9MwcpeS9Rqa3GcgIre6 XUT0tKYbANuMU92+RS1d+BXXVei7un4zVH2J+qzs0y4DV1R4y7o3vGdrvSz7O+MEX7Qm YXTFS2PZtbUHHACnCUb1D85etXA27YHWyc0Je7MAadGop4cQQrU1+Wser0uLfJtNENSd WsuyMNHTP3MEr0fJlxAZEbHJRf60H5u1bMJ+9d+UsrZ2P6PAJhqlWO7ilpGZSedoUys7 m/5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=YSlciWXVQrClJFCdwVR9kprZSOtIy77hpT0oNXcOcF8=; b=ioWgz7ahqgzCKq+pgGdr+VG52oBOiaQmYF6dipxo1zP4Qmklxm9NuWGTDxwT9/VxDc awJPflOfaHdSMNxBwR5+KDsoxjCy0WZejcDmt9B8NcbmZ0DOZxpiPvXcXWl9dqMCR1HG H9rXeWHhyOGHC6EypthlpEnD6vABOFnaQNfUahR013WZjJJ4sdFIyRtYDe+RXQ1ixJPa ec9kD+tPVAamBM0CVXhn0a/j+f1I5A9sFMEjzarkp6EpM+aVUokZ39AlXVc9mufIJq00 3sVewvHApjRwDjHZV7JQc9psrLSRczRwhFdOOHxMsws6xjw3Our/8MYNfJzRdXCvVNKv nm6Q== X-Gm-Message-State: AO0yUKXCY7SPOqTMDh8WB9Y4OBpZnIxl+4xeagkeWZ+/XMAbXG9uXCJy cWpRVtEZx3HwqmrbXSeItJI= X-Google-Smtp-Source: AK7set/srLJWIM/9PQbpWYWKxo+3Jq88EMy+sL6ZoWZIoyvnAPrVDu8Xs8T1XRykWEK/LoaCmJrhwA== X-Received: by 2002:aa7:955a:0:b0:594:26a7:cbd2 with SMTP id w26-20020aa7955a000000b0059426a7cbd2mr212202pfq.8.1676396878411; Tue, 14 Feb 2023 09:47:58 -0800 (PST) Received: from nathanxps13 ([50.47.162.83]) by smtp.gmail.com with ESMTPSA id e16-20020aa78250000000b005a75d85c0c7sm9998043pfn.51.2023.02.14.09.47.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Feb 2023 09:47:57 -0800 (PST) Date: Tue, 14 Feb 2023 09:47:55 -0800 From: Nathan Bossart To: Andres Freund Cc: Robert Haas , Tom Lane , Michael Paquier , Thomas Munro , Fujii Masao , Postgres hackers Subject: Re: Weird failure with latches in curculio on v15 Message-ID: <20230214174755.GA1348509@nathanxps13> References: <20230208174350.GB451849@nathanxps13> <20230208222554.GB546776@nathanxps13> <20230209002413.GA603595@nathanxps13> <3907196.1675957889@sss.pgh.pa.us> <20230209192952.jvx56yuutlxuvjjf@awork3.anarazel.de> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="opJtzjQTFsWo+cga" Content-Disposition: inline In-Reply-To: <20230209192952.jvx56yuutlxuvjjf@awork3.anarazel.de> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --opJtzjQTFsWo+cga Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Here is a new version of the stopgap/back-branch fix for restore_command. This is more or less a rebased version of v4 with an added stderr message as Andres suggested upthread. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --opJtzjQTFsWo+cga Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v5-0001-stopgap-fix-for-restore_command.patch" From 20edca59834c7755bfddb070fb9db3f59dc6ff96 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Tue, 14 Feb 2023 09:44:53 -0800 Subject: [PATCH v5 1/1] stopgap fix for restore_command --- src/backend/access/transam/xlogarchive.c | 15 +++++++++++---- src/backend/postmaster/startup.c | 20 +++++++++++++++++++- src/backend/storage/ipc/ipc.c | 3 +++ src/backend/storage/lmgr/proc.c | 2 ++ 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/backend/access/transam/xlogarchive.c b/src/backend/access/transam/xlogarchive.c index fcc87ff44f..41684418b6 100644 --- a/src/backend/access/transam/xlogarchive.c +++ b/src/backend/access/transam/xlogarchive.c @@ -159,20 +159,27 @@ RestoreArchivedFile(char *path, const char *xlogfname, (errmsg_internal("executing restore command \"%s\"", xlogRestoreCmd))); + fflush(NULL); + pgstat_report_wait_start(WAIT_EVENT_RESTORE_COMMAND); + /* - * Check signals before restore command and reset afterwards. + * PreRestoreCommand() informs the SIGTERM handler for the startup process + * that it should proc_exit() right away. This is done for the duration of + * the system() call because there isn't a good way to break out while it + * is executing. Since we might call proc_exit() in a signal handler, it + * is best to put any additional logic before or after the + * PreRestoreCommand()/PostRestoreCommand() section. */ PreRestoreCommand(); /* * Copy xlog from archival storage to XLOGDIR */ - fflush(NULL); - pgstat_report_wait_start(WAIT_EVENT_RESTORE_COMMAND); rc = system(xlogRestoreCmd); - pgstat_report_wait_end(); PostRestoreCommand(); + + pgstat_report_wait_end(); pfree(xlogRestoreCmd); if (rc == 0) diff --git a/src/backend/postmaster/startup.c b/src/backend/postmaster/startup.c index efc2580536..de2b56c2fa 100644 --- a/src/backend/postmaster/startup.c +++ b/src/backend/postmaster/startup.c @@ -19,6 +19,8 @@ */ #include "postgres.h" +#include + #include "access/xlog.h" #include "access/xlogrecovery.h" #include "access/xlogutils.h" @@ -121,7 +123,23 @@ StartupProcShutdownHandler(SIGNAL_ARGS) int save_errno = errno; if (in_restore_command) - proc_exit(1); + { + /* + * If we are in a child process (e.g., forked by system() in + * RestoreArchivedFile()), we don't want to call any exit callbacks. + * The parent will take care of that. + */ + if (MyProcPid == (int) getpid()) + proc_exit(1); + else + { + const char msg[] = "StartupProcShutdownHandler() called in child process"; + int rc pg_attribute_unused(); + + rc = write(STDERR_FILENO, msg, sizeof(msg)); + _exit(1); + } + } else shutdown_requested = true; WakeupRecovery(); diff --git a/src/backend/storage/ipc/ipc.c b/src/backend/storage/ipc/ipc.c index 1904d21795..6796cabc3e 100644 --- a/src/backend/storage/ipc/ipc.c +++ b/src/backend/storage/ipc/ipc.c @@ -103,6 +103,9 @@ static int on_proc_exit_index, void proc_exit(int code) { + /* proc_exit() is not safe in forked processes from system(), etc. */ + Assert(MyProcPid == getpid()); + /* Clean up everything that must be cleaned up */ proc_exit_prepare(code); diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c index 22b4278610..ae845e8249 100644 --- a/src/backend/storage/lmgr/proc.c +++ b/src/backend/storage/lmgr/proc.c @@ -805,6 +805,7 @@ ProcKill(int code, Datum arg) dlist_head *procgloballist; Assert(MyProc != NULL); + Assert(MyProcPid == getpid()); /* not safe if forked by system(), etc. */ /* Make sure we're out of the sync rep lists */ SyncRepCleanupAtProcExit(); @@ -925,6 +926,7 @@ AuxiliaryProcKill(int code, Datum arg) PGPROC *proc; Assert(proctype >= 0 && proctype < NUM_AUXILIARY_PROCS); + Assert(MyProcPid == getpid()); /* not safe if forked by system(), etc. */ auxproc = &AuxiliaryProcs[proctype]; -- 2.25.1 --opJtzjQTFsWo+cga--