🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.git
Article #1608

Re: [PATCH v2 6/6] odb/source-files: only reload alternates when the file has changed

#1608
From: Christian Couder
Date: Tue, 27 Oct 2026 11:22
93 lines
15521 bytes
On Fri, Oct 23, 2026 at 2:48 PM Patrick Steinhardt <ps@pks.im> wrote:

> -static int read_alternates(const char *object_dir, struct strvec *out)
> +static void read_alternates(const char *object_dir, struct strvec *out,
> +                           struct stat_validity *validity)
>  {
>         struct strbuf buf = STRBUF_INIT;
>         char *path;
>
>         path = xstrfmt("%s/info/alternates", object_dir);
> -       if (strbuf_read_file(&buf, path, 1024) < 0) {
> -               warn_on_fopen_errors(path);
> -               free(path);
> -               return 0;
> +       if (!validity || !stat_validity_check(validity, path)) {
> +               bool successful = false;
> +               int fd;
> +
> +               fd = open(path, O_RDONLY);
> +               if (fd < 0) {
> +                       warn_on_fopen_errors(path);
> +               } else if (strbuf_read(&buf, fd, 1024) < 0) {

Here we start reading from the alternates file, but that file could
start to be written to...

> +                       warning_errno(_("unable to read alternates file '%s'"),
> +                                     path);
> +               } else {
> +                       parse_alternates(buf.buf, '\n', object_dir, out);

...suppose we parse the old content of the file (which calls
strbuf_realpath() for each entry and so does a few syscalls) while an
in-place rewrite of the file, like the:

  `echo "$PWD/alternate/.git/objects" >repo/.git/objects/info/alternates`

in the new t1006 test added by patch 3/6, happens...

> +                       successful = true;
> +               }
> +
> +               if (validity) {
> +                       if (successful)
> +                               stat_validity_update(validity, fd);

...then we would update the validity while the alternates file's
content has changed (and we only parsed its old content).

So, if the file content is not changed after that, we never parse the
content of the file from the in-place rewrite.

(If the rewrite is not an in-place one, but the file is for example
replaced through a lock file and rename, the race doesn't happen.)

It seems to me that if we want to avoid a race here, we should have
the `stat_validity_update(..., fd);` call before reading from fd like
in "refs/packed-backend.c" and "shallow.c".

> +                       else
> +                               stat_validity_clear(validity);
> +               }
> +
> +               if (fd >= 0)
> +                       close(fd);

It looks like v1 didn't have this issue, as it called
stat_validity_update() before reading. A concurrent rewrite then only
caused a spurious re-read on the next check. So I wonder if we could
keep that order, and just clear the validity when reading fails, which
would also address Junio's point:

               fd = open(path, O_RDONLY);
               if (fd < 0) {
                       warn_on_fopen_errors(path);
               } else {
                       if (validity)
                               stat_validity_update(validity, fd);
                       if (strbuf_read(&buf, fd, 1024) < 0) {
                               warning_errno(_("unable to read
alternates file '%s'"),
                                                       path);
                               if (validity)
                                       stat_validity_clear(validity);
                       } else {
                               parse_alternates(buf.buf, '\n', object_dir, out);
                       }
                       close(fd);
               }
Technical Details
Message-ID: <CAP8UFD3XSQXFAiqGEp++=Y_OmEhogDo77t-XRFYAu_1yOQc+Gw@mail.gmail.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!git+bounces-73473-linux-kernel-git=dewy.ru
References: <20261023-pks-odb-files-reload-alternates-v2-0-30559d7ff43b@pks.im> <20261023-pks-odb-files-reload-alternates-v2-6-30559d7ff43b@pks.im>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!git+bounces-73473-linux-kernel-git=dewy.ru
From: christian.couder@gmail.com (Christian Couder)
Newsgroups: linux.kernel.git
Subject: Re: [PATCH v2 6/6] odb/source-files: only reload alternates when the
 file has changed
Date: Tue, 27 Oct 2026 12:22:50 +0100
Organization: Usenet Network
Message-ID: <CAP8UFD3XSQXFAiqGEp++=Y_OmEhogDo77t-XRFYAu_1yOQc+Gw@mail.gmail.com>
References: <20261023-pks-odb-files-reload-alternates-v2-0-30559d7ff43b@pks.im>
 <20261023-pks-odb-files-reload-alternates-v2-6-30559d7ff43b@pks.im>
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Injection-Info: usenet.network;
	logging-data="1521827"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:C3p8Gwum/xTApUYyeNkXncYEKl0= sha256:Sfb5rJ9hYYGg/034wZ/lD07THBMMVpdwCyKfJXUTx2s=
	sha1:Uh4CQr3BBfS+nZkFBDUJILSttHg= sha256:LC1IRcmzFTQC1BUCx9By0oGdPZFEX9lZBi6xj68R/5Q=
List-ID: <git.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 893EE125BEE; Tue, 27 Oct 2026 14:27:54 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.048
X-Spam-Status: No, score=-3.048 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.001,
	FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=gmail.com
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id DO2htff1lVow;
	Tue, 27 Oct 2026 14:27:52 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=git+bounces-73473-linux-kernel-git=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id D274A125BE8; Tue, 27 Oct 2026 14:27:43 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id 580B325AF7; Tue, 27 Oct 2026 11:23:06 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1793100186;
	b=S+W4iXRhdmStlzg7cCEmlHa4ZVAAnvJ0wTRxzpDzyDalsIOZnXTn45o7cnhe0SalxFOE
	 b7L4Slv0/TeyDNprI7AzI0qUdZ9R6UoCSNdoKVk6j+h6Lxu0XgKnMDl/YpOgu2bYvCd+J
	 hQbFR3Z9wyaXncaFGA1If+k7HP44dPdlnNqTEv/sQ0tXJz6+VamVBrkdL0k7t85u8Ny3/
	 E9dKFXeQ+tJMNPj1N2Dhd3NPWMXWPyNNvp7oHbuBgJkq+aC3k1x/7UqYCq59bJnb3fmEb
	 r4lxyThqsJVOwTJ1QvBDogy9ro8CkB4Pz9+F9XQqkTG4ciQ/Zvz7vCeW9QVw+rzbTLQ==
X-ARC-Message-Signature: i=3; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1793100186;
	h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:
	 X-Google-DKIM-Signature:X-Gm-Message-State:X-Gm-Gg:X-Received:
	 X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
	 List-Unsubscribe:List-Owner:Precedence:MIME-Version:References:
	 In-Reply-To:From:Date:X-Gm-Features:Message-ID:Subject:To:Cc:
	 Content-Type:Content-Transfer-Encoding;
	bh=Ljph5d3KePALy+AQzkJZr3HSCfY/bhkUr/N2EDbBzO8=;
	b=AOQswyiF+U8/odyU41RdVFptNzHuSPxRWj5LGf7JuZyTwRvsrq5e5RocutcJVWB7oyUX
	 rWr2UaAz313kQmcTldZbizuzB31CDf8fZijQ70vSPHx7rfp4zIRcO1BmGebmyima9uNxE
	 XcQQJuBqOxXxPsGN52N1vpFwsONDFjw4hMopsuXPMflOeyqtfVTKmrr+/yb7qJ9lmGK7B
	 PuRKFWEd1qsaUglUSgU83dbI8jgZA2PWQnWpoa80AY7HID9RijG8QSnkGHmCw2D+EP+AO
	 qYuS10eq49VVy/ueT/bxmgCbAbD/9Lcr98lNPqDpRX/5UIFaefV7JZYWg6PdMCzvWSg==
X-ARC-Authentication-Results: i=3; smtp.subspace.kernel.org; 
	dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b="pfG/q8c4";
	dmarc=pass header.from=gmail.com;
	spf=pass smtp.mailfrom=gmail.com;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 14ACB606A321;
	Tue, 27 Oct 2026 11:23:06 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=pfG/q8c4
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=74.125.82.47
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1793100185;
	b=ZxwCt52uy548Wt/rvL1CERAc+G+Dr8w+dnzLyYWulIGq+eWSoNk4SzxwjfWmQlRo2SWU
	 xzoge7uXc7yYzsyJ2cC/fXTY702Vr0vx359tV5HfjjrNz7zEPYwL3R5+TCiuMeVfRCpYP
	 4GJ8XwkDx3abtOEGUCFRtY4mYrMDDrDJ0LFI34ZOmYRhHIefCBZAA8iMecEdlsQQYawzR
	 ERrfQiriK03I7nhoJDkXI9i+/V/xvK+lmeBsSJicy5mJgdQ/hfSSt1cuIBWcI02waFIxj
	 Aji1D+h5qqxJCb0zaj7kQkd/A5W3Yg2hoKiGV8ncUYs3AcquJsgnH0BLMxJhlOFDiFg==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1793100185;
	h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
	 X-Gm-Message-State:X-Gm-Gg:X-Received:MIME-Version:References:
	 In-Reply-To:From:Date:X-Gm-Features:Message-ID:Subject:To:Cc:
	 Content-Type:Content-Transfer-Encoding;
	bh=Ljph5d3KePALy+AQzkJZr3HSCfY/bhkUr/N2EDbBzO8=;
	b=2H0Pf1deVsUsH9E7Rqo0vMKpXZcKtXLSP1GflsbLcD6VI8DJNvdK2urSTxB1fvIxs01D
	 fHEYf3fkgNL0tTgq3JOy1y/PPKbMGNSsrklPJCo67yoPlA1ZseI2wwPsrLofkQ4eosbyB
	 wDNa9DPDiVvjSthDka/yQ3Ypa7DHJsEzZ8hc9oiwvgLNuKASfP0MZxfwC7FhHTwGSY9Yj
	 bRB1deciQjwpIQPTYwtHlCZfRXYLBE5MIRWwUMUda0ifEpnaBl3NoDg8NFafSjpYEIqsL
	 Lebikbyd9YefbEJLQ7FLL+1pRVcZylGfLUJjPiiw7TWK/90ZQq9FfY0+OJvRVeIiTDA==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dmarc=pass header.from=gmail.com;
	spf=pass smtp.mailfrom=gmail.com;
	dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b="pfG/q8c4";
	arc=pass header.oldest-pass=0 smtp.remote-ip=74.125.82.47
X-Received: from mail-dl1-f47.google.com (mail-dl1-f47.google.com [74.125.82.47])
	(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC531606A31A
	for <git@vger.kernel.org>; Tue, 27 Oct 2026 11:23:04 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CC531606A31A
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-dl1-f47.google.com with SMTP id a92af1059eb24-14394530ec6so4812660c88.1
        for <git@vger.kernel.org>; Tue, 27 Oct 2026 04:23:04 -0700 (PDT)
X-ARC-Seal: i=1; a=rsa-sha256; t=1793100184; cv=none;
        d=google.com; s=arc-20260327;
        b=MXnGalCrR1+l1Ufd8V/GzVU0vBuVnLrIPgOHs4ochreTDzr16tGp4PIKv4Mt1Dv720
         aI7A5X2V/dbCGJQb7mg9Vn+AN9P7xY5d4Q2VJPJYMpP+xCEuC7kJwDamQZhRDKaVZQIw
         BaV1jPyUd9v67V0WQeRV/kMGf6wpfOJrHHcu9qnARuJJICwgFrfWt6swvX+mzPluUJln
         Ug2XvWE2phpj7mdRKfDlaHK5cyOBGg8w6GBBTTrqb+pGOllZDa7c0+5LRPUrskagC3DV
         7P43DKtNPCnzjRuyVHvKNfQPihT9z0R3+GlNNG1JHNcS1PGN+TdmSASi1dAq+VDY1V9H
         FBlg==
X-ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327;
        h=content-transfer-encoding:cc:to:subject:message-id:date:from
         :in-reply-to:references:mime-version:dkim-signature;
        bh=Ljph5d3KePALy+AQzkJZr3HSCfY/bhkUr/N2EDbBzO8=;
        fh=4ryOG4wBbIqKixWzeKd5dc717SQGdNJth+KYU2DAukY=;
        b=bLfgCfGxcNvtXxnBcZ2O/Fb42psbk6G33bvVMeQokGHVSFuAH2cvw9AM+pTdT/lTo0
         W/WKk/gp1NpvtLiqHS0mtFL/TmYdOjQrGL94FntWGkdP9DXH45iCSnUWe1tDYRmX+bCp
         G4E24ZCyEs4dOZBJdzZeXp4fRBD2dvj6TEtbTIWW7Qv3m4OAvSGcpstlHH61dKzuMkHg
         YtL92fT2h35QolMvLO+F1nBvM4QAgcPxKdiDkInreygFzj9Uw94hOlNKwTQisunoMHA6
         MLGgWouZTxtwoePJuL3FSgwZt1WTt5bQ1vHSiuu5zymm/5EYgnqEldOIcYYj1KX0G4gn
         fqHg==;
        darn=vger.kernel.org
X-ARC-Authentication-Results: i=1; mx.google.com; arc=none
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20251104; t=1793100184; x=1793704984; darn=vger.kernel.org;
        h=content-transfer-encoding:content-type:cc:to:subject:message-id
         :date:from:in-reply-to:references:mime-version:from:to:cc:subject
         :date:message-id:reply-to:content-type;
        bh=Ljph5d3KePALy+AQzkJZr3HSCfY/bhkUr/N2EDbBzO8=;
        b=pfG/q8c4ezoCM7Z30NRPCc3F+lg/IefwpDCzZLuRXEUpggVCEgC84V+mPSqse/QSzk
         w8lVIBzCJ1TCayBVWIBLdKIeW2tzyLbBK9hKOsB4lqyxH046RGPdxpOu1vxjSlNSWTBq
         bG2f3m6gt+FYH9tVxjgslO+T4vPcbB2HoZpr+NlpII0HehMPArqX6E2Y2DR+PllqtCNN
         UIqqmLYJlFsFVDHaUHtcLnFYQCupIuIdTXmUeFZeUh8rs6K61/nPvBnwAYr7JYkST8Yg
         oNp9UKSXGRizWQm5WiC9QvTdyErOHlBTe4z43LLJPnSL1O3RklTwQqGu1qHZCYVcCOyi
         jE/w==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20260707; t=1793100184; x=1793704984;
        h=content-transfer-encoding:content-type:cc:to:subject:message-id
         :date:from:in-reply-to:references:mime-version:x-gm-gg
         :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to
         :content-type;
        bh=Ljph5d3KePALy+AQzkJZr3HSCfY/bhkUr/N2EDbBzO8=;
        b=Rq3f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iaN
	KC3xOWcqiTz7jgXKiB4s7xT
X-Received: by 2002:a05:7023:d04:b0:15c:1679:1cc7 with SMTP id
 a92af1059eb24-19936e34bacmr15182620c88.37.1793100183869; Tue, 27 Oct 2026
 04:23:03 -0700 (PDT)
X-Mailing-List: git@vger.kernel.org
X-List-Post: <mailto:git@vger.kernel.org>
X-List-Help: <mailto:git+help@vger.kernel.org>
X-List-Subscribe: <mailto:git+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:git+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:git+owner@vger.kernel.org>
X-Precedence: list
X-In-Reply-To: <20261023-pks-odb-files-reload-alternates-v2-6-30559d7ff43b@pks.im>
X-Gm-Features: AclHuK-B1k3pgdGe9369Zkn46EsKPwKYybP488eTCYSmVt7osod6yTo4fmw5ZSE
X-Cc: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>