Skip to contentSkip to conversation

object-file: small error handling follow-ups

PATCH In review Justin Tobler 5 participants 13 messages Nov 3 – Nov 10

lore
Share & tools
lore
#0 lore raw

cover object-file: small error handling follow-ups

Greetings,

This small series is a follow-up to jt/odb-keep-lockfile-fix to address some feedback that was submitted after the series was merged to next. Both patches here make some small improvements to some error handling code.

Thanks,
-Justin

Justin Tobler (2):
object-file: warn on failed unlink
object-file: return error on index-pack abnormal exit

 object-file.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)

base-commit: 750f5de7dfe9a4062f95088212b063f189d50e91

Signature · 1 line
2.56.0.173.gf57c9a7388
#1 lore raw

PATCH 1/2 object-file: warn on failed unlink

When the "files" ODB transaction cleans up a temporary packfile or ".keep" lockfile on an error path, any failure to unlink the file is silently ignored. A ".keep" file left behind this way prevents its packfile from ever being removed by housekeeping.

Use unlink_or_warn() instead so that such failures are reported.

---
 object-file.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
object-file.c +3 -3
index 132aaab6f8..a9800143bd 100644
@@ -823,7 +823,7 @@ static void flush_packfile_transaction(struct odb_transaction_files *transaction
 	if (state->nr_written == 0) {
 		close(state->f->fd);
 		free_hashfile(state->f);
-		unlink(state->pack_tmp_name);
+		unlink_or_warn(state->pack_tmp_name);
 		goto clear_exit;
 	} else if (state->nr_written == 1) {
 		finalize_hashfile(state->f, hash, FSYNC_COMPONENT_PACK,
@@ -1276,13 +1276,13 @@ static int write_pack_lockfile(struct odb_source *source, const char *pack_name,
 	if (write_in_full(fd, contents.buf, contents.len) < 0) {
 		ret = error_errno(_("unable to write '%s'"), lock_path.buf);
 		close(fd);
-		unlink(lock_path.buf);
+		unlink_or_warn(lock_path.buf);
 		goto out;
 	}
 
 	if (close(fd) < 0) {
 		ret = error_errno(_("unable to close '%s'"), lock_path.buf);
-		unlink(lock_path.buf);
+		unlink_or_warn(lock_path.buf);
 		goto out;
 	}
 
Signature · 1 line
2.56.0.173.gf57c9a7388
#2 lore raw

Re: [PATCH 1/2] object-file: warn on failed unlink

Quoted 30 lines · Justin Tobler

When the "files" ODB transaction cleans up a temporary packfile or ".keep" lockfile on an error path, any failure to unlink the file is silently ignored. A ".keep" file left behind this way prevents its packfile from ever being removed by housekeeping.

Use unlink_or_warn() instead so that such failures are reported.

---
 object-file.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
object-file.c +1 -2
index 132aaab6f8..a9800143bd 100644
@@ -823,7 +823,7 @@ static void flush_packfile_transaction(struct odb_transaction_files *transaction
 	if (state->nr_written == 0) {
 		close(state->f->fd);
 		free_hashfile(state->f);
-		unlink(state->pack_tmp_name);
+		unlink_or_warn(state->pack_tmp_name);
 		goto clear_exit;
 	} else if (state->nr_written == 1) {
 		finalize_hashfile(state->f, hash, FSYNC_COMPONENT_PACK,
@@ -1276,13 +1276,13 @@ static int write_pack_lockfile(struct odb_source *source, const char *pack_name,
 	if (write_in_full(fd, contents.buf, contents.len) < 0) {
 		ret = error_errno(_("unable to write '%s'"), lock_path.buf);
 		close(fd);
-		unlink(lock_path.buf);

object-file.c static int write_pack_lockfile(struct odb_source *source, const char *pack_name, · line 1287

Quoted 10 lines
object-file.c +2 -1
+		unlink_or_warn(lock_path.buf);
 		goto out;
 	}
 
 	if (close(fd) < 0) {
 		ret = error_errno(_("unable to close '%s'"), lock_path.buf);
-		unlink(lock_path.buf);
+		unlink_or_warn(lock_path.buf);
 		goto out;
 	}

Trivially correct.

#3 lore raw

PATCH 2/2 object-file: return error on index-pack abnormal exit

In reply to Justin Tobler

When git-index-pack(1) exits abnormally after reporting the packfile name, odb_transaction_files_write_pack() records an error message in err_msg but still returns success. Current callers ultimately discard the return value, so this does not cause any issues at present, but the return value should still reflect the failure.

Update odb_transaction_files_write_pack() to return an error value in this case accordingly. While at it, simplify how the child process exit statuses are checked.

---
 object-file.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
object-file.c +10 -10
index a9800143bd..e04ce0bc11 100644
@@ -1429,8 +1429,7 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,
 	struct pack_header hdr;
 	const char *hdr_err;
 	int err_fd = opts->err_fd;
-	int ret = -1;
-	int status;
+	int ret;
 
 	hdr_err = parse_pack_header(&hdr, pack_fd);
 	if (hdr_err) {
@@ -1462,12 +1461,13 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,
 		child.in = pack_fd;
 		child.err = err_fd;
 		child.git_cmd = 1;
-		status = run_command(&child);
-		if (status) {
+		if (run_command(&child)) {
 			strbuf_addstr(err_msg, "unpack-objects abnormal exit");
 			return -1;
 		}
 	} else {
+		int report_err;
+
 		strvec_pushl(&child.args, "index-pack", "--stdin", NULL);
 		push_header_arg(&child.args, &hdr);
 
@@ -1487,24 +1487,24 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,
 		child.in = pack_fd;
 		child.err = err_fd;
 		child.git_cmd = 1;
-		status = start_command(&child);
-		if (status) {
+		if (start_command(&child)) {
 			strbuf_addstr(err_msg, "index-pack fork failed");
 			return -1;
 		}
 
-		ret = read_index_pack_report(repo, child.out, &pack_name, NULL);
+		report_err = read_index_pack_report(repo, child.out, &pack_name, NULL);
 
 		close(child.out);
 
-		status = finish_command(&child);
-		if (status) {
+		if (finish_command(&child)) {
 			strbuf_addstr(err_msg, "index-pack abnormal exit");
+			ret = -1;
 			goto out;
 		}
 
-		if (ret) {
+		if (report_err) {
 			strbuf_addstr(err_msg, "index-pack failed to report pack name");
+			ret = -1;
 			goto out;
 		}
 
Signature · 1 line
2.56.0.173.gf57c9a7388
#4 lore raw

Re: [PATCH 2/2] object-file: return error on index-pack abnormal exit

Quoted 15 lines · Justin Tobler
object-file.c +1 -2
index a9800143bd..e04ce0bc11 100644
@@ -1429,8 +1429,7 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,
 	struct pack_header hdr;
 	const char *hdr_err;
 	int err_fd = opts->err_fd;
-	int ret = -1;
-	int status;
+	int ret;
 
 	hdr_err = parse_pack_header(&hdr, pack_fd);
 	if (hdr_err) {

object-file.c static int odb_transaction_files_write_pack(struct odb_transaction *base, · line 1467

Quoted 9 lines
object-file.c +1 -2
@@ -1462,12 +1461,13 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,
 		child.in = pack_fd;
 		child.err = err_fd;
 		child.git_cmd = 1;
-		status = run_command(&child);
-		if (status) {
+		if (run_command(&child)) {
 			strbuf_addstr(err_msg, "unpack-objects abnormal exit");
 			return -1;
 		}

OK. A slight simplication that does not change any behaviour.

Quoted 19 lines
object-file.c +4 -3
 	} else {
+		int report_err;
+
 		strvec_pushl(&child.args, "index-pack", "--stdin", NULL);
 		push_header_arg(&child.args, &hdr);
 
@@ -1487,24 +1487,24 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,
 		child.in = pack_fd;
 		child.err = err_fd;
 		child.git_cmd = 1;
-		status = start_command(&child);
-		if (status) {
+		if (start_command(&child)) {
 			strbuf_addstr(err_msg, "index-pack fork failed");
 			return -1;
 		}
 
-		ret = read_index_pack_report(repo, child.out, &pack_name, NULL);
+		report_err = read_index_pack_report(repo, child.out, &pack_name, NULL);

object-file.c static int odb_transaction_files_write_pack(struct odb_transaction *base, · line 1503

Quoted 10 lines
object-file.c +2 -2
 
 		close(child.out);
 
-		status = finish_command(&child);
-		if (status) {
+		if (finish_command(&child)) {
 			strbuf_addstr(err_msg, "index-pack abnormal exit");
+			ret = -1;
 			goto out;
 		}

OK. A slight simplication that does not change any behaviour again.

object-file.c static int odb_transaction_files_write_pack(struct odb_transaction *base, · line 1509

Quoted 6 lines
object-file.c +2 -1
-		if (ret) {
+		if (report_err) {
 			strbuf_addstr(err_msg, "index-pack failed to report pack name");
+			ret = -1;
 			goto out;
 		}

I guess this also does not change a behaviour? We used to relay the error status returned by read_index_pack_report(), but now we force the error code to an explicit -1.

Personally, I somehow find that the original "unless we positively declare success, default to 'ret = -1', and if we may have detected more specific error, return it back, and reset ret to 0 when we see a success" easier to follow, but that may be subjective preference.

Will queue. Thanks.

#5 lore raw

Re: [PATCH 2/2] object-file: return error on index-pack abnormal exit

Quoted 11 lines · Junio C Hamano

object-file.c static int odb_transaction_files_write_pack(struct odb_transaction *base, · line 1509

Quoted 6 lines · Justin Tobler
object-file.c +2 -1
-		if (ret) {
+		if (report_err) {
 			strbuf_addstr(err_msg, "index-pack failed to report pack name");
+			ret = -1;
 			goto out;
 		}

I guess this also does not change a behaviour? We used to relay the error status returned by read_index_pack_report(), but now we force the error code to an explicit -1.

That is correct, there is no behavior change here. The main change is that finish_command() properly reports its error and is not overwritten by read_index_pack_report().

[snip]

Quoted 2 lines

Will queue. Thanks.

Thanks for the review,
-Justin

#6 lore raw

Re: [PATCH 2/2] object-file: return error on index-pack abnormal exit

In reply to Junio C Hamano
Quoted 37 lines · Junio C Hamano
Quoted 13 lines
object-file.c +2 -3
@@ -1487,24 +1487,24 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,
  		child.in = pack_fd;
  		child.err = err_fd;
  		child.git_cmd = 1;
-		status = start_command(&child);
-		if (status) {
+		if (start_command(&child)) {
  			strbuf_addstr(err_msg, "index-pack fork failed");
  			return -1;
  		}
 
-		ret = read_index_pack_report(repo, child.out, &pack_name, NULL);
+		report_err = read_index_pack_report(repo, child.out, &pack_name, NULL);

object-file.c static int odb_transaction_files_write_pack(struct odb_transaction *base, · line 1503

Quoted 10 lines
object-file.c +2 -2
 
  		close(child.out);
 
-		status = finish_command(&child);
-		if (status) {
+		if (finish_command(&child)) {
  			strbuf_addstr(err_msg, "index-pack abnormal exit");
+			ret = -1;
  			goto out;
  		}

OK. A slight simplication that does not change any behaviour again.

object-file.c static int odb_transaction_files_write_pack(struct odb_transaction *base, · line 1509

Quoted 6 lines
object-file.c +2 -1
-		if (ret) {
+		if (report_err) {
  			strbuf_addstr(err_msg, "index-pack failed to report pack name");
+			ret = -1;
  			goto out;
  		}

I guess this also does not change a behaviour? We used to relay the error status returned by read_index_pack_report(), but now we force the error code to an explicit -1.

Quoted 5 lines

Personally, I somehow find that the original "unless we positively declare success, default to 'ret = -1', and if we may have detected more specific error, return it back, and reset ret to 0 when we see a success" easier to follow, but that may be subjective preference.

+1. I too have a slight preference for the old flow of having ret = -1 and resetting it when there's no error. It may not be worth a re-roll, though.

Otherwise, both patches look good to me.

Signature · 1 line
Sivaraam
#7 lore raw

Re: [PATCH 2/2] object-file: return error on index-pack abnormal exit

Quoted 8 lines · Kaartic Sivaraam
Quoted 4 lines

Personally, I somehow find that the original "unless we positively declare success, default to 'ret = -1', and if we may have detected more specific error, return it back, and reset ret to 0 when we see a success" easier to follow, but that may be subjective preference.

+1. I too have a slight preference for the old flow of having ret = -1 and resetting it when there's no error. It may not be worth a re-roll, though.

Note that my comment you quoted was made with a misunderstanding of the patch and misses one important point, which is that read_index_pack_report() used to overwrite "ret" even when it succeeded, but the new code uses a separate variable to avoid clobbering "ret" when the call succeeds.

In other words, the original failed to honor the "have ret = -1 and reset to 0 only after seeing everything go well" pattern, either, and Justin's patch is a definite improvement.

#8 lore raw

Re: [PATCH 2/2] object-file: return error on index-pack abnormal exit

Quoted 17 lines · Junio C Hamano
Quoted 8 lines · Kaartic Sivaraam
Quoted 4 lines

Personally, I somehow find that the original "unless we positively declare success, default to 'ret = -1', and if we may have detected more specific error, return it back, and reset ret to 0 when we see a success" easier to follow, but that may be subjective preference.

+1. I too have a slight preference for the old flow of having ret = -1 and resetting it when there's no error. It may not be worth a re-roll, though.

Note that my comment you quoted was made with a misunderstanding of the patch and misses one important point, which is that read_index_pack_report() used to overwrite "ret" even when it succeeded, but the new code uses a separate variable to avoid clobbering "ret" when the call succeeds.

Quoted 3 lines

In other words, the original failed to honor the "have ret = -1 and reset to 0 only after seeing everything go well" pattern, either, and Justin's patch is a definite improvement.

It is an improvement indeed. No arguments about it. I was just wondering if his change could also include a change like this on top:

object-file.c +1 -3
index e04ce0bc11..c52004014f 100644
@@ -1429,7 +1429,7 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,
 	struct pack_header hdr;
 	const char *hdr_err;
 	int err_fd = opts->err_fd;
-	int ret;
+	int ret = -1;
 
 	hdr_err = parse_pack_header(&hdr, pack_fd);
 	if (hdr_err) {
@@ -1498,13 +1498,11 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,
 
 		if (finish_command(&child)) {
 			strbuf_addstr(err_msg, "index-pack abnormal exit");
-			ret = -1;
 			goto out;
 		}
 
 		if (report_err) {
 			strbuf_addstr(err_msg, "index-pack failed to report pack name");
-			ret = -1;
 			goto out;
 		}
 

But, I would agree that this is a digression.

Signature · 1 line
Sivaraam
#9 lore raw

Re: [PATCH 2/2] object-file: return error on index-pack abnormal exit

Quoted 33 lines · Kaartic Sivaraam

It is an improvement indeed. No arguments about it. I was just wondering if his change could also include a change like this on top:

object-file.c +1 -1
index e04ce0bc11..c52004014f 100644
@@ -1429,7 +1429,7 @@ static int odb_transaction_files_write_pack(struct
 odb_transaction *base,
 	struct pack_header hdr;
 	const char *hdr_err;
 	int err_fd = opts->err_fd;
-	int ret;
+	int ret = -1;
 
 	hdr_err = parse_pack_header(&hdr, pack_fd);
object-file.c +0 -2
 	if (hdr_err) {
@@ -1498,13 +1498,11 @@ static int odb_transaction_files_write_pack(struct
 odb_transaction *base,
 
 		if (finish_command(&child)) {
 			strbuf_addstr(err_msg, "index-pack abnormal exit");
-			ret = -1;
 			goto out;
 		}
 
 		if (report_err) {
 			strbuf_addstr(err_msg, "index-pack failed to report pack name");
-			ret = -1;
 			goto out;
 		}
Quoted 1 line

But, I would agree that this is a digression.

I think this is mostly just a matter of style preference. I'm inclined to leave it as-is, but an open to change it anyone feels strongly.

-Justin

#10 lore raw

Re: [PATCH 2/2] object-file: return error on index-pack abnormal exit

In reply to Justin Tobler
Quoted 5 lines · Justin Tobler

When git-index-pack(1) exits abnormally after reporting the packfile name, odb_transaction_files_write_pack() records an error message in err_msg but still returns success. Current callers ultimately discard the return value, so this does not cause any issues at present, but the return value should still reflect the failure.

Is this a theoretical concern or does git-index-pack(1) perform any important steps after reporting the packfile name? I think we should of course respect errors in either case, but I wonder whether this is a bug fix or whether it's a simple cleanup.

Patrick

#11 lore raw

Re: [PATCH 2/2] object-file: return error on index-pack abnormal exit

Quoted 11 lines · Patrick Steinhardt
Quoted 5 lines · Justin Tobler

When git-index-pack(1) exits abnormally after reporting the packfile name, odb_transaction_files_write_pack() records an error message in err_msg but still returns success. Current callers ultimately discard the return value, so this does not cause any issues at present, but the return value should still reflect the failure.

Is this a theoretical concern or does git-index-pack(1) perform any important steps after reporting the packfile name? I think we should of course respect errors in either case, but I wonder whether this is a bug fix or whether it's a simple cleanup.

This is a theoretical concern. If read_index_pack_report() returned success and finish_command() returned an error, odb_transaction_files_write_pack() would append an error message but return 0. This isn't really the correct behavior. In this case though, it doesn't cause any bugs because the return value is ultimately discarded by its callers.

-Justin

#12 lore raw
In reply to Justin Tobler
Quoted 13 lines · Justin Tobler

Greetings,

This small series is a follow-up to jt/odb-keep-lockfile-fix to address some feedback that was submitted after the series was merged to next. Both patches here make some small improvements to some error handling code.

Thanks,
-Justin

Justin Tobler (2):
object-file: warn on failed unlink
object-file: return error on index-pack abnormal exit

Quoted 7 lines
 object-file.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)

base-commit: 750f5de7dfe9a4062f95088212b063f189d50e91 --
2.56.0.173.gf57c9a7388

So these seem to be the fixes for the comments I raised on your other series. The patches look good. Thanks for following up!