Skip to contentSkip to conversation

Change xwrite() to write_in_full() in builtins.

PATCH In review Randall S. Becker 4 participants 12 messages Feb 27, 2024 – Mar 7, 2024

lore
Share & tools
lore
#0 lore raw

v2 cover Change xwrite() to write_in_full() in builtins.

From: "Randall S. Becker" <[email protected]>

This series replaces xwrite to write_in_full in builtins/. The change is required to fix critical problems that prevent full writes to be processed by on platforms where xwrite may be limited to a platform size limit. Further changes outside of builtins/ may be required but do not appear to be as urgent as this change, which causes test breakage in t7704. A separate series will be contributed for changes outside of builtins/ at a later date.

The change in unpack-objects.c is necessary as len is being passed into xwrite that exceeds the size supported by the limit in that method (56Kb on NonStop ia64).

Randall S. Becker (3):
builtin/repack.c: change xwrite to write_in_full and report errors. builtin/receive-pack.c: change xwrite to write_in_full. builtin/unpack-objects.c: change xwrite to write_in_full avoid
truncation.

 builtin/receive-pack.c   | 2 +-
 builtin/repack.c         | 9 +++++++--
 builtin/unpack-objects.c | 2 +-
 3 files changed, 9 insertions(+), 4 deletions(-)
Signature · 1 line
2.42.1
#1 lore raw

PATCH v2 1/3 builtin/repack.c: change xwrite to write_in_full and report errors.

From: "Randall S. Becker" <[email protected]>

This change is required because some platforms do not support file writes of arbitrary sizes (e.g, NonStop). xwrite ends up truncating the output to the maximum single I/O size possible for the destination device. The result of write_in_full() is also passed to the caller, which was previously ignored.

---
 builtin/repack.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
builtin/repack.c +7 -2
index ede36328a3..932d24c60b 100644
@@ -307,6 +307,7 @@ static int write_oid(const struct object_id *oid,
 		     struct packed_git *pack UNUSED,
 		     uint32_t pos UNUSED, void *data)
 {
+	int err;
 	struct child_process *cmd = data;
 
 	if (cmd->in == -1) {
@@ -314,8 +315,12 @@ static int write_oid(const struct object_id *oid,
 			die(_("could not start pack-objects to repack promisor objects"));
 	}
 
-	xwrite(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz);
-	xwrite(cmd->in, "\n", 1);
+	err = write_in_full(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz);
+	if (err <= 0)
+		return err;
+	err = write_in_full(cmd->in, "\n", 1);
+	if (err <= 0)
+		return err;
 	return 0;
 }
 
Signature · 1 line
2.42.1
#2 lore raw

Re: [PATCH v2 1/3] builtin/repack.c: change xwrite to write_in_full and report errors.

Quoted 6 lines · Randall S. Becker

From: "Randall S. Becker" <[email protected]>

This change is required because some platforms do not support file writes of arbitrary sizes (e.g, NonStop). xwrite ends up truncating the output to the maximum single I/O size possible for the destination device. The result of write_in_full() is also passed to the caller, which was previously ignored.

This misleads readers to think that maximum single I/O size is smaller than a single write of oid_to_hex() string on some platforms. I somehow do not think that is why we want to make this change.

Rather, the use of these xwrites() are simply wrong regardless of maximum I/O size of the platforms, as this caller is not prepared to see xwrite() result in a short write(2), and we do want to write all bytes we have even in such a case.

You're right to also point out that we attempt to propagate the errors to the caller (but see below).

Quoted 21 lines
---
 builtin/repack.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
builtin/repack.c +1 -0
index ede36328a3..932d24c60b 100644
@@ -307,6 +307,7 @@ static int write_oid(const struct object_id *oid,
 		     struct packed_git *pack UNUSED,
 		     uint32_t pos UNUSED, void *data)
 {
+	int err;
 	struct child_process *cmd = data;
 
 	if (cmd->in == -1) {
@@ -314,8 +315,12 @@ static int write_oid(const struct object_id *oid,
 			die(_("could not start pack-objects to repack promisor objects"));
 	}
 

builtin/repack.c static int write_oid(const struct object_id *oid, · line 325

Quoted 10 lines
builtin/repack.c +6 -2
-	xwrite(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz);
-	xwrite(cmd->in, "\n", 1);
+	err = write_in_full(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz);
+	if (err <= 0)
+		return err;
+	err = write_in_full(cmd->in, "\n", 1);
+	if (err <= 0)
+		return err;
 	return 0;
 }

I think this has already been brought up, but the caller of this helper does not make such an error stand out enough and instead makes the resulting repack silently produce wrong result, which is not an improvement. Perhaps

	if (write_in_full(...) ||
	    write_in_full(...))
		die(_("failed to list promisor objects to repack"));

or something?

Thanks.

#3 lore raw

PATCH v2 2/3 builtin/receive-pack.c: change xwrite to write_in_full.

In reply to Randall S. Becker

From: "Randall S. Becker" <[email protected]>

This change is required because some platforms do not support file writes of arbitrary sizes (e.g, NonStop). xwrite ends up truncating the output to the
maximum single I/O size possible for the destination device.

---
 builtin/receive-pack.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
builtin/receive-pack.c +1 -1
index db65607485..4277c63d08 100644
@@ -456,7 +456,7 @@ static void report_message(const char *prefix, const char *err, va_list params)
 	if (use_sideband)
 		send_sideband(1, 2, msg, sz, use_sideband);
 	else
-		xwrite(2, msg, sz);
+		write_in_full(2, msg, sz);
 }
 
 __attribute__((format (printf, 1, 2)))
Signature · 1 line
2.42.1
#4 lore raw

Re: [PATCH v2 2/3] builtin/receive-pack.c: change xwrite to write_in_full.

Quoted 15 lines · Randall S. Becker

From: "Randall S. Becker" <[email protected]>

This change is required because some platforms do not support file writes of arbitrary sizes (e.g, NonStop). xwrite ends up truncating the output to the
maximum single I/O size possible for the destination device.

---
 builtin/receive-pack.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
builtin/receive-pack.c +0 -0
index db65607485..4277c63d08 100644

builtin/receive-pack.c static void report_message(const char *prefix, const char *err, va_list params) · line 460

Quoted 6 lines
builtin/receive-pack.c +1 -1
@@ -456,7 +456,7 @@ static void report_message(const char *prefix, const char *err, va_list params)
 	if (use_sideband)
 		send_sideband(1, 2, msg, sz, use_sideband);
 	else
-		xwrite(2, msg, sz);
+		write_in_full(2, msg, sz);
 }

This change does make sense, as we can see a short write(2) from xwrite() and this caller is not repeating the call to flush the remainder after a short write.

Quoted 2 lines
builtin/receive-pack.c +0 -0
 __attribute__((format (printf, 1, 2)))
#5 lore raw

PATCH v2 3/3 builtin/unpack-objects.c: change xwrite to write_in_full avoid truncation.

In reply to Randall S. Becker

From: "Randall S. Becker" <[email protected]>

This change is required because some platforms do not support file writes of arbitrary sizes (e.g, NonStop). xwrite ends up truncating the output to the maximum single I/O size possible for the destination device if the supplied len value exceeds the supported value. Replacing xwrite with write_in_full corrects this problem. Future optimisations could remove the loop in favour of just calling write_in_full.

---
 builtin/unpack-objects.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
builtin/unpack-objects.c +1 -1
index e0a701f2b3..6935c4574e 100644
@@ -680,7 +680,7 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix UNUSED)
 
 	/* Write the last part of the buffer to stdout */
 	while (len) {
-		int ret = xwrite(1, buffer + offset, len);
+		int ret = write_in_full(1, buffer + offset, len);
 		if (ret <= 0)
 			break;
 		len -= ret;
Signature · 1 line
2.42.1
#6 lore raw

Re: [PATCH v2 3/3] builtin/unpack-objects.c: change xwrite to write_in_full avoid truncation.

Quoted 19 lines · Randall S. Becker

From: "Randall S. Becker" <[email protected]>

This change is required because some platforms do not support file writes of arbitrary sizes (e.g, NonStop). xwrite ends up truncating the output to the maximum single I/O size possible for the destination device if the supplied len value exceeds the supported value. Replacing xwrite with write_in_full corrects this problem. Future optimisations could remove the loop in favour of just calling write_in_full.

---
 builtin/unpack-objects.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
builtin/unpack-objects.c +0 -0
index e0a701f2b3..6935c4574e 100644

builtin/unpack-objects.c int cmd_unpack_objects(int argc, const char **argv, const char *prefix UNUSED) · line 686

Quoted 8 lines
builtin/unpack-objects.c +1 -1
@@ -680,7 +680,7 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix UNUSED)
 
 	/* Write the last part of the buffer to stdout */
 	while (len) {
-		int ret = xwrite(1, buffer + offset, len);
+		int ret = write_in_full(1, buffer + offset, len);
 		if (ret <= 0)
 			break;
 		len -= ret;

Why do we need this with a retry loop that is prepared for short write(2) specifically like this?

If xwrite() calls underlying write(2) with too large a value, then your MAX_IO_SIZE is misconfigured, and the fix should go there, not here in a loop that expects a working xwrite() that is allowed to return on short write(2), I would think.

#7 lore raw

RE: [PATCH v2 3/3] builtin/unpack-objects.c: change xwrite to write_in_full avoid truncation.

Quoted 10 lines · Junio C Hamano
Quoted 8 lines · Randall S. Becker

From: "Randall S. Becker" <[email protected]>

This change is required because some platforms do not support file writes of arbitrary sizes (e.g, NonStop). xwrite ends up truncating the output to the maximum single I/O size possible for the destination device if the supplied len value exceeds the supported value. Replacing xwrite with write_in_full corrects this problem. Future optimisations could remove the loop in favour of just calling

write_in_full.

Quoted 24 lines
Quoted 19 lines
---
 builtin/unpack-objects.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
builtin/unpack-objects.c index renamed from builtin/unpack-objects.c +0 -0

e0a701f2b3..6935c4574e 100644

builtin/unpack-objects.c +1 -1
@@ -680,7 +680,7 @@ int cmd_unpack_objects(int argc, const char
 **argv, const char *prefix UNUSED)
 
 	/* Write the last part of the buffer to stdout */
 	while (len) {
-		int ret = xwrite(1, buffer + offset, len);
+		int ret = write_in_full(1, buffer + offset, len);
 		if (ret <= 0)
 			break;
Quoted 1 line

len -= ret;

Why do we need this with a retry loop that is prepared for short write(2) specifically like this?

Quoted 1 line

If xwrite() calls underlying write(2) with too large a value, then your

MAX_IO_SIZE is misconfigured, and the fix should go there, not

Quoted 1 line

here in a loop that expects a working xwrite() that is allowed to return on

short write(2), I would think.

I experimented with using write_in_full vs. keeping xwrite. With xwrite in this loop, t7704.9 consistently fails as described in the other thread. With write_in_full, the code works correctly. I assume there are side-effects that are present. This change is critical to having the code work on NonStop. Otherwise git seems to be at risk of actually being seriously broken if unpack does not work correctly. I am happy to have my series ignored as long as the problem is otherwise corrected.

#8 lore raw

Re: [PATCH v2 3/3] builtin/unpack-objects.c: change xwrite to write_in_full avoid truncation.

Quoted 7 lines · On Tue, Feb 27, 2024 at 02:04:46PM -0500, [email protected] wrote:
Quoted 7 lines
Quoted 7 lines
builtin/unpack-objects.c index renamed from builtin/unpack-objects.c +0 -0

e0a701f2b3..6935c4574e 100644

builtin/unpack-objects.c +0 -0
@@ -680,7 +680,7 @@ int cmd_unpack_objects(int argc, const char
 **argv, const char *prefix UNUSED)
 
Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
builtin/unpack-objects.c +1 -1
 	/* Write the last part of the buffer to stdout */
 	while (len) {
-		int ret = xwrite(1, buffer + offset, len);
+		int ret = write_in_full(1, buffer + offset, len);
 		if (ret <= 0)
 			break;

len -= ret;

[...]
I experimented with using write_in_full vs. keeping xwrite. With xwrite in this loop, t7704.9 consistently fails as described in the other thread. With write_in_full, the code works correctly. I assume there are side-effects that are present. This change is critical to having the code work on NonStop. Otherwise git seems to be at risk of actually being seriously broken if unpack does not work correctly. I am happy to have my series ignored as long as the problem is otherwise corrected.

I'm somewhat skeptical that this code is to blame, as it should be run very rarely at all; it is just dumping any content in the pack stream after the end of the checksum to stdout. But in normal use by Git, there is no such content in the first place.

If I do this:

builtin/unpack-objects.c +1 -5
index e0a701f2b3..affe55035d 100644
@@ -680,11 +680,7 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix UNUSED)
 
 	/* Write the last part of the buffer to stdout */
 	while (len) {
-		int ret = xwrite(1, buffer + offset, len);
-		if (ret <= 0)
-			break;
-		len -= ret;
-		offset += ret;
+		BUG("cruft at the end of the pack!");
 	}
 
 	/* All done */

then t7704 still passes, as it does not run this code at all. In fact, nothing in the test suite fails. Which is not to say we should get rid of those code. If we were writing today we might flag it as an error, but we should keep it for historical compatibility.

But I do not see any bug in the code, and nor do I think it could contribute to a test failure.

-Peff

#9 lore raw

RE: [PATCH v2 3/3] builtin/unpack-objects.c: change xwrite to write_in_full avoid truncation.

Quoted 51 lines · Peff

To: [email protected]
Cc: 'Junio C Hamano' <[email protected]>; 'Randall S. Becker' <[email protected]>; [email protected]
Subject: Re: [PATCH v2 3/3] builtin/unpack-objects.c: change xwrite to write_in_full avoid truncation.

Quoted 7 lines · On Tue, Feb 27, 2024 at 02:04:46PM -0500, [email protected] wrote:
Quoted 7 lines
Quoted 7 lines
builtin/unpack-objects.c +0 -0
index e0a701f2b3..6935c4574e 100644
@@ -680,7 +680,7 @@ int cmd_unpack_objects(int argc, const char
 **argv, const char *prefix UNUSED)
 
Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
builtin/unpack-objects.c +1 -1
 	/* Write the last part of the buffer to stdout */
 	while (len) {
-		int ret = xwrite(1, buffer + offset, len);
+		int ret = write_in_full(1, buffer + offset, len);
 		if (ret <= 0)
 			break;

len -= ret;

[...]
I experimented with using write_in_full vs. keeping xwrite. With xwrite in this loop, t7704.9 consistently fails as described in the other thread. With write_in_full, the code works correctly. I assume there are side-effects that are present. This change is critical to having the code work on NonStop. Otherwise git seems to be at risk of actually being seriously broken if unpack does not work correctly. I am happy to have my series ignored as long as the problem is otherwise corrected.

I'm somewhat skeptical that this code is to blame, as it should be run very rarely at all; it is just dumping any content in the pack stream after the end of the checksum to stdout. But in normal use by Git, there is no such content in the first place.

If I do this:

builtin/unpack-objects.c +1 -5
@@ -680,11 +680,7 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix UNUSED)
 
 	/* Write the last part of the buffer to stdout */
 	while (len) {
-		int ret = xwrite(1, buffer + offset, len);
-		if (ret <= 0)
-			break;
-		len -= ret;
-		offset += ret;
+		BUG("cruft at the end of the pack!");
 	}
 
 	/* All done */
Quoted 4 lines

then t7704 still passes, as it does not run this code at all. In fact, nothing in the test suite fails. Which is not to say we should get rid of those code. If we were writing today we might flag it as an error, but we should keep it for historical compatibility.

But I do not see any bug in the code, and nor do I think it could contribute to a test failure.

I have obviously gone down the wrong path trying to resolve this situation. Please consider this entire series dropped with my apologies for the time-waste.

Unfortunately, I do not have sufficient knowledge of the code to resolve the originally reported problem without further assistance to determine the root case (assuming it still is a problem). Changes in master post-2.44.0 appear to have contributed to resolving the situation, so I am now getting random pass/fail on the test. I'm going to hold 2.44.0 on ia64 and wait for a subsequent release at retest at that time.

Sadly,
--Randall

#10 lore raw

Re: [PATCH v2 3/3] builtin/unpack-objects.c: change xwrite to write_in_full avoid truncation.

Quoted 7 lines · On Tue, Feb 27, 2024 at 04:05:53PM -0500, [email protected] wrote:

Unfortunately, I do not have sufficient knowledge of the code to resolve the originally reported problem without further assistance to determine the root case (assuming it still is a problem). Changes in master post-2.44.0 appear to have contributed to resolving the situation, so I am now getting random pass/fail on the test. I'm going to hold 2.44.0 on ia64 and wait for a subsequent release at retest at that time.

If you're getting random pass/fail (which does seem like the kind of thing that could be related to pipe write() sizes), you might try using the "--stress" argument. That can give you more consistent results while bisecting (e.g., if "--stress" runs successfully for a few minutes).

That said, given the failing test you mentioned, I kind of assume that it was not a code change that caused the problem, but rather a new test exercising new code that happens to tickle your race.

-Peff

#11 lore raw
In reply to Randall S. Becker

Please withdraw this series.

Quoted 10 lines

-----Original Message-----
From: Randall S. Becker <[email protected]>
Sent: Tuesday, February 27, 2024 10:10 AM
To: [email protected]
Cc: Randall S. Becker <[email protected]>
Subject: [PATCH v2 0/2] Change xwrite() to write_in_full() in builtins.

From: "Randall S. Becker" <[email protected]>

This series replaces xwrite to write_in_full in builtins/. The change is

required to fix critical problems that prevent full writes to be

Quoted 1 line

processed by on platforms where xwrite may be limited to a platform size

limit. Further changes outside of builtins/ may be required

Quoted 1 line

but do not appear to be as urgent as this change, which causes test

breakage in t7704. A separate series will be contributed for

Quoted 3 lines

changes outside of builtins/ at a later date.

The change in unpack-objects.c is necessary as len is being passed into

xwrite that exceeds the size supported by the limit in that

Quoted 15 lines

method (56Kb on NonStop ia64).

Randall S. Becker (3):
builtin/repack.c: change xwrite to write_in_full and report errors. builtin/receive-pack.c: change xwrite to write_in_full. builtin/unpack-objects.c: change xwrite to write_in_full avoid
truncation.

builtin/receive-pack.c   | 2 +-
builtin/repack.c         | 9 +++++++--
builtin/unpack-objects.c | 2 +-
3 files changed, 9 insertions(+), 4 deletions(-)

--
2.42.1