From: Jeff King <peff@peff.net>
To: Paulo Casaretto via GitGitGadget <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org, "Taylor Blau" <me@ttaylorr.com>,
"D. Ben Knoble" <ben.knoble@gmail.com>,
"Torsten Bögershausen" <tboegi@web.de>,
"Paulo Casaretto (Shopify)" <paulo.casaretto@shopify.com>,
"Patrick Steinhardt" <ps@pks.im>,
"Paulo Casaretto" <pcasaretto@gmail.com>
Subject: Re: [PATCH v5] lockfile: add PID file for debugging stale locks
Date: Wed, 21 Jan 2026 02:13:44 -0500 [thread overview]
Message-ID: <20260121071344.GA570838@coredump.intra.peff.net> (raw)
In-Reply-To: <pull.2011.v5.git.1768933954845.gitgitgadget@gmail.com>
On Tue, Jan 20, 2026 at 06:32:34PM +0000, Paulo Casaretto via GitGitGadget wrote:
> +static struct tempfile *create_lock_pid_file(const char *pid_path, int mode)
> +{
> + struct strbuf content = STRBUF_INIT;
> + struct tempfile *pid_tempfile = NULL;
> + int fd = -1;
> +
> + if (!lockfile_pid_enabled)
> + goto out;
> +
> + fd = open(pid_path, O_WRONLY | O_CREAT | O_EXCL, mode);
> + if (fd < 0)
> + goto out;
> +
> + strbuf_addf(&content, "pid %" PRIuMAX "\n", (uintmax_t)getpid());
> + if (write_in_full(fd, content.buf, content.len) < 0) {
> + warning_errno(_("could not write lock pid file '%s'"), pid_path);
> + close(fd);
> + fd = -1;
> + unlink(pid_path);
> + goto out;
> + }
> +
> + close(fd);
> + fd = -1;
> + pid_tempfile = register_tempfile(pid_path);
> +
> +out:
> + if (fd >= 0)
> + close(fd);
> + strbuf_release(&content);
> + return pid_tempfile;
> +}
Coverity complains that the close(fd) call in the "out" label is
unreachable, and I think it is right. When we jump from before the
open(), or if the open failed, then fd is negative (and thus no close).
If we get there when write_in_full() fails, then we close ourselves in
the conditional. And if we succeed, then we close the descriptor before
registering the tempfile.
I don't think it's wrong, but the cleanup is redundant between the "out"
path and the others.
Did you mean this:
diff --git a/lockfile.c b/lockfile.c
index 731cdd4944..e5d6ae0df6 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -122,14 +122,10 @@ static struct tempfile *create_lock_pid_file(const char *pid_path, int mode)
strbuf_addf(&content, "pid %" PRIuMAX "\n", (uintmax_t)getpid());
if (write_in_full(fd, content.buf, content.len) < 0) {
warning_errno(_("could not write lock pid file '%s'"), pid_path);
- close(fd);
- fd = -1;
unlink(pid_path);
goto out;
}
- close(fd);
- fd = -1;
pid_tempfile = register_tempfile(pid_path);
out:
which would just let the close after the out label handle all cases?
-Peff
next prev parent reply other threads:[~2026-01-21 7:13 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-02 15:07 [PATCH] lockfile: add PID file for debugging stale locks Paulo Casaretto via GitGitGadget
2025-12-02 22:29 ` D. Ben Knoble
2025-12-03 19:48 ` Torsten Bögershausen
2025-12-03 21:16 ` Jeff King
2025-12-03 22:21 ` Junio C Hamano
2025-12-03 22:32 ` Jeff King
2025-12-03 23:19 ` Taylor Blau
2025-12-05 11:03 ` Patrick Steinhardt
2025-12-05 18:46 ` Jeff King
2025-12-03 23:39 ` Taylor Blau
2025-12-17 18:59 ` [PATCH v2] " Paulo Casaretto via GitGitGadget
2025-12-18 0:32 ` Junio C Hamano
2025-12-18 0:47 ` Junio C Hamano
2025-12-18 1:33 ` Junio C Hamano
2025-12-18 3:38 ` Ben Knoble
2025-12-18 8:07 ` Patrick Steinhardt
2025-12-24 12:24 ` [PATCH v3] " Paulo Casaretto via GitGitGadget
2025-12-25 0:01 ` Junio C Hamano
2025-12-27 7:50 ` Jeff King
2026-01-05 12:23 ` Patrick Steinhardt
2026-01-07 16:45 ` [PATCH v4] " Paulo Casaretto via GitGitGadget
2026-01-08 1:59 ` Junio C Hamano
2026-01-08 14:19 ` D. Ben Knoble
2026-01-20 18:32 ` [PATCH v5] " Paulo Casaretto via GitGitGadget
2026-01-20 20:02 ` Junio C Hamano
2026-01-21 7:13 ` Jeff King [this message]
2026-01-21 8:13 ` Eric Sunshine
2026-01-21 10:14 ` Johannes Sixt
2026-01-21 16:39 ` Jeff King
2026-01-21 18:55 ` Junio C Hamano
2026-01-21 19:53 ` Jeff King
2026-01-21 16:23 ` Junio C Hamano
2026-01-22 19:23 ` [PATCH v6] " Paulo Casaretto via GitGitGadget
2026-01-22 20:17 ` Junio C Hamano
2026-02-06 16:27 ` Patrick Steinhardt
2026-02-06 19:31 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260121071344.GA570838@coredump.intra.peff.net \
--to=peff@peff.net \
--cc=ben.knoble@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitgitgadget@gmail.com \
--cc=me@ttaylorr.com \
--cc=paulo.casaretto@shopify.com \
--cc=pcasaretto@gmail.com \
--cc=ps@pks.im \
--cc=tboegi@web.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox