From: Jeff King <peff@peff.net>
To: Farid Zakaria <farid.m.zakaria@gmail.com>
Cc: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>,
Patrick Steinhardt <ps@pks.im>,
M Hickford <mirth.hickford@gmail.com>,
Johannes Schindelin <Johannes.Schindelin@gmx.de>,
Jeff Hostetler <jeffhostetler@github.com>
Subject: Re: [PATCH 2/2] credential-cache--daemon: do not take over a running daemon's socket
Date: Tue, 3 Nov 2026 22:07:53 -0500 [thread overview]
Message-ID: <20261104030753.GC1492379@coredump.intra.peff.net> (raw)
In-Reply-To: <20261101-fz-credential-cache-race-v1-2-28f0bac51e11@gmail.com>
On Sun, Nov 01, 2026 at 03:29:26PM -0800, Farid Zakaria wrote:
> The fix is to create the socket with unix_ss_create(), which the
> simple-ipc server already uses. It takes a lock and binds only if no
> other server is listening on the socket. A daemon that finds one prints
> "ok" and exits, and its client talks to the running daemon. Register
> the socket for removal only after binding it, so a daemon that loses
> never removes the other one's socket.
OK. One of the reasons the original did not take a lock is that a stale
lockfile might force the user to clean it up manually (rather than
everything eventually Just Working).
But unix_ss_create() only holds the lockfile briefly to make the socket
acquisition atomic. So I think the risk of stale files is negligible,
and worth the tradeoff to avoid races.
> static void serve_cache(const char *socket_path, int debug)
> {
> struct unix_stream_listen_opts opts = UNIX_STREAM_LISTEN_OPTS_INIT;
> - int fd;
> + struct unix_ss_socket *server_socket;
> + struct tempfile *socket_file;
> + int ret;
>
> - fd = unix_stream_listen(socket_path, &opts);
> - if (fd < 0)
> + /*
> + * Two clients may each start a daemon at the same time. Bind only
> + * if no other daemon is listening yet; otherwise leave the socket
> + * to that one, and our client will talk to it.
> + */
> + ret = unix_ss_create(socket_path, &opts, -1, &server_socket);
> + if (ret == -2) {
> + printf("ok\n");
> + return;
> + }
So if we found that somebody else beat us to it, we still say "ok",
which makes sense: we tell the client that _some_ daemon is ready for
them to talk to, even if it's not us.
And then conceptually we are done and can exit. The return here does the
same thing. I think that's OK, but it might be more obvious to add a
comment for the "ok" and just call exit(0). I am happy enough either
way.
> + /* Remove the socket at exit only now that it is ours. */
> + socket_file = register_tempfile(socket_path);
And then tempfile handling moves in here from the caller, because we
might or might not actually make a socket. Makes sense.
> [...]
The rest of the code changes look as I'd expect.
> diff --git a/t/t0301-credential-cache.sh b/t/t0301-credential-cache.sh
> index 6f7cfd9e33..8e07e55520 100755
> --- a/t/t0301-credential-cache.sh
> +++ b/t/t0301-credential-cache.sh
> @@ -51,6 +51,29 @@ test_expect_success 'socket defaults to ~/.cache/git/credential/socket' '
> test_path_is_socket "$HOME/.cache/git/credential/socket"
> '
>
> +test_expect_success 'a second daemon does not take over the socket' '
> + test_when_finished "git credential-cache exit" &&
> + check approve cache <<-\EOF &&
> + protocol=https
> + host=example.com
> + username=store-user
> + password=store-pass
> + EOF
> + git credential-cache--daemon "$HOME/.cache/git/credential/socket" >out &&
> + echo ok >expect &&
> + test_cmp expect out &&
> + check fill cache <<-\EOF
> + protocol=https
> + host=example.com
> + --
> + protocol=https
> + host=example.com
> + username=store-user
> + password=store-pass
> + --
> + EOF
> +'
I'd have thought this would pass even before the patch, but perhaps not.
Usually we rely on the "credential-cache" client doing a ping to avoid
starting a second daemon, but here we do it explicitly. So probably it
did just take over the other one, but nobody would really hit that case
outside of the race between the client pings and the daemons actually
starting.
So overall, I think the patch looks good.
-Peff
next prev parent reply other threads:[~2026-11-04 3:07 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-01 23:29 [PATCH 0/2] credential-cache: fix two races when daemons start at once Farid Zakaria
2026-11-01 23:29 ` [PATCH 1/2] credential-cache--daemon: allow a concurrently created socket dir Farid Zakaria
2026-11-03 8:01 ` Patrick Steinhardt
2026-11-04 18:25 ` Farid Zakaria
2026-11-04 2:58 ` Jeff King
2026-11-01 23:29 ` [PATCH 2/2] credential-cache--daemon: do not take over a running daemon's socket Farid Zakaria
2026-11-03 8:01 ` Patrick Steinhardt
2026-11-04 18:28 ` Farid Zakaria
2026-11-03 17:24 ` Junio C Hamano
2026-11-04 3:07 ` Jeff King [this message]
2026-11-04 19:53 ` [PATCH v2 0/3] credential-cache: fix two races when daemons start at once Farid Zakaria
2026-11-04 19:53 ` [PATCH v2 1/3] credential-cache--daemon: do not apply core.sharedRepository Farid Zakaria
2026-11-04 19:53 ` [PATCH v2 2/3] credential-cache--daemon: allow a concurrently created socket dir Farid Zakaria
2026-11-04 19:53 ` [PATCH v2 3/3] credential-cache--daemon: do not take over a running daemon's socket Farid Zakaria
2026-11-05 7:39 ` [PATCH v2 0/3] credential-cache: fix two races when daemons start at once Junio C Hamano
2026-11-06 18:45 ` Farid Zakaria
2026-11-08 1:56 ` Junio C Hamano
2026-11-08 4:27 ` Farid Zakaria
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=20261104030753.GC1492379@coredump.intra.peff.net \
--to=peff@peff.net \
--cc=Johannes.Schindelin@gmx.de \
--cc=farid.m.zakaria@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=jeffhostetler@github.com \
--cc=mirth.hickford@gmail.com \
--cc=ps@pks.im \
/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