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