From 1fd0e322f89abeede66acaca138db5ac92daee12 Mon Sep 17 00:00:00 2001 From: Derrick Stolee Date: Tue, 27 Aug 2019 15:30:37 -0400 Subject: [PATCH] upload-pack: fix race condition in error messages Test t5516-fetch-push.sh has a test 'deny fetch unreachable SHA1, allowtipsha1inwant=true' that checks stderr for a specific error string from the remote. In some build environments the error sent over the remote connection gets mingled with the error from the die() statement. Since both signals are being output to the same file descriptor (but from parent and child processes), the output we are matching with grep gets split. To remove this failure, follow this process instead: 1. Write an error message to stderr. 2. Write an error message across the connection. 3. Flush the connection. 4. exit(1). This reorders the events so the error is written entirely before the client receives a message from the remote, removing the race condition. The flush is important for avoiding a client error when the connection is cut in the middle of a packet line. Signed-off-by: Derrick Stolee --- upload-pack.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/upload-pack.c b/upload-pack.c index 222cd3ad8960f3..ccbf86f2a193d7 100644 --- a/upload-pack.c +++ b/upload-pack.c @@ -613,11 +613,13 @@ static void check_non_tip(struct object_array *want_obj, for (i = 0; i < want_obj->nr; i++) { struct object *o = want_obj->objects[i].item; if (!is_our_ref(o)) { + error("git upload-pack: not our ref %s", + oid_to_hex(&o->oid)); packet_writer_error(writer, "upload-pack: not our ref %s", oid_to_hex(&o->oid)); - die("git upload-pack: not our ref %s", - oid_to_hex(&o->oid)); + packet_writer_flush(writer); + exit(1); } } }