From: Elias Naur Date: Wed, 31 Jul 2019 12:33:57 +0000 (+0200) Subject: runtime: don't forward SIGPIPE on macOS X-Git-Tag: go1.14beta1~1280 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=d56a86e01f2e771a4706f0a5cfbe2d87cd888f05;p=gostls13.git runtime: don't forward SIGPIPE on macOS macOS and iOS deliver SIGPIPE signals to the main thread and not the thread that raised it by writing to a closed socket or pipe. SIGPIPE signals can be suppressed for sockets with the SO_NOSIGPIPE option, but there is no similar option for pipes. We have no other choice but to never forward SIGPIPE on macOS. Fixes #33384 Change-Id: Ice3de75b121f00006ee11c26d560e619536460be Reviewed-on: https://go-review.googlesource.com/c/go/+/188297 Run-TryBot: Elias Naur TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- diff --git a/misc/cgo/testcarchive/testdata/main3.c b/misc/cgo/testcarchive/testdata/main3.c index 60a16cf5fc..4d11d9ce4c 100644 --- a/misc/cgo/testcarchive/testdata/main3.c +++ b/misc/cgo/testcarchive/testdata/main3.c @@ -12,6 +12,7 @@ #include #include #include +#include #include "libgo3.h" @@ -51,11 +52,18 @@ static void init() { } } +static void *provokeSIGPIPE(void *arg) { + ProvokeSIGPIPE(); + return NULL; +} + int main(int argc, char** argv) { int verbose; struct sigaction sa; int i; struct timespec ts; + int res; + pthread_t tid; verbose = argc > 2; setvbuf(stdout, NULL, _IONBF, 0); @@ -68,6 +76,19 @@ int main(int argc, char** argv) { // a non-default SIGPIPE handler before the runtime initializes. ProvokeSIGPIPE(); + // Test that SIGPIPE on a non-main thread is also handled by Go. + res = pthread_create(&tid, NULL, provokeSIGPIPE, NULL); + if (res != 0) { + fprintf(stderr, "pthread_create: %s\n", strerror(res)); + exit(EXIT_FAILURE); + } + + res = pthread_join(tid, NULL); + if (res != 0) { + fprintf(stderr, "pthread_join: %s\n", strerror(res)); + exit(EXIT_FAILURE); + } + if (verbose) { printf("calling sigaction\n"); } diff --git a/src/runtime/signal_unix.go b/src/runtime/signal_unix.go index ad51dc1800..436c18c126 100644 --- a/src/runtime/signal_unix.go +++ b/src/runtime/signal_unix.go @@ -636,6 +636,13 @@ func sigfwdgo(sig uint32, info *siginfo, ctx unsafe.Pointer) bool { return true } + // This function and its caller sigtrampgo assumes SIGPIPE is delivered on the + // originating thread. This property does not hold on macOS (golang.org/issue/33384), + // so we have no choice but to ignore SIGPIPE. + if GOOS == "darwin" && sig == _SIGPIPE { + return true + } + // If there is no handler to forward to, no need to forward. if fwdFn == _SIG_DFL { return false