Git development
 help / color / mirror / Atom feed
From: Justin Tobler <jltobler@gmail.com>
To: git@vger.kernel.org
Cc: ps@pks.im, gitster@pobox.com, Justin Tobler <jltobler@gmail.com>
Subject: [PATCH v4 06/11] odb/transaction: propagate begin errors
Date: Fri, 10 Jul 2026 11:37:17 -0500	[thread overview]
Message-ID: <20260710163722.2962278-7-jltobler@gmail.com> (raw)
In-Reply-To: <20260710163722.2962278-1-jltobler@gmail.com>

When `odb_transaction_begin()` is invoked, the function returns the
transaction pointer directly. There is no way for the backend to
signal that it failed to set up its state, such as when creating the
temporary object directory backing the transaction.

In a subsequent commit, git-receive-pack(1) starts using ODB
transactions and needs to be able to report such failures rather
than silently ignore them. Refactor `odb_transaction_begin()` to
return an int error code and write the resulting transaction into an
out parameter. Also introduce `odb_transaction_begin_or_die()` as a
convenience for callsites that do not need to handle errors
explicitly.

Note that `odb_transaction_begin()` now returns an error when the ODB
already has an inflight transaction pending. ODB transaction call sites
that may encounter an inflight transaction are updated to explicitly
handle this case.

Signed-off-by: Justin Tobler <jltobler@gmail.com>
---
 builtin/add.c            |  2 +-
 builtin/unpack-objects.c |  2 +-
 builtin/update-index.c   |  2 +-
 cache-tree.c             |  7 +++++--
 object-file.c            | 10 +++++++---
 odb/transaction.c        | 14 ++++++++++----
 odb/transaction.h        | 19 +++++++++++++++----
 read-cache.c             |  7 +++++--
 8 files changed, 45 insertions(+), 18 deletions(-)

diff --git a/builtin/add.c b/builtin/add.c
index c859f66519..3d5d9cfdb9 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -581,7 +581,7 @@ int cmd_add(int argc,
 		string_list_clear(&only_match_skip_worktree, 0);
 	}
 
-	transaction = odb_transaction_begin(repo->objects);
+	odb_transaction_begin_or_die(repo->objects, &transaction);
 
 	ps_matched = xcalloc(pathspec.nr, 1);
 	if (add_renormalize)
diff --git a/builtin/unpack-objects.c b/builtin/unpack-objects.c
index f3849bb654..d0136cdd99 100644
--- a/builtin/unpack-objects.c
+++ b/builtin/unpack-objects.c
@@ -598,7 +598,7 @@ static void unpack_all(void)
 		progress = start_progress(the_repository,
 					  _("Unpacking objects"), nr_objects);
 	CALLOC_ARRAY(obj_list, nr_objects);
-	transaction = odb_transaction_begin(the_repository->objects);
+	odb_transaction_begin_or_die(the_repository->objects, &transaction);
 	for (i = 0; i < nr_objects; i++) {
 		unpack_one(i);
 		display_progress(progress, i + 1);
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 3d6646c318..17f3ea284c 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -1124,7 +1124,7 @@ int cmd_update_index(int argc,
 	 * Allow the object layer to optimize adding multiple objects in
 	 * a batch.
 	 */
-	transaction = odb_transaction_begin(the_repository->objects);
+	odb_transaction_begin_or_die(the_repository->objects, &transaction);
 	while (ctx.argc) {
 		if (parseopt_state != PARSE_OPT_DONE)
 			parseopt_state = parse_options_step(&ctx, options,
diff --git a/cache-tree.c b/cache-tree.c
index 184f7e2635..8eec1d4d52 100644
--- a/cache-tree.c
+++ b/cache-tree.c
@@ -474,6 +474,7 @@ static int update_one(struct cache_tree *it,
 
 int cache_tree_update(struct index_state *istate, int flags)
 {
+	int inflight = !!the_repository->objects->transaction;
 	struct odb_transaction *transaction;
 	int skip, i;
 
@@ -490,10 +491,12 @@ int cache_tree_update(struct index_state *istate, int flags)
 
 	trace_performance_enter();
 	trace2_region_enter("cache_tree", "update", istate->repo);
-	transaction = odb_transaction_begin(the_repository->objects);
+	if (!inflight)
+		odb_transaction_begin_or_die(the_repository->objects, &transaction);
 	i = update_one(istate->cache_tree, istate->cache, istate->cache_nr,
 		       "", 0, &skip, flags);
-	odb_transaction_commit(transaction);
+	if (!inflight)
+		odb_transaction_commit(transaction);
 	trace2_region_leave("cache_tree", "update", istate->repo);
 	trace_performance_leave("cache_tree_update");
 	if (i < 0)
diff --git a/object-file.c b/object-file.c
index 3651605ea2..358684beae 100644
--- a/object-file.c
+++ b/object-file.c
@@ -1354,13 +1354,17 @@ int index_fd(struct index_state *istate, struct object_id *oid,
 
 		if (flags & INDEX_WRITE_OBJECT) {
 			struct object_database *odb = the_repository->objects;
-			struct odb_transaction *transaction = odb_transaction_begin(odb);
+			struct odb_transaction *transaction = odb->transaction;
+			int inflight = !!transaction;
 
-			ret = odb_transaction_write_object_stream(odb->transaction,
+			if (!inflight)
+				odb_transaction_begin_or_die(odb, &transaction);
+			ret = odb_transaction_write_object_stream(transaction,
 								  &stream,
 								  xsize_t(st->st_size),
 								  oid);
-			odb_transaction_commit(transaction);
+			if (!inflight)
+				odb_transaction_commit(transaction);
 		} else {
 			ret = hash_blob_stream(&stream,
 					       the_repository->hash_algo, oid,
diff --git a/odb/transaction.c b/odb/transaction.c
index b16e07aebf..b6da4a3942 100644
--- a/odb/transaction.c
+++ b/odb/transaction.c
@@ -1,15 +1,21 @@
 #include "git-compat-util.h"
+#include "gettext.h"
 #include "odb/source.h"
 #include "odb/transaction.h"
 
-struct odb_transaction *odb_transaction_begin(struct object_database *odb)
+int odb_transaction_begin(struct object_database *odb,
+			  struct odb_transaction **out)
 {
+	int ret;
+
 	if (odb->transaction)
-		return NULL;
+		return error(_("object database transaction already pending"));
 
-	odb_source_begin_transaction(odb->sources, &odb->transaction);
+	ret = odb_source_begin_transaction(odb->sources, out);
+	if (!ret)
+		odb->transaction = *out;
 
-	return odb->transaction;
+	return ret;
 }
 
 void odb_transaction_commit(struct odb_transaction *transaction)
diff --git a/odb/transaction.h b/odb/transaction.h
index d52f0533ce..f5c43187c9 100644
--- a/odb/transaction.h
+++ b/odb/transaction.h
@@ -1,6 +1,7 @@
 #ifndef ODB_TRANSACTION_H
 #define ODB_TRANSACTION_H
 
+#include "gettext.h"
 #include "odb.h"
 #include "odb/source.h"
 
@@ -36,11 +37,21 @@ struct odb_transaction {
 };
 
 /*
- * Starts an ODB transaction. Subsequent objects are written to the transaction
- * and not committed until odb_transaction_commit() is invoked on the
- * transaction. If the ODB already has a pending transaction, NULL is returned.
+ * Starts an ODB transaction and returns it via `out`. Subsequent objects are
+ * written to the transaction and not committed until odb_transaction_commit()
+ * is invoked on the transaction. Returns 0 on success and a negative value on
+ * error. Note that it is considered an error to start a new transaction if the
+ * ODB already has an inflight transaction pending.
  */
-struct odb_transaction *odb_transaction_begin(struct object_database *odb);
+int odb_transaction_begin(struct object_database *odb,
+			  struct odb_transaction **out);
+
+static inline void odb_transaction_begin_or_die(struct object_database *odb,
+						struct odb_transaction **out)
+{
+	if (odb_transaction_begin(odb, out))
+		die(_("failed to start ODB transaction"));
+}
 
 /*
  * Commits an ODB transaction making the written objects visible. If the
diff --git a/read-cache.c b/read-cache.c
index 21ca58beea..d511d25834 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -4012,6 +4012,7 @@ int add_files_to_cache(struct repository *repo, const char *prefix,
 		       const struct pathspec *pathspec, char *ps_matched,
 		       int include_sparse, int flags, int ignored_too )
 {
+	int inflight = !!repo->objects->transaction;
 	struct odb_transaction *transaction;
 	struct update_callback_data data;
 	struct rev_info rev;
@@ -4042,9 +4043,11 @@ int add_files_to_cache(struct repository *repo, const char *prefix,
 	 * This function is invoked from commands other than 'add', which
 	 * may not have their own transaction active.
 	 */
-	transaction = odb_transaction_begin(repo->objects);
+	if (!inflight)
+		odb_transaction_begin_or_die(repo->objects, &transaction);
 	run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
-	odb_transaction_commit(transaction);
+	if (!inflight)
+		odb_transaction_commit(transaction);
 
 	release_revisions(&rev);
 	return !!data.add_errors;
-- 
2.55.0.122.gf85a7e6620


  parent reply	other threads:[~2026-07-10 16:37 UTC|newest]

Thread overview: 90+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-24  4:19 [PATCH 0/6] receive-pack: use ODB transactions to stage object writes Justin Tobler
2026-06-24  4:19 ` [PATCH 1/6] object-file: rename files transaction prepare function Justin Tobler
2026-06-24 18:26   ` Junio C Hamano
2026-06-29 18:11     ` Justin Tobler
2026-06-24  4:19 ` [PATCH 2/6] object-file: propagate files transaction errors Justin Tobler
2026-06-24 11:26   ` Patrick Steinhardt
2026-06-29 18:58     ` Justin Tobler
2026-06-29 19:04       ` Justin Tobler
2026-06-30  8:45         ` Patrick Steinhardt
2026-06-30 14:14           ` Justin Tobler
2026-06-30  8:45       ` Patrick Steinhardt
2026-06-24 18:35   ` Junio C Hamano
2026-06-29 19:10     ` Justin Tobler
2026-06-24  4:19 ` [PATCH 3/6] odb/transaction: propagate begin errors Justin Tobler
2026-06-24 11:26   ` Patrick Steinhardt
2026-06-29 19:15     ` Justin Tobler
2026-06-24  4:19 ` [PATCH 4/6] odb/transaction: propagate commit errors Justin Tobler
2026-06-24 11:26   ` Patrick Steinhardt
2026-06-29 19:16     ` Justin Tobler
2026-06-24  4:19 ` [PATCH 5/6] odb/transaction: add transaction env interface Justin Tobler
2026-06-24 11:26   ` Patrick Steinhardt
2026-06-29 19:20     ` Justin Tobler
2026-06-24  4:19 ` [PATCH 6/6] builtin/receive-pack: stage incoming objects via ODB transactions Justin Tobler
2026-06-24 11:26   ` Patrick Steinhardt
2026-06-29 20:25     ` Justin Tobler
2026-06-30  8:45       ` Patrick Steinhardt
2026-06-24 11:27 ` [PATCH 0/6] receive-pack: use ODB transactions to stage object writes Patrick Steinhardt
2026-06-24 20:09 ` Junio C Hamano
2026-07-08  4:14 ` [PATCH v2 00/11] " Justin Tobler
2026-07-08  4:14   ` [PATCH v2 01/11] object-file: rename files transaction prepare function Justin Tobler
2026-07-08  4:14   ` [PATCH v2 02/11] object-file: rename files transaction fsync function Justin Tobler
2026-07-08  6:41     ` Patrick Steinhardt
2026-07-08  4:14   ` [PATCH v2 03/11] object-file: embed transaction flush logic in commit function Justin Tobler
2026-07-08  6:41     ` Patrick Steinhardt
2026-07-08 16:08       ` Justin Tobler
2026-07-08  4:14   ` [PATCH v2 04/11] object-file: drop check for inflight transactions Justin Tobler
2026-07-08  6:41     ` Patrick Steinhardt
2026-07-08  4:14   ` [PATCH v2 05/11] object-file: propagate files transaction errors Justin Tobler
2026-07-08  6:41     ` Patrick Steinhardt
2026-07-08 16:21       ` Justin Tobler
2026-07-08  4:14   ` [PATCH v2 06/11] odb/transaction: propagate begin errors Justin Tobler
2026-07-08  6:41     ` Patrick Steinhardt
2026-07-08 16:56       ` Justin Tobler
2026-07-09  9:39         ` Patrick Steinhardt
2026-07-08  4:14   ` [PATCH v2 07/11] odb/transaction: propagate commit errors Justin Tobler
2026-07-08  6:41     ` Patrick Steinhardt
2026-07-08 17:24       ` Justin Tobler
2026-07-08  4:14   ` [PATCH v2 08/11] odb/transaction: add transaction env interface Justin Tobler
2026-07-08  4:14   ` [PATCH v2 09/11] odb/transaction: introduce ODB transaction flags Justin Tobler
2026-07-08  6:41     ` Patrick Steinhardt
2026-07-08 17:34       ` Justin Tobler
2026-07-08  4:14   ` [PATCH v2 10/11] builtin/receive-pack: drop redundant tmpdir env Justin Tobler
2026-07-08  6:41     ` Patrick Steinhardt
2026-07-08  4:14   ` [PATCH v2 11/11] builtin/receive-pack: stage incoming objects via ODB transactions Justin Tobler
2026-07-08  6:42   ` [PATCH v2 00/11] receive-pack: use ODB transactions to stage object writes Patrick Steinhardt
2026-07-08 17:36     ` Justin Tobler
2026-07-08 23:59   ` [PATCH v3 " Justin Tobler
2026-07-08 23:59     ` [PATCH v3 01/11] object-file: rename files transaction prepare function Justin Tobler
2026-07-08 23:59     ` [PATCH v3 02/11] object-file: rename files transaction fsync function Justin Tobler
2026-07-08 23:59     ` [PATCH v3 03/11] object-file: embed transaction flush logic in commit function Justin Tobler
2026-07-09  9:38       ` Patrick Steinhardt
2026-07-08 23:59     ` [PATCH v3 04/11] object-file: drop check for inflight transactions Justin Tobler
2026-07-08 23:59     ` [PATCH v3 05/11] object-file: propagate files transaction errors Justin Tobler
2026-07-08 23:59     ` [PATCH v3 06/11] odb/transaction: propagate begin errors Justin Tobler
2026-07-09  3:32       ` Junio C Hamano
2026-07-09 14:03         ` Justin Tobler
2026-07-08 23:59     ` [PATCH v3 07/11] odb/transaction: propagate commit errors Justin Tobler
2026-07-08 23:59     ` [PATCH v3 08/11] odb/transaction: add transaction env interface Justin Tobler
2026-07-09  3:36       ` Junio C Hamano
2026-07-09 15:02         ` Justin Tobler
2026-07-08 23:59     ` [PATCH v3 09/11] odb/transaction: introduce ODB transaction flags Justin Tobler
2026-07-08 23:59     ` [PATCH v3 10/11] builtin/receive-pack: drop redundant tmpdir env Justin Tobler
2026-07-08 23:59     ` [PATCH v3 11/11] builtin/receive-pack: stage incoming objects via ODB transactions Justin Tobler
2026-07-09  3:49       ` Junio C Hamano
2026-07-10 14:37         ` Justin Tobler
2026-07-10 16:52           ` Junio C Hamano
2026-07-09  9:39     ` [PATCH v3 00/11] receive-pack: use ODB transactions to stage object writes Patrick Steinhardt
2026-07-10 16:37     ` [PATCH v4 " Justin Tobler
2026-07-10 16:37       ` [PATCH v4 01/11] object-file: rename files transaction prepare function Justin Tobler
2026-07-10 16:37       ` [PATCH v4 02/11] object-file: rename files transaction fsync function Justin Tobler
2026-07-10 16:37       ` [PATCH v4 03/11] object-file: embed transaction flush logic in commit function Justin Tobler
2026-07-10 16:37       ` [PATCH v4 04/11] object-file: drop check for inflight transactions Justin Tobler
2026-07-10 16:37       ` [PATCH v4 05/11] object-file: propagate files transaction errors Justin Tobler
2026-07-10 16:37       ` Justin Tobler [this message]
2026-07-10 16:37       ` [PATCH v4 07/11] odb/transaction: propagate commit errors Justin Tobler
2026-07-10 16:37       ` [PATCH v4 08/11] odb/transaction: add transaction env interface Justin Tobler
2026-07-10 16:37       ` [PATCH v4 09/11] odb/transaction: introduce ODB transaction flags Justin Tobler
2026-07-10 16:37       ` [PATCH v4 10/11] builtin/receive-pack: drop redundant tmpdir env Justin Tobler
2026-07-10 16:37       ` [PATCH v4 11/11] builtin/receive-pack: stage incoming objects via ODB transactions Justin Tobler
2026-07-13  5:18       ` [PATCH v4 00/11] receive-pack: use ODB transactions to stage object writes Patrick Steinhardt

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=20260710163722.2962278-7-jltobler@gmail.com \
    --to=jltobler@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.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