Skip to content

Commit f65003b

Browse files
jiangxingitster
authored andcommitted
receive-pack: gently write messages to proc-receive
Johannes found a flaky hang in `t5411/test-0013-bad-protocol.sh` in the osx-clang job of the CI/PR builds, and ran into an issue when using the `--stress` option with the following error messages: fatal: unable to write flush packet: Broken pipe send-pack: unexpected disconnect while reading sideband packet fatal: the remote end hung up unexpectedly In this test case, the "proc-receive" hook sends an error message and dies earlier. While "receive-pack" on the other side of the pipe should forward the error message of the "proc-receive" hook to the client side, but it fails to do so. This is because "receive-pack" uses `packet_write_fmt()` and `packet_flush()` to write pkt-line message to "proc-receive" hook, and these functions die immediately when pipe is broken. Using "gently" forms for these functions will get more predicable output. Add more "--die-*" options to test helper to test different stages of the protocol between "receive-pack" and "proc-receive" hook. Reported-by: Johannes Schindelin <[email protected]> Suggested-by: Jeff King <[email protected]> Signed-off-by: Jiang Xin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cf3d868 commit f65003b

File tree

4 files changed

+392
-48
lines changed

4 files changed

+392
-48
lines changed

builtin/receive-pack.c

Lines changed: 63 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -977,15 +977,25 @@ static int read_proc_receive_report(struct packet_reader *reader,
977977
int new_report = 0;
978978
int code = 0;
979979
int once = 0;
980+
int response = 0;
980981

981982
for (;;) {
982983
struct object_id old_oid, new_oid;
983984
const char *head;
984985
const char *refname;
985986
char *p;
986-
987-
if (packet_reader_read(reader) != PACKET_READ_NORMAL)
987+
enum packet_read_status status;
988+
989+
status = packet_reader_read(reader);
990+
if (status != PACKET_READ_NORMAL) {
991+
/* Check whether proc-receive exited abnormally */
992+
if (status == PACKET_READ_EOF && !response) {
993+
strbuf_addstr(errmsg, "proc-receive exited abnormally");
994+
return -1;
995+
}
988996
break;
997+
}
998+
response++;
989999

9901000
head = reader->line;
9911001
p = strchr(head, ' ');
@@ -1145,28 +1155,41 @@ static int run_proc_receive_hook(struct command *commands,
11451155
if (use_push_options)
11461156
strbuf_addstr(&cap, " push-options");
11471157
if (cap.len) {
1148-
packet_write_fmt(proc.in, "version=1%c%s\n", '\0', cap.buf + 1);
1158+
code = packet_write_fmt_gently(proc.in, "version=1%c%s\n", '\0', cap.buf + 1);
11491159
strbuf_release(&cap);
11501160
} else {
1151-
packet_write_fmt(proc.in, "version=1\n");
1161+
code = packet_write_fmt_gently(proc.in, "version=1\n");
11521162
}
1153-
packet_flush(proc.in);
1163+
if (!code)
1164+
code = packet_flush_gently(proc.in);
11541165

1155-
for (;;) {
1156-
int linelen;
1166+
if (!code)
1167+
for (;;) {
1168+
int linelen;
1169+
enum packet_read_status status;
11571170

1158-
if (packet_reader_read(&reader) != PACKET_READ_NORMAL)
1159-
break;
1171+
status = packet_reader_read(&reader);
1172+
if (status != PACKET_READ_NORMAL) {
1173+
/* Check whether proc-receive exited abnormally */
1174+
if (status == PACKET_READ_EOF)
1175+
code = -1;
1176+
break;
1177+
}
11601178

1161-
if (reader.pktlen > 8 && starts_with(reader.line, "version=")) {
1162-
version = atoi(reader.line + 8);
1163-
linelen = strlen(reader.line);
1164-
if (linelen < reader.pktlen) {
1165-
const char *feature_list = reader.line + linelen + 1;
1166-
if (parse_feature_request(feature_list, "push-options"))
1167-
hook_use_push_options = 1;
1179+
if (reader.pktlen > 8 && starts_with(reader.line, "version=")) {
1180+
version = atoi(reader.line + 8);
1181+
linelen = strlen(reader.line);
1182+
if (linelen < reader.pktlen) {
1183+
const char *feature_list = reader.line + linelen + 1;
1184+
if (parse_feature_request(feature_list, "push-options"))
1185+
hook_use_push_options = 1;
1186+
}
11681187
}
11691188
}
1189+
1190+
if (code) {
1191+
strbuf_addstr(&errmsg, "fail to negotiate version with proc-receive hook");
1192+
goto cleanup;
11701193
}
11711194

11721195
if (version != 1) {
@@ -1180,20 +1203,36 @@ static int run_proc_receive_hook(struct command *commands,
11801203
for (cmd = commands; cmd; cmd = cmd->next) {
11811204
if (!cmd->run_proc_receive || cmd->skip_update || cmd->error_string)
11821205
continue;
1183-
packet_write_fmt(proc.in, "%s %s %s",
1184-
oid_to_hex(&cmd->old_oid),
1185-
oid_to_hex(&cmd->new_oid),
1186-
cmd->ref_name);
1206+
code = packet_write_fmt_gently(proc.in, "%s %s %s",
1207+
oid_to_hex(&cmd->old_oid),
1208+
oid_to_hex(&cmd->new_oid),
1209+
cmd->ref_name);
1210+
if (code)
1211+
break;
1212+
}
1213+
if (!code)
1214+
code = packet_flush_gently(proc.in);
1215+
if (code) {
1216+
strbuf_addstr(&errmsg, "fail to write commands to proc-receive hook");
1217+
goto cleanup;
11871218
}
1188-
packet_flush(proc.in);
11891219

11901220
/* Send push options */
11911221
if (hook_use_push_options) {
11921222
struct string_list_item *item;
11931223

1194-
for_each_string_list_item(item, push_options)
1195-
packet_write_fmt(proc.in, "%s", item->string);
1196-
packet_flush(proc.in);
1224+
for_each_string_list_item(item, push_options) {
1225+
code = packet_write_fmt_gently(proc.in, "%s", item->string);
1226+
if (code)
1227+
break;
1228+
}
1229+
if (!code)
1230+
code = packet_flush_gently(proc.in);
1231+
if (code) {
1232+
strbuf_addstr(&errmsg,
1233+
"fail to write push-options to proc-receive hook");
1234+
goto cleanup;
1235+
}
11971236
}
11981237

11991238
/* Read result from proc-receive */

t/helper/test-proc-receive.c

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ static const char *proc_receive_usage[] = {
1010
NULL
1111
};
1212

13-
static int die_version;
14-
static int die_readline;
13+
static int die_read_version;
14+
static int die_write_version;
15+
static int die_read_commands;
16+
static int die_read_push_options;
17+
static int die_write_report;
1518
static int no_push_options;
1619
static int use_atomic;
1720
static int use_push_options;
@@ -33,6 +36,9 @@ struct command {
3336
static void proc_receive_verison(struct packet_reader *reader) {
3437
int server_version = 0;
3538

39+
if (die_read_version)
40+
die("die with the --die-read-version option");
41+
3642
for (;;) {
3743
int linelen;
3844

@@ -52,9 +58,12 @@ static void proc_receive_verison(struct packet_reader *reader) {
5258
}
5359
}
5460

55-
if (server_version != 1 || die_version)
61+
if (server_version != 1)
5662
die("bad protocol version: %d", server_version);
5763

64+
if (die_write_version)
65+
die("die with the --die-write-version option");
66+
5867
packet_write_fmt(1, "version=%d%c%s\n",
5968
version, '\0',
6069
use_push_options && !no_push_options ? "push-options": "");
@@ -75,11 +84,13 @@ static void proc_receive_read_commands(struct packet_reader *reader,
7584
if (packet_reader_read(reader) != PACKET_READ_NORMAL)
7685
break;
7786

87+
if (die_read_commands)
88+
die("die with the --die-read-commands option");
89+
7890
if (parse_oid_hex(reader->line, &old_oid, &p) ||
7991
*p++ != ' ' ||
8092
parse_oid_hex(p, &new_oid, &p) ||
81-
*p++ != ' ' ||
82-
die_readline)
93+
*p++ != ' ')
8394
die("protocol error: expected 'old new ref', got '%s'",
8495
reader->line);
8596
refname = p;
@@ -99,6 +110,9 @@ static void proc_receive_read_push_options(struct packet_reader *reader,
99110
if (no_push_options || !use_push_options)
100111
return;
101112

113+
if (die_read_push_options)
114+
die("die with the --die-read-push-options option");
115+
102116
while (1) {
103117
if (packet_reader_read(reader) != PACKET_READ_NORMAL)
104118
break;
@@ -117,10 +131,16 @@ int cmd__proc_receive(int argc, const char **argv)
117131
struct option options[] = {
118132
OPT_BOOL(0, "no-push-options", &no_push_options,
119133
"disable push options"),
120-
OPT_BOOL(0, "die-version", &die_version,
121-
"die during version negotiation"),
122-
OPT_BOOL(0, "die-readline", &die_readline,
123-
"die when readline"),
134+
OPT_BOOL(0, "die-read-version", &die_read_version,
135+
"die when reading version"),
136+
OPT_BOOL(0, "die-write-version", &die_write_version,
137+
"die when writing version"),
138+
OPT_BOOL(0, "die-read-commands", &die_read_commands,
139+
"die when reading commands"),
140+
OPT_BOOL(0, "die-read-push-options", &die_read_push_options,
141+
"die when reading push-options"),
142+
OPT_BOOL(0, "die-write-report", &die_write_report,
143+
"die when writing report"),
124144
OPT_STRING_LIST('r', "return", &returns, "old/new/ref/status/msg",
125145
"return of results"),
126146
OPT__VERBOSE(&verbose, "be verbose"),
@@ -136,7 +156,7 @@ int cmd__proc_receive(int argc, const char **argv)
136156
usage_msg_opt("Too many arguments.", proc_receive_usage, options);
137157
packet_reader_init(&reader, 0, NULL, 0,
138158
PACKET_READ_CHOMP_NEWLINE |
139-
PACKET_READ_DIE_ON_ERR_PACKET);
159+
PACKET_READ_GENTLE_ON_EOF);
140160

141161
sigchain_push(SIGPIPE, SIG_IGN);
142162
proc_receive_verison(&reader);
@@ -166,6 +186,8 @@ int cmd__proc_receive(int argc, const char **argv)
166186
fprintf(stderr, "proc-receive> %s\n", item->string);
167187
}
168188

189+
if (die_write_report)
190+
die("die with the --die-write-report option");
169191
if (returns.nr)
170192
for_each_string_list_item(item, &returns)
171193
packet_write_fmt(1, "%s\n", item->string);

0 commit comments

Comments
 (0)