write_or_die: handle EPIPE in async threads
authorJeff King <peff@peff.net>
Wed, 24 Feb 2016 07:40:16 +0000 (02:40 -0500)
committerJunio C Hamano <gitster@pobox.com>
Thu, 25 Feb 2016 21:51:45 +0000 (13:51 -0800)
When write_or_die() sees EPIPE, it treats it specially by
converting it into a SIGPIPE death. We obviously cannot
ignore it, as the write has failed and the caller expects us
to die. But likewise, we cannot just call die(), because
printing any message at all would be a nuisance during
normal operations.

However, this is a problem if write_or_die() is called from
a thread. Our raised signal ends up killing the whole
process, when logically we just need to kill the thread
(after all, if we are ignoring SIGPIPE, there is good reason
to think that the main thread is expecting to handle it).

Inside an async thread, the die() code already does the
right thing, because we use our custom die_async() routine,
which calls pthread_join(). So ideally we would piggy-back
on that, and simply call:

die_quietly_with_code(141);

or similar. But refactoring the die code to do this is
surprisingly non-trivial. The die_routines themselves handle
both printing and the decision of the exit code. Every one
of them would have to be modified to take new parameters for
the code, and to tell us to be quiet.

Instead, we can just teach write_or_die() to check for the
async case and handle it specially. We do have to build an
interface to abstract the async exit, but it's simple and
self-contained. If we had many call-sites that wanted to do
this die_quietly_with_code(), this approach wouldn't scale
as well, but we don't. This is the only place where do this
weird exit trick.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
run-command.c
run-command.h
write_or_die.c
index 13fa452e8c3d5a5e20e24a969d53ce7ade4019bf..3add1d66ac344feab2ca39ee6fff663628c5fe32 100644 (file)
@@ -633,6 +633,11 @@ int in_async(void)
        return !pthread_equal(main_thread, pthread_self());
 }
 
+void NORETURN async_exit(int code)
+{
+       pthread_exit((void *)(intptr_t)code);
+}
+
 #else
 
 static struct {
@@ -678,6 +683,11 @@ int in_async(void)
        return process_is_async;
 }
 
+void NORETURN async_exit(int code)
+{
+       exit(code);
+}
+
 #endif
 
 int start_async(struct async *async)
index 12bb26c2a6155750203babfec47b08e8bde0ad27..c0969c7695f6b550b382b5704ceb9b2c23b3e3e0 100644 (file)
@@ -121,5 +121,6 @@ struct async {
 int start_async(struct async *async);
 int finish_async(struct async *async);
 int in_async(void);
+void NORETURN async_exit(int code);
 
 #endif
index e7afe7a295e586d58313e82e87b22de347aef7a1..49e80aa222132c3c151b79bd37749d92a12f745a 100644 (file)
@@ -1,8 +1,12 @@
 #include "cache.h"
+#include "run-command.h"
 
 static void check_pipe(int err)
 {
        if (err == EPIPE) {
+               if (in_async())
+                       async_exit(141);
+
                signal(SIGPIPE, SIG_DFL);
                raise(SIGPIPE);
                /* Should never happen, but just in case... */