Skip to content

Commit 756e676

Browse files
peffgitster
authored andcommitted
write_or_die: raise SIGPIPE when we get EPIPE
The write_or_die function will always die on an error, including EPIPE. However, it currently treats EPIPE specially by suppressing any error message, and by exiting with exit code 0. Suppressing the error message makes some sense; a pipe death may just be a sign that the other side is not interested in what we have to say. However, exiting with a successful error code is not a good idea, as write_or_die is frequently used in cases where we want to be careful about having written all of the output, and we may need to signal to our caller that we have done so (e.g., you would not want a push whose other end has hung up to report success). This distinction doesn't typically matter in git, because we do not ignore SIGPIPE in the first place. Which means that we will not get EPIPE, but instead will just die when we get a SIGPIPE. But it's possible for a default handler to be set by a parent process, or for us to add a callsite inside one of our few SIGPIPE-ignoring blocks of code. This patch converts write_or_die to actually raise SIGPIPE when we see EPIPE, rather than exiting with zero. This brings the behavior in line with the "normal" case that we die from SIGPIPE (and any callers who want to check why we died will see the same thing). We also give the same treatment to other related functions, including write_or_whine_pipe and maybe_flush_or_die. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 090fd4f commit 756e676

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

write_or_die.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
#include "cache.h"
22

3+
static void check_pipe(int err)
4+
{
5+
if (err == EPIPE) {
6+
signal(SIGPIPE, SIG_DFL);
7+
raise(SIGPIPE);
8+
/* Should never happen, but just in case... */
9+
exit(141);
10+
}
11+
}
12+
313
/*
414
* Some cases use stdio, but want to flush after the write
515
* to get error handling (and to get better interactive
@@ -34,8 +44,7 @@ void maybe_flush_or_die(FILE *f, const char *desc)
3444
return;
3545
}
3646
if (fflush(f)) {
37-
if (errno == EPIPE)
38-
exit(0);
47+
check_pipe(errno);
3948
die_errno("write failure on '%s'", desc);
4049
}
4150
}
@@ -50,17 +59,15 @@ void fsync_or_die(int fd, const char *msg)
5059
void write_or_die(int fd, const void *buf, size_t count)
5160
{
5261
if (write_in_full(fd, buf, count) < 0) {
53-
if (errno == EPIPE)
54-
exit(0);
62+
check_pipe(errno);
5563
die_errno("write error");
5664
}
5765
}
5866

5967
int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg)
6068
{
6169
if (write_in_full(fd, buf, count) < 0) {
62-
if (errno == EPIPE)
63-
exit(0);
70+
check_pipe(errno);
6471
fprintf(stderr, "%s: write error (%s)\n",
6572
msg, strerror(errno));
6673
return 0;

0 commit comments

Comments
 (0)