From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.174 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== 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== 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 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 ; Fri, 4 Dec 2026 02:27:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4EA96606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com 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 Received: by mail-dy1-f174.google.com with SMTP id 5a478bee46e88-38431138772so603635eec.1 for ; Thu, 03 Dec 2026 18:27:32 -0800 (PST) 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) 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) From: Aman Ganapathy To: git@vger.kernel.org Cc: Patrick Steinhardt , John Cai , Aman Ganapathy Subject: [PATCH] reftable/writer: fsync table after writing its footer Date: Thu, 3 Dec 2026 18:25:50 -0800 Message-Id: <20261204022550.17883-1-amanganapathy@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) X-Mailing-List: git@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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)