From: Aman Ganapathy <amanganapathy@gmail.com>
To: git@vger.kernel.org
Cc: Patrick Steinhardt <ps@pks.im>, John Cai <johncai86@gmail.com>,
Aman Ganapathy <amanganapathy@gmail.com>
Subject: [PATCH] reftable/writer: fsync table after writing its footer
Date: Thu, 3 Dec 2026 18:25:50 -0800 [thread overview]
Message-ID: <20261204022550.17883-1-amanganapathy@gmail.com> (raw)
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)
next reply other threads:[~2026-12-04 2:27 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 2:25 Aman Ganapathy [this message]
2026-12-04 6:53 ` [PATCH] reftable/writer: fsync table after writing its footer Patrick Steinhardt
2026-12-04 18:25 ` Aman Ganapathy
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=20261204022550.17883-1-amanganapathy@gmail.com \
--to=amanganapathy@gmail.com \
--cc=git@vger.kernel.org \
--cc=johncai86@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