git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[RFC PATCH] Modify fetch-pack to no longer die on error?

From
JTJonathan Tan <jonathantanmy@google.com>
Date
Jul 24, 2020, 22:38 UTC
Message-ID
<20200724223844.2723397-1-jonathantanmy@google.com>

We've had a few instances where a lazy fetch in a partial clone fails, leading to a fatal error, when the calling code could have easily recovered - in other words, the severity of the bug should have just a wasted fetch instead of stopping the whole command. Part of the issue (and possibly the whole issue - I haven't looked at this beyond fetch-pack yet) is that fetch-pack dies whenever it encounters an error, so I took a look at fixing that.

(Note that fetch-pack is sometimes run through a remote helper, meaning that we could leave the die() invocations in and just make sure that we handle failure in the separate process correctly. But when the promisor remote is HTTP protocol v2 or SSH protocol v0/v2, this is not true - fetch_pack() is run in-process.)

I think the best way for easy authorship and review is to convert each possibly-dying function into a function that either returns a possibly-null error string or returns success/failure and writes the error string into an "out" parameter. In this way, the change is rather mechanical and should be easy to review. In the patch below I chose the former approach, and I modified 2 functions (one that returns no value and one that returns a value) to demonstrate what it would look like.

We could also take this further and have a "struct error" for type safety and macros - e.g. THROW() to return a "struct error", TRY() to execute what's inside the parentheses and return the error if there is one, and OR_DIE() to execute what's inside the parentheses and die if there is an error.

Any opinions before I continue working on this?
Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
---
 fetch-pack.c | 78 +++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 53 insertions(+), 25 deletions(-)
diff --git a/fetch-pack.c b/fetch-pack.c
index 80fb3bd899..20a7e05ea8 100644
--- a/fetch-pack.c
+++ b/fetch-pack.c
@@ -24,6 +24,8 @@
 #include "fsck.h"
 #include "shallow.h"
 
+typedef char * error_string;
+
 static int transfer_unpack_limit = -1;
 static int fetch_unpack_limit = -1;
 static int unpack_limit = 100;
@@ -136,8 +138,8 @@ enum ack_type {
 	ACK_ready
 };
 
-static void consume_shallow_list(struct fetch_pack_args *args,
-				 struct packet_reader *reader)
+static error_string consume_shallow_list(struct fetch_pack_args *args,
+					 struct packet_reader *reader)
 {
 	if (args->stateless_rpc && args->deepen) {
 		/* If we sent a depth we will get back "duplicate"
@@ -149,41 +151,54 @@ static void consume_shallow_list(struct fetch_pack_args *args,
 				continue;
 			if (starts_with(reader->line, "unshallow "))
 				continue;
-			die(_("git fetch-pack: expected shallow list"));
+			return xstrdup(_("git fetch-pack: expected shallow list"));
 		}
 		if (reader->status != PACKET_READ_FLUSH)
-			die(_("git fetch-pack: expected a flush packet after shallow list"));
+			return xstrdup(_("git fetch-pack: expected a flush packet after shallow list"));
 	}
+	return NULL;
 }
 
-static enum ack_type get_ack(struct packet_reader *reader,
-			     struct object_id *result_oid)
+static error_string get_ack(struct packet_reader *reader,
+			    enum ack_type *result_ack,
+			    struct object_id *result_oid)
 {
 	int len;
 	const char *arg;
 
 	if (packet_reader_read(reader) != PACKET_READ_NORMAL)
-		die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
+		return xstrdup(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
 	len = reader->pktlen;
 
-	if (!strcmp(reader->line, "NAK"))
-		return NAK;
+	if (!strcmp(reader->line, "NAK")) {
+		*result_ack = NAK;
+		return NULL;
+	}
 	if (skip_prefix(reader->line, "ACK ", &arg)) {
 		const char *p;
 		if (!parse_oid_hex(arg, result_oid, &p)) {
 			len -= p - reader->line;
-			if (len < 1)
-				return ACK;
-			if (strstr(p, "continue"))
-				return ACK_continue;
-			if (strstr(p, "common"))
-				return ACK_common;
-			if (strstr(p, "ready"))
-				return ACK_ready;
-			return ACK;
+			if (len < 1) {
+				*result_ack = ACK;
+				return NULL;
+			}
+			if (strstr(p, "continue")) {
+				*result_ack = ACK_continue;
+				return NULL;
+			}
+			if (strstr(p, "common")) {
+				*result_ack = ACK_common;
+				return NULL;
+			}
+			if (strstr(p, "ready")) {
+				*result_ack = ACK_ready;
+				return NULL;
+			}
+			*result_ack = ACK;
+			return NULL;
 		}
 	}
-	die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line);
+	return xstrfmt(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line);
 }
 
 static void send_request(struct fetch_pack_args *args,
@@ -394,7 +409,8 @@ static int find_common(struct fetch_negotiator *negotiator,
 		print_verbose(args, "have %s", oid_to_hex(oid));
 		in_vain++;
 		if (flush_at <= ++count) {
-			int ack;
+			enum ack_type ack;
+			error_string err;
 
 			packet_buf_flush(&req_buf);
 			send_request(args, fd[1], &req_buf);
@@ -409,9 +425,11 @@ static int find_common(struct fetch_negotiator *negotiator,
 			if (!args->stateless_rpc && count == INITIAL_FLUSH)
 				continue;
 
-			consume_shallow_list(args, &reader);
+			if ((err = consume_shallow_list(args, &reader)))
+				die("%s", err);
 			do {
-				ack = get_ack(&reader, result_oid);
+				if ((err = get_ack(&reader, &ack, result_oid)))
+					die("%s", err);
 				if (ack)
 					print_verbose(args, _("got %s %d %s"), "ack",
 						      ack, oid_to_hex(result_oid));
@@ -457,6 +475,9 @@ static int find_common(struct fetch_negotiator *negotiator,
 						got_ready = 1;
 					break;
 					}
+				case NAK:
+					/* nothing */
+					break;
 				}
 			} while (ack);
 			flushes--;
@@ -481,10 +502,17 @@ static int find_common(struct fetch_negotiator *negotiator,
 	}
 	strbuf_release(&req_buf);
 
-	if (!got_ready || !no_done)
-		consume_shallow_list(args, &reader);
+	if (!got_ready || !no_done) {
+		error_string err;
+		if ((err = consume_shallow_list(args, &reader)))
+			die("%s", err);
+	}
 	while (flushes || multi_ack) {
-		int ack = get_ack(&reader, result_oid);
+		error_string err;
+		enum ack_type ack;
+
+		if ((err = get_ack(&reader, &ack, result_oid)))
+			die("%s", err);
 		if (ack) {
 			print_verbose(args, _("got %s (%d) %s"), "ack",
 				      ack, oid_to_hex(result_oid));
-- 
2.28.0.rc0.142.g3c755180ce-goog
Next: Junio C Hamano
Message 1 of 57 in “Modify fetch-pack to no longer die on error?”
  1. Modify fetch-pack to no longer die on error?Jonathan Tan, Jul 24, 2020
  2. Junio C HamanoJul 24, 2020
  3. Junio C HamanoJul 24, 2020
  4. Jeff KingJul 25, 2020
  5. Junio C HamanoJul 25, 2020
  6. Jeff KingJul 27, 2020
  7. Jonathan TanJul 28, 2020
  8. Jeff KingJul 28, 2020
  9. Jonathan TanJul 29, 2020
  10. Jeff KingJul 29, 2020
  11. Junio C HamanoJul 29, 2020
  12. Jonathan TanJul 29, 2020
  13. 0/7 Lazy fetch with subprocessJonathan Tan, Aug 5, 2020
  14. 1/7 fetch-pack: allow NULL negotiator->add_tipJonathan Tan, Aug 5, 2020
  15. Junio C HamanoAug 5, 2020
  16. Jonathan TanAug 7, 2020
  17. 3/7 negotiator/null: add null fetch negotiatorJonathan Tan, Aug 5, 2020
  18. 2/7 fetch-pack: allow NULL negotiator->known_commonJonathan Tan, Aug 5, 2020
  19. Junio C HamanoAug 5, 2020
  20. Junio C HamanoAug 5, 2020
  21. Jonathan TanAug 7, 2020
  22. 4/7 fetch: --stdinJonathan Tan, Aug 5, 2020
  23. Junio C HamanoAug 5, 2020
  24. Jonathan TanAug 7, 2020
  25. Junio C HamanoAug 7, 2020
  26. Jonathan TanAug 7, 2020
  27. 5/7 fetch: submodule configJonathan Tan, Aug 5, 2020
  28. 7/7 promisor-remote: use subprocess to fetchJonathan Tan, Aug 5, 2020
  29. 6/7 fetch: only populate existing_refs if neededJonathan Tan, Aug 5, 2020
  30. 0/7 Lazy fetch with subprocessJonathan Tan, Aug 11, 2020
  31. 1/7 negotiator/null: add null fetch negotiatorJonathan Tan, Aug 11, 2020
  32. Derrick StoleeAug 12, 2020
  33. Junio C HamanoAug 12, 2020
  34. Jonathan TanAug 12, 2020
  35. 2/7 fetch: allow refspecs specified through stdinJonathan Tan, Aug 11, 2020
  36. 3/7 fetch: avoid reading submodule config until neededJonathan Tan, Aug 11, 2020
  37. Junio C HamanoAug 12, 2020
  38. 4/7 fetch: only populate existing_refs if neededJonathan Tan, Aug 11, 2020
  39. Junio C HamanoAug 12, 2020
  40. 5/7 fetch-pack: do not lazy-fetch during ref iterationJonathan Tan, Aug 11, 2020
  41. Junio C HamanoAug 12, 2020
  42. 6/7 promisor-remote: lazy-fetch objects in subprocessJonathan Tan, Aug 11, 2020
  43. Junio C HamanoAug 12, 2020
  44. 7/7 fetch-pack: remove no_dependents codeJonathan Tan, Aug 11, 2020
  45. Derrick StoleeAug 12, 2020
  46. 0/7 Lazy fetch with subprocessJonathan Tan, Aug 18, 2020
  47. 1/7 negotiator/noop: add noop fetch negotiatorJonathan Tan, Aug 18, 2020
  48. 2/7 fetch: allow refspecs specified through stdinJonathan Tan, Aug 18, 2020
  49. 4/7 fetch: only populate existing_refs if neededJonathan Tan, Aug 18, 2020
  50. 5/7 fetch-pack: do not lazy-fetch during ref iterationJonathan Tan, Aug 18, 2020
  51. 3/7 fetch: avoid reading submodule config until neededJonathan Tan, Aug 18, 2020
  52. 6/7 promisor-remote: lazy-fetch objects in subprocessJonathan Tan, Aug 18, 2020
  53. 7/7 fetch-pack: remove no_dependents codeJonathan Tan, Aug 18, 2020
  54. Junio C HamanoAug 18, 2020
  55. Junio C HamanoAug 18, 2020
  56. fixup! promisor-remote: lazy-fetch objects in subprocessJonathan Tan, Aug 18, 2020
  57. Junio C HamanoAug 18, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.