Git development
 help / color / mirror / Atom feed
* [PATCH] reftable/writer: fsync table after writing its footer
@ 2026-12-04  2:25 Aman Ganapathy
  2026-12-04  6:53 ` Patrick Steinhardt
  0 siblings, 1 reply; 3+ messages in thread
From: Aman Ganapathy @ 2026-12-04  2:25 UTC (permalink / raw)
  To: git; +Cc: Patrick Steinhardt, John Cai, Aman Ganapathy

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)


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] reftable/writer: fsync table after writing its footer
  2026-12-04  2:25 [PATCH] reftable/writer: fsync table after writing its footer Aman Ganapathy
@ 2026-12-04  6:53 ` Patrick Steinhardt
  2026-12-04 18:25   ` Aman Ganapathy
  0 siblings, 1 reply; 3+ messages in thread
From: Patrick Steinhardt @ 2026-12-04  6:53 UTC (permalink / raw)
  To: Aman Ganapathy; +Cc: git, John Cai

On Thu, Dec 03, 2026 at 06:25:50PM -0800, Aman Ganapathy wrote:
> 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.

This failure mode is quite surprising. I would have expected that we die
with a lot of noise and refuse to do anything in case the reftable stack
is corrupt. Did you by any chance also investigate why that's the case?

> 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;

The diff looks obviously good to me. In theory we could even move the
flush after the check for the empty table, because we won't commit such
a table to disk anyway. But I don't think that necessitates a reroll.

> 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);
> +}

Clever. It's nice to see how easy it is with our unit tests to exercise
even small details of the system.

Overall I think this patch is already in a good state. Thanks for the
fix!

Patrick

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] reftable/writer: fsync table after writing its footer
  2026-12-04  6:53 ` Patrick Steinhardt
@ 2026-12-04 18:25   ` Aman Ganapathy
  0 siblings, 0 replies; 3+ messages in thread
From: Aman Ganapathy @ 2026-12-04 18:25 UTC (permalink / raw)
  To: Patrick Steinhardt; +Cc: git, John Cai

On Fri, Dec 04, 2026 at 07:53:15AM +0100, Patrick Steinhardt wrote:
> This failure mode is quite surprising. I would have expected that we die
> with a lot of noise and refuse to do anything in case the reftable stack
> is corrupt. Did you by any chance also investigate why that's the case?

I hadn't yet, so I traced a path through the code now. As I see it, the
stack fails to load in reftable_be_init(), which saves the error in
refs->err and doesn't print anything. Every ref read after that fails,
but the error gets lost on the way up.

If you look up a single ref, refs_resolve_ref_unsafe() just returns NULL,
which is the same as when the ref doesn't exist. So "git rev-parse HEAD"
says "unknown revision", like in an empty repo.

If you list refs, the error does come back, but for-each-ref and gc don't
check it. So for-each-ref prints nothing, and gc thinks nothing is
reachable and prunes everything.

You can see it by cutting the footer off the newest table:

    git init -q --ref-format=reftable repo && cd repo
    git commit -q --allow-empty -m one
    t=.git/reftable/$(tail -1 .git/reftable/tables.list)
    truncate -s -68 "$t"
    git rev-parse HEAD     # "unknown revision"
    git for-each-ref       # prints nothing, exits 0

With the files backend, a truncated packed-refs dies right away with
"unterminated line in .git/packed-refs", so I'd expect the same here.
Should reftable_be_init() just die when the stack is corrupt, or is there
a reason the error is kept for later? Happy to send a patch for it if
need be!

Thanks for the review!

Aman

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-04 18:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04  2:25 [PATCH] reftable/writer: fsync table after writing its footer Aman Ganapathy
2026-12-04  6:53 ` Patrick Steinhardt
2026-12-04 18:25   ` Aman Ganapathy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox