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 1idFiO-0005ck-Ud for pgsql-bugs@arkaria.postgresql.org; Fri, 06 Dec 2019 15:40:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1idFiN-0006vd-Mm for pgsql-bugs@arkaria.postgresql.org; Fri, 06 Dec 2019 15:40:19 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1idFiN-0006vW-Bc for pgsql-bugs@lists.postgresql.org; Fri, 06 Dec 2019 15:40:19 +0000 Received: from mail-lf1-x141.google.com ([2a00:1450:4864:20::141]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1idFiL-0006hY-44 for pgsql-bugs@lists.postgresql.org; Fri, 06 Dec 2019 15:40:18 +0000 Received: by mail-lf1-x141.google.com with SMTP id f15so4776663lfl.13 for ; Fri, 06 Dec 2019 07:40:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=Unr+Q98Nnrl5LkQfj1qSnpjO59Xt792iYVXpaJmpQrU=; b=OoY4RV9VZBFbbyqucxTP6qReeLbUVcJXOI1fxaW7yJxhI2gydT6QifIaj82G5MzpqW 4oPOB/1iYrzeXLeO2XbySXePamvx0CDwoZtKp4uBxcLdAdK4nWrBfgzpihSYjznh6ozo lfcSIk3R46efrDZPHUJKmKFUrPl2VtFMM2/7gmNoDiGf9FO4tnduTL+AQlRLOlg+Nj21 /lEIe0dE8oMNX6vKBJRGLCT2KpH6hCNhY2RbhY9fVXQuSimlSS04Eq04Wxx5waHY67KV ul6zDUYc1O8ilPJufaqoxkmJbWTK44o6Mz5RrWE854nT3MCKmnLhIconl3CAau7uvY/m Avzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=Unr+Q98Nnrl5LkQfj1qSnpjO59Xt792iYVXpaJmpQrU=; b=aRRGdczvDtkqBWE1iGs/lhbl1/UG7QwgyJuWZD8HmFWhqzZxq/ng27lPQc2UjOD06M mwrfkZ1FURVQb0PYNux30wRVsex7CwIRXi5q1tNqBVC8Vqd1ZwE++dwPvLxq5kcCh9dI at/t9Oo5aDJ5ubGwogeK0vbvO/d4mh8nNYSwfBGyGg9KnfgIc0ZLdAQ7mgL7YByk9KLK FJVvLN/QfnVApicPJYvpe74HaaBDX7Pl4aAt8oV4IBM4YVMFT9/55NQ3F0oF5mrTxKvi bbzsaLP4z2bprIfPVZbzMDYW7QbOaKoZtP3VM4aG7kWgQv5YGjOuf10xnKUfvVyzVOry 2xnQ== X-Gm-Message-State: APjAAAUo06PV84EHXRv5e7eCarxZCilincgJKHMTV0AzFOCRR6zpU9km GP5hJaUV3BZfPFZizP6KB6/zZp93 X-Google-Smtp-Source: APXvYqwcJo59KwU4DvlAOUEscKmhOGcJxRYGcRJbtkCId5pucFCJrpL1W93Jia1Tvy7tJXe0UGQagw== X-Received: by 2002:a19:dc14:: with SMTP id t20mr8496004lfg.47.1575646815720; Fri, 06 Dec 2019 07:40:15 -0800 (PST) Received: from [1.0.0.7] ([178.155.4.37]) by smtp.gmail.com with ESMTPSA id a19sm4749443ljb.103.2019.12.06.07.40.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 06 Dec 2019 07:40:14 -0800 (PST) Subject: Re: BUG #16154: pg_ctl restart with a logfile fails sometimes (on Windows) To: Tom Lane Cc: pgsql-bugs@lists.postgresql.org References: <16154-1ccf0b537b24d5e0@postgresql.org> <6120.1575645256@sss.pgh.pa.us> From: Alexander Lakhin Message-ID: Date: Fri, 6 Dec 2019 18:40:12 +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: <6120.1575645256@sss.pgh.pa.us> Content-Type: multipart/mixed; boundary="------------7A79CF79C8472D11EAA8C2F6" Content-Language: ru-RU List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------7A79CF79C8472D11EAA8C2F6 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit 06.12.2019 18:14, Tom Lane wrote: > Alexander Lakhin writes: >> 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. > Interesting. I wonder whether this explains all of the remaining > buildfarm failures of this sort that we've been seeing even after > 0ba06e0bf. > >> To fix this issue I propose the attached patch >> (fix_logfile_sharing_violation ). > This seems like a pretty ugly hack ... please at least make it > #ifdef WIN32, so that the rest of us don't have to deal with it. > Also, if I read it correctly, it causes a pre-existing logfile > to get truncated, which has never happened before. Mode "a" > would be a better choice. This change should go under the following code: #else                            /* WIN32 */     /*      * As with the Unix case, it's easiest to use the shell (CMD.EXE) to      * handle redirection etc.  Unfortunately CMD.EXE lacks any equivalent of      * "exec", so we don't get to find out the postmaster's PID immediately.      */     PROCESS_INFORMATION pi;     const char *comspec;     /* Find CMD.EXE location using COMSPEC, if it's set */     comspec = getenv("COMSPEC");     if (comspec == NULL)         comspec = "CMD"; So it should affect only Windows. I've fixed the mode. Thanks for your review! Best regards, Alexander --------------7A79CF79C8472D11EAA8C2F6 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, "a"); + 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); --------------7A79CF79C8472D11EAA8C2F6--