Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1id8X6-0004ly-Ig for pgsql-bugs@arkaria.postgresql.org; Fri, 06 Dec 2019 08:00:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1id8X5-0004MM-8s for pgsql-bugs@arkaria.postgresql.org; Fri, 06 Dec 2019 08:00:11 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1id8X4-0004MF-PJ for pgsql-bugs@lists.postgresql.org; Fri, 06 Dec 2019 08:00:10 +0000 Received: from mail-lj1-x244.google.com ([2a00:1450:4864:20::244]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1id8X0-0003pa-4U for pgsql-bugs@lists.postgresql.org; Fri, 06 Dec 2019 08:00:09 +0000 Received: by mail-lj1-x244.google.com with SMTP id a13so6615552ljm.10 for ; Fri, 06 Dec 2019 00:00:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=Q3yh06sK0oDAlfczETGRRoarcZp7BqEqONqfRVhL19E=; b=cAvaGbXHRMB2wytwKHl7sNmUeiewfyzBTrD14ePLlQS+Ai/JBXLIc/fwclk+2wDMXS kOZUBkUzt33XfbDu6igrLeZMdEz8zM7TCUGKTt2Awy2u1sZfzhKB0rW+bM3CY+kEnrJS j1khFBN8OAqEsK6vlnbOqPWBV0OyAFfRsnVfkCqNSIvywchzylC9CJAgt11gBUCjtSFs 6DJOHOJKywefpd2BoMrt7qyTpuovPJME7O62NsAFk4fiam6or6rrNcvckvHr/Tb/lE9q x90XGqr5s2ghgb9H32mk28hM7GRsbKKM4Rv+aQEkQ8SfVldFQBAKF4izLiiUdFEU5+JN nWkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=Q3yh06sK0oDAlfczETGRRoarcZp7BqEqONqfRVhL19E=; b=R/dQGH0KpQvxDyJFRNaXtG8jdUkWgT27CWbUoalVA2b0zpkkMqQsKCfGqhb0Isp7XU YOJrbFQDcFo+8qhoVW1y2QF4OJvSVb2WrAdUqvOZv2eK42IcnzxnGJDwOFFZjsvGsMXg pXlzcvm+46EICZv3BkEYIbg7hZo+aCVoUD0cEU0EJUp7/5zBjwA/954Ib2jyUkAKTE/C QQtBOe/mliw6OwFhZd3VCh3PSZgnO0AmRje3oZWjRfo1JKlD/P4NM8QO6Pj1LkBRyiWg daWn+jGd79fk9yeMmJcBMowYrWmv4vEDhqdFe0TvGdkN9hGkaKp7fYNydjcpbmaG8ct7 INfw== X-Gm-Message-State: APjAAAUEm0gzOvNhEQJOL/5ltJQZ9BiYo3MC9xLBW/4mmZa0HfHtKxF+ 8EH3E5WtOK3BKQGXwmE94wl8P2bl X-Google-Smtp-Source: APXvYqw1sjnctyQWh1br9qYcMfT7jSD4vx8kvGSi8kkfzq9YhAStHITpP/1kdvBFFvdT+kzwDAVAyg== X-Received: by 2002:a2e:3c1a:: with SMTP id j26mr7791803lja.79.1575619203718; Fri, 06 Dec 2019 00:00:03 -0800 (PST) Received: from [1.0.0.7] ([178.155.4.37]) by smtp.gmail.com with ESMTPSA id q26sm2342175lfb.26.2019.12.06.00.00.02 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 06 Dec 2019 00:00:02 -0800 (PST) Subject: Re: BUG #16154: pg_ctl restart with a logfile fails sometimes (on Windows) To: pgsql-bugs@lists.postgresql.org References: <16154-1ccf0b537b24d5e0@postgresql.org> From: Alexander Lakhin Message-ID: Date: Fri, 6 Dec 2019 11:00:01 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: <16154-1ccf0b537b24d5e0@postgresql.org> Content-Type: multipart/mixed; boundary="------------C9227EE100DB1B730994A475" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------C9227EE100DB1B730994A475 Content-Type: multipart/alternative; boundary="------------E676E5EB66EA9606DD0ACF69" --------------E676E5EB66EA9606DD0ACF69 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit 06.12.2019 10:00, PG Bug reporting form wrote: > When performing regression tests on Windows intermittent failures are > observed, e.g. in src/bin/pg_basebackup test: > vcregress taptest src/bin/pg_basebackup > ... > t/010_pg_basebackup.pl ... 10/106 Bailout called. Further testing stopped: > system pg_ctl failed > FAILED--Further testing stopped: system pg_ctl failed > > > waiting for server to start....The process cannot access the file because it > is being used by another process. > stopped waiting > pg_ctl: could not start server > > The issue is caused by sporadic "pg_ctl ... restart -l logfile" failures. To reproduce this issue reliably I propose the simple demo patch (delay_after_unlink_pid p, li { white-space: pre-wrap;). With the delay added the "pg_ctl ... restart -l logfile" command (and "vcregress taptest src/bin/pg_basebackup") fails always. Error message is not very informational, but debugging shows that the file in question is the log file, specified when running the command: "C:\Windows\system32\cmd.exe" /C ""C:/src/postgresql/tmp_install/bin/postgres.exe" -D "C:/src/postgresql/src/bin/pg_basebackup/tmp_check/t_010_pg_basebackup_main_data/pgdata" --cluster-name=main < "nul" >> "/C:/src/postgresql/src/bin/pg_basebackup/tmp_check/log/010_pg_basebackup_main.log/" 2>&1" If this file is still opened by the previous server shell (it can happen when the previous server instance has unlinked it's pid file, but it's CMD shell is still running), the next CMD start fails with the aforementioned error message. To fix this issue I propose the attached patch (fix_logfile_sharing_violation ). With the patch, pg_ctl will wait for the log file to become available (for 30 seconds). And if the file still could not be opened (it can be reproduced with a larger delay in the demo patch), you'll get more meaningful message: /pg_ctl: could not access log file "C:/src/postgresql/src/bin/pg_basebackup/tmp_check/log/010_pg_basebackup_main.log": Permission denied/ Best regards, Alexander --------------E676E5EB66EA9606DD0ACF69 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit
06.12.2019 10:00, PG Bug reporting form wrote:
When performing regression tests on Windows intermittent failures are
observed, e.g. in src/bin/pg_basebackup test:
vcregress taptest src/bin/pg_basebackup
...
t/010_pg_basebackup.pl ... 10/106 Bailout called.  Further testing stopped: 
system pg_ctl failed
FAILED--Further testing stopped: system pg_ctl failed


waiting for server to start....The process cannot access the file because it
is being used by another process.
 stopped waiting
pg_ctl: could not start server

The issue is caused by sporadic "pg_ctl ... restart -l logfile" failures.

To reproduce this issue reliably I propose the simple demo patch (delay_after_unlink_pid p, li { white-space: pre-wrap;).
With the delay added the "pg_ctl ... restart -l logfile" command (and "vcregress taptest src/bin/pg_basebackup") fails always.
Error message is not very informational, but debugging shows that the file in question is the log file, specified when running the command:
"C:\Windows\system32\cmd.exe" /C ""C:/src/postgresql/tmp_install/bin/postgres.exe" -D "C:/src/postgresql/src/bin/pg_basebackup/tmp_check/t_010_pg_basebackup_main_data/pgdata" --cluster-name=main < "nul" >> "C:/src/postgresql/src/bin/pg_basebackup/tmp_check/log/010_pg_basebackup_main.log" 2>&1"

If this file is still opened by the previous server shell (it can happen when the previous server instance has unlinked it's pid file, but it's CMD shell is still running), the next CMD start fails with the aforementioned error message.

To fix this issue I propose the attached patch (fix_logfile_sharing_violation ).
With the patch, pg_ctl will wait for the log file to become available (for 30 seconds). And if the file still could not be opened (it can be reproduced with a larger delay in the demo patch), you'll get more meaningful message:
pg_ctl: could not access log file "C:/src/postgresql/src/bin/pg_basebackup/tmp_check/log/010_pg_basebackup_main.log": Permission denied

Best regards,
Alexander
--------------E676E5EB66EA9606DD0ACF69-- --------------C9227EE100DB1B730994A475 Content-Type: text/x-patch; name="delay_after_unlink_pid.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="delay_after_unlink_pid.patch" diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c index 83c9514856..130d666f33 100644 --- a/src/backend/utils/init/miscinit.c +++ b/src/backend/utils/init/miscinit.c @@ -858,6 +858,7 @@ UnlinkLockFiles(int status, Datum arg) */ ereport(IsPostmasterEnvironment ? LOG : NOTICE, (errmsg("database system is shut down"))); + pg_usleep(1000000L); } /* --------------C9227EE100DB1B730994A475 Content-Type: text/x-patch; name="fix_logfile_sharing_violation.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="fix_logfile_sharing_violation.patch" diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c index 65f9fb4c0a..9bc44b2de4 100644 --- a/src/bin/pg_ctl/pg_ctl.c +++ b/src/bin/pg_ctl/pg_ctl.c @@ -519,8 +519,21 @@ start_postmaster(void) comspec = "CMD"; if (log_file != NULL) + { + /* Check the log file availability to prevent CMD.EXE failing with + * ERROR_SHARING_VIOLATION, e.g. when the log file is still opened + * by the previous server shell. + */ + FILE *fd = fopen(log_file, "w+"); + if (!fd) { + write_stderr(_("%s: could not access log file \"%s\": %s\n"), + progname, log_file, strerror(errno)); + exit(1); + } + fclose(fd); snprintf(cmd, MAXPGPATH, "\"%s\" /C \"\"%s\" %s%s < \"%s\" >> \"%s\" 2>&1\"", comspec, exec_path, pgdata_opt, post_opts, DEVNULL, log_file); + } else snprintf(cmd, MAXPGPATH, "\"%s\" /C \"\"%s\" %s%s < \"%s\" 2>&1\"", comspec, exec_path, pgdata_opt, post_opts, DEVNULL); --------------C9227EE100DB1B730994A475--