🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.git
Article #4313

[PATCH] reftable/writer: fsync table after writing its footer

#4313
From: Aman Ganapathy
Date: Fri, 04 Dec 2026 02:25
121 lines
14509 bytes
When adding a table to the stack, we write it to a temporary file, fsync
it, rename it into place and then update "tables.list" to refer to it.
The fsync is there so that the table is on disk before "tables.list"
points to it. But reftable_writer_close() calls the flush callback
before writing the footer, so the footer is not covered by the fsync.

After a crash, the new table can thus end up without its footer while
"tables.list" already refers to it. Reading the stack then fails and git
acts as if there were no refs at all, not just the ones in the new
table: "git for-each-ref" prints nothing, and "git gc --prune=now"
deletes every object. This can be reproduced with core.fsync=reference
by shutting down the filesystem with "xfs_io -x -c shutdown" right after
"git update-ref" returns.

Fix this by writing the footer before calling the flush callback, and
add a test that checks that nothing is written after the flush.

Signed-off-by: Aman Ganapathy <amanganapathy@gmail.com>
---
I was testing what happens when a machine crashes right after a ref
update, and with core.fsync=reference every ref was gone afterwards, not
just the new one. Here's a small repro (needs root and xfsprogs):

    truncate -s 300M /tmp/disk.img && mkfs.xfs -q /tmp/disk.img
    mount -o loop /tmp/disk.img /mnt && cd /mnt
    git init -q --ref-format=reftable repo && cd repo
    git config core.fsync reference
    git commit -q --allow-empty -m one && sync
    git update-ref refs/heads/new HEAD
    xfs_io -x -c shutdown /mnt
    cd / && umount /mnt && mount -o loop /tmp/disk.img /mnt
    git -C /mnt/repo for-each-ref   # prints nothing
    git -C /mnt/repo log            # "current branch appears to be broken"

With the patch both refs are still there. Same thing on ext4, and when I
used a dm-error target instead of xfs_io.

 reftable/writer.c                   |  8 +++---
 t/unit-tests/u-reftable-readwrite.c | 41 +++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/reftable/writer.c b/reftable/writer.c
index 073b9bbd89..1ba54398a2 100644
--- a/reftable/writer.c
+++ b/reftable/writer.c
@@ -775,16 +775,16 @@ int reftable_writer_close(struct reftable_writer *w)
 	reftable_put_be32(p, crc32(0, footer, p - footer));
 	p += 4;
 
+	err = padded_write(w, footer, footer_size(writer_version(w)), 0);
+	if (err < 0)
+		goto done;
+
 	err = w->flush(w->write_arg);
 	if (err < 0) {
 		err = REFTABLE_IO_ERROR;
 		goto done;
 	}
 
-	err = padded_write(w, footer, footer_size(writer_version(w)), 0);
-	if (err < 0)
-		goto done;
-
 	if (empty_table) {
 		err = REFTABLE_EMPTY_TABLE_ERROR;
 		goto done;
diff --git a/t/unit-tests/u-reftable-readwrite.c b/t/unit-tests/u-reftable-readwrite.c
index 5794b460c6..ead2e80987 100644
--- a/t/unit-tests/u-reftable-readwrite.c
+++ b/t/unit-tests/u-reftable-readwrite.c
@@ -662,6 +662,47 @@ void test_reftable_readwrite__write_empty_table(void)
 	reftable_buf_release(&buf);
 }
 
+struct flush_check {
+	struct reftable_buf buf;
+	size_t len_at_flush;
+};
+
+static ssize_t flush_check_write(void *arg, const void *data, size_t sz)
+{
+	struct flush_check *check = arg;
+	cl_assert(!reftable_buf_add(&check->buf, data, sz));
+	return sz;
+}
+
+static int flush_check_flush(void *arg)
+{
+	struct flush_check *check = arg;
+	check->len_at_flush = check->buf.len;
+	return 0;
+}
+
+void test_reftable_readwrite__write_flush_after_footer(void)
+{
+	struct reftable_write_options opts = { 0 };
+	struct flush_check check = { .buf = REFTABLE_BUF_INIT };
+	struct reftable_ref_record ref = {
+		.refname = (char *) "refs/heads/main",
+		.update_index = 1,
+		.value_type = REFTABLE_REF_VAL1,
+	};
+	struct reftable_writer *w;
+
+	cl_assert(!reftable_writer_new(&w, flush_check_write, flush_check_flush,
+				       &check, REFTABLE_HASH_SHA1, &opts));
+	reftable_writer_set_limits(w, 1, 1);
+	cl_assert(!reftable_writer_add_ref(w, &ref));
+	cl_assert(!reftable_writer_close(w));
+	cl_assert_equal_i(check.len_at_flush, check.buf.len);
+
+	reftable_writer_free(w);
+	reftable_buf_release(&check.buf);
+}
+
 void test_reftable_readwrite__write_object_id_min_length(void)
 {
 	struct reftable_write_options opts = {
-- 
2.39.5 (Apple Git-154)
Technical Details
Message-ID: <20261204022550.17883-1-amanganapathy@gmail.com>
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mi.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!peer01.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!git+bounces-76970-linux-kernel-git=dewy.ru

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mi.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!peer01.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!git+bounces-76970-linux-kernel-git=dewy.ru
From: amanganapathy@gmail.com (Aman Ganapathy)
Newsgroups: linux.kernel.git
Subject: [PATCH] reftable/writer: fsync table after writing its footer
Date: Thu,  3 Dec 2026 18:25:50 -0800
Organization: Usenet Network
Message-ID: <20261204022550.17883-1-amanganapathy@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network; logging-data="2655614"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:vxsqonp0UriEkHlpX1sMdsn+krc= sha256:YHFR7yOi0nl+mgPq7S9WRs/Aq918oHdZ9jYc3mZYoSY= sha1:2eVqKsfXzWSgHrw3pa3mDAFYdXU= sha256:heB7IpT0K2WXExrz2pIrk8+IYyYXsMGkRnyL/yYKipY=
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 8EBCF117AB8; Fri,  4 Dec 2026 05:27:48 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.397
X-Spam-Status: No, score=-3.397 tagged_above=-9999 required=6.31 tests=[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.001, 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 zTkdP1UyEBeC; Fri,  4 Dec 2026 05:27:46 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=git+bounces-76970-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 tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114]) by mail.dewy.ru (Postfix) with ESMTPS id F1633116D9F; Fri,  4 Dec 2026 05:27:37 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by tor.lore.kernel.org (Postfix) with ESMTP id 1B99023EEB; Fri,  4 Dec 2026 02:27:36 +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=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796351253; b=lQxVISILflDe3SCiTZqD1DpTQgdVRzj8ObJL/icJisxXb2pjB7cNAtJK4TohRpcErrW9 r8JuadOwLbaPdqEOb6NuAllGP3kbxUavP7VRW9i8Zf33q9R7mkDRUDKHmdvZBAGjWAPCP BqcQRSIiygIrOeo7T4Xfa6wR+9coopPp8eWD1+dgNfAYNMh6U/uncuPtV6Qb1xf3h4mb6 +WzFFnZscKCnH7xqkV9FGuhI6UaOnwwKCPaa3kuQcQnBKEayAvVXIyZ5nf/SxIZVPODe7 6lHc3WvkeD7BBLsDLCV/T20HxcLNv/fvpYFtW8ZF/Q3nQZTrN9tf9QKcYpF5NKxg2fw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796351253; h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature: X-Google-DKIM-Signature:X-Gm-Message-State:X-Gm-Gg:X-Received: Received:From:To:Cc:Subject:Date:Message-Id:X-Mailer:X-Mailing-List: List-Id:List-Post:List-Help:List-Subscribe:List-Unsubscribe: List-Owner:Precedence:MIME-Version:Content-Transfer-Encoding; bh=eU2KNCslEzuE0/okOTmT+y6seLle2XYCUC7bqQbjhf8=; b=Or4IJngiQb784w8P84sYEyfbQ8cYT7Z7+Xno5HID5D9N3p0bRuijOjUXEptISABollIu dGM79lxoYBNYLpBa2NnSY98pM+FLtp6JEB9jiM/2tB31YLTcfxT5CRCAcZ3NcPuGsDpvA OLcOA42bY2VSGcVRZFQBzlyun54oaHiLHdbelicXMK1QcxztngMCbOZl1VWvo7OzNtS0M jkRk/DSweiDH6XA3P6ibJHf8g7ijUin4DILkcTeLYs3uu78AFnV64N67VIH56EDNhGk3f P3Vs67SUkY2udHIH9zk3ZNA2ifTViMqxetQSCgd2UPfWw2lstEbeCMg/Cq7GbzCTx0w==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=tjC2a6yh; 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 7FAE6606A333; Fri,  4 Dec 2026 02:27:33 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=tjC2a6yh
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.174
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796351252; b=A4itXUfsFU7CvQ8AQEaabpfLnzM1IuOk6SFVqGsnFtMyNQPusYUr647eyFyCFJBmCKc3 WYmJUREDv4AnFnkDhH+/TjCqykw3G+DbVF1WFL9x8roHMBvJvKIl/yAvgqq13EfjL3Vcp m9RL4vhhsEXaIDVGAaV79lnltrxQ2cHMjX5Rb763E/XUvzqyEwElbHLsF3BNTHV8F2+Xp PgRNyNyKjz0BAcIF9UNzCRSiT5dLlmHtaHIzLZDNMm50JgnIxbGhQz/ceBxhzDPVjouQE D3VRzcp+2LtcdNmxR7b/HNo9mZW20eBg6/UjiCLkpESBKD9JO54q/YZmGoPJhF9I43A==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796351252; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-Id:X-Mailer:MIME-Version:Content-Transfer-Encoding; bh=eU2KNCslEzuE0/okOTmT+y6seLle2XYCUC7bqQbjhf8=; b=zqdEcM80wxR8cEbjW4yFO2IwBo2FsjWD68gqTEXo6M4iIy9s3Tqk5M7+mNzwSQQbwUwZ vCFpHZ+ArH6+gfk6e19B66VUt4yLHgkyAT2Vy/8vPDzNOKf/nTjsDk/LpfXf/XNu30z5G ed1r3QzjS/HGyTdUdChZ+ZotuqqV6OKHfIQQr/ho2n6wHnY+r6iOBn0WE7YIQYViZiTIf zMT/JQjs5TiLbbzJSIZY7I0ykGB9HYB9ZGsACJzwVWId+KUmEkAnG5jE69LeJpRxmp/IW BNUEx0ufEV4+f6jQE1RnYg+V3nXY6vAbUD/rHV8/e02KzEXuKqpYF1T2EAoDfo48WkQ==
X-ARC-Authentication-Results: i=1; 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=20260707 header.b=tjC2a6yh; arc=none smtp.remote-ip=74.125.82.174
X-Received: from mail-dy1-f174.google.com (mail-dy1-f174.google.com [74.125.82.174]) (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 4EA96606A322 for <git@vger.kernel.org>; Fri,  4 Dec 2026 02:27:32 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4EA96606A322
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-dy1-f174.google.com with SMTP id 5a478bee46e88-38431138772so603635eec.1 for <git@vger.kernel.org>; Thu, 03 Dec 2026 18:27:32 -0800 (PST)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796351251; x=1796956051; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=eU2KNCslEzuE0/okOTmT+y6seLle2XYCUC7bqQbjhf8=; b=tjC2a6yh0dQIrsMcqF862KEJvYRoDhybFHXGZvffNQ5IZwiOdbkuR9YN+CfTl69xqQ FRJku0T+EadykSY7Fkyb9Spo8iEVDttxkcsnWTLrOLKTckEjMBMkkfk115nCd3AjgRao 0cO9OuPXu4JdwMNRy6wNiI9HnVJvw8hYA0ejjAhVGdk3L+lXjA/jQsXGoRzEvdn/2Kph QGdffBHnknePLnpUTIj+KSEaGSQSb9z0n6M6BbSeVOuVID/YDTrnoP9MRf4/3WW2KoAm wQ6CfzJSEaGDoWLtK06YaDtg5ia/PdQEfFDYOdOUBQj+qhB0RGbo4I3vs5NVxi9bAxww aHUg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796351251; x=1796956051; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=eU2KNCslEzuE0/okOTmT+y6seLle2XYCUC7bqQbjhf8=; b=kJ23UFRVpnY5AcXOExCgK7nxckqB10JUESSNbWpYvXwnzaIE3arSL5m8AU/KXnEVc0 a4Lvhj10j5i9QsJjn48GVV0w2PzIDPHWa3P9oM9KRAp6yzE1h/zlQ6SF+I++VKcOQ8+f /JejwTpOjL10Upv1QMjFqRpgnZhjuuJ1+DBZEzpefzixwBgXFTR3hp1tJ9QV8lfFkuv7 S9QnU/FnZ3m9eRq7F0lqk9OQsfVqBxoWWGnGoGAfSIYQQF+dE0pp9GhF7qA00IlokTQD bcjNASAtL266susw3svFGLNLJ/m/u1q0FLZJwV5OlemiNXNNGjG93v2k4bcZyHEzQMBz vvJw==
X-Gm-Message-State: APuU4Z/xyXSGGDtJJXB+F02lL64/Zh/OJd2M+85a8s+YF8qeT52QgBv6 2ko1aVY8yAFKk+V6uhGYnZaRmewDjS+7RakSnMz+3dJCPd6hUOKX09Ed5SaiKw==
X-Gm-Gg: AZFdTZfL8Nae3aP9k29kwMbKtWJSnnXuqDFmBM7A5A50CM8ysCqqrUuaGnsvNGA74S8 uQWEqAvJAwt6J4eZP4WwhjqdA4aknSBCGAr4tWWCsiN3Wi9qv7hrZw7EwoKFth3wQfRFD3cGcnc wBs4g+MB54Pi70hnl7pJMy7nuHn9zdpoI00lF6Yf/vaZidWu3Y2LWajkF9r9xbkaONKU8vNxXrN KqAckvauUgWGzwvSq7gEZ7jymBjLsHA9JatYBdWeUSZhP7kOwNEdaXqF3Dv5vexlLRD/kHnVl6B qqmrXhFcZw2mtsP/S3AJpq3OCfhmFWH96Ohr1ZQK7JYH2H1r0aUv7xHcJ+ENxolSX2nEVy2/pzZ HNj21Of3WLmOh4DNicwp5jBgvGusYCEI3u5oA2MgGCaxiOg39t55DKGGYe/INLe5YUrcVSGWol+ 27G4d9fal4D3naN6PTyy7/HczO+mRddaf6Z1Cojzvr/Y2iM8AjMUMI7K7fVNSeSlf+ppERxwFjD JbruVMp6q9i0Gu+0JczyJFV4P/pfes+rCO/VIgBZJmJQZBLEIJeWlUKSqAxXo4RctLAHGt5A3OA Okx8rM+cMeWWC0YDI98D72IUjdcMiSYwEriyYhD0/gyEWJNyyarjEr2KisWe6luH1sjCdlX3ta3 LX4uM2uuG2o0tot2+Wj0YxHQ=
X-Received: by 2002:a05:7300:c00f:10b0:386:9c9f:83d8 with SMTP id 5a478bee46e88-387521868cbmr806071eec.17.1796351251336; Thu, 03 Dec 2026 18:27:31 -0800 (PST)
X-Received: from localhost.localdomain ([2601:645:8100:18e8:9cc9:4283:3bdb:82ff]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-387521e1c55sm1778505eec.16.2026.12.03.18.27.30 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 03 Dec 2026 18:27:30 -0800 (PST)
X-Cc: Patrick Steinhardt <ps@pks.im>, John Cai <johncai86@gmail.com>, Aman Ganapathy <amanganapathy@gmail.com>
X-Mailer: git-send-email 2.39.5 (Apple Git-154)
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-Received-Bytes: 14202