aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShenghou Ma <minux@golang.org>2014-12-27 19:15:38 -0500
committerRuss Cox <rsc@golang.org>2015-01-14 05:25:22 +0000
commitcc7bbb0ae9cb34170fa9b53ca7accbe1b80fece4 (patch)
tree524101c8d3d185217c2c71388105978db0af6bf4
parent4482c7b1a1ad3a7dd31f6b6cd26fcfd2758743d9 (diff)
downloadgo-cc7bbb0ae9cb34170fa9b53ca7accbe1b80fece4.tar.gz
go-cc7bbb0ae9cb34170fa9b53ca7accbe1b80fece4.zip
[release-branch.go1.4] runtime: ignore SIGPROF to foreign threads before cgocallback is fully initialized
Some libraries, for example, OpenBLAS, create work threads in a global constructor. If we're doing cpu profiling, it's possible that SIGPROF might come to some of the worker threads before we make our first cgo call. Cgocallback used to terminate the process when that happens, but it's better to miss a couple profiling signals than to abort in this case. Fixes #9456. Change-Id: I112b8e1a6e10e6cc8ac695a4b518c0f577309b6b Reviewed-on: https://go-review.googlesource.com/2141 Reviewed-by: Ian Lance Taylor <iant@golang.org> (cherry picked from commit 5da9c8cd0a0427d1771b3a9a6d8d931430ce50dd) Reviewed-on: https://go-review.googlesource.com/2789 Reviewed-by: Andrew Gerrand <adg@golang.org>
-rw-r--r--src/runtime/crash_cgo_test.go55
-rw-r--r--src/runtime/sigqueue.go9
2 files changed, 64 insertions, 0 deletions
diff --git a/src/runtime/crash_cgo_test.go b/src/runtime/crash_cgo_test.go
index 972eedc624..98c4c1c0d4 100644
--- a/src/runtime/crash_cgo_test.go
+++ b/src/runtime/crash_cgo_test.go
@@ -50,6 +50,18 @@ func TestCgoExternalThreadPanic(t *testing.T) {
}
}
+func TestCgoExternalThreadSIGPROF(t *testing.T) {
+ // issue 9456.
+ if runtime.GOOS == "plan9" || runtime.GOOS == "windows" {
+ t.Skipf("no pthreads on %s", runtime.GOOS)
+ }
+ got := executeTest(t, cgoExternalThreadSIGPROFSource, nil)
+ want := "OK\n"
+ if got != want {
+ t.Fatalf("expected %q, but got %q", want, got)
+ }
+}
+
const cgoSignalDeadlockSource = `
package main
@@ -194,3 +206,46 @@ start(void)
printf("_beginthreadex failed\n");
}
`
+
+const cgoExternalThreadSIGPROFSource = `
+package main
+
+/*
+#include <stdint.h>
+#include <signal.h>
+#include <pthread.h>
+
+volatile int32_t spinlock;
+
+static void *thread1(void *p) {
+ (void)p;
+ while (spinlock == 0)
+ ;
+ pthread_kill(pthread_self(), SIGPROF);
+ spinlock = 0;
+ return NULL;
+}
+__attribute__((constructor)) void issue9456() {
+ pthread_t tid;
+ pthread_create(&tid, 0, thread1, NULL);
+}
+*/
+import "C"
+
+import (
+ "runtime"
+ "sync/atomic"
+ "unsafe"
+)
+
+func main() {
+ // This test intends to test that sending SIGPROF to foreign threads
+ // before we make any cgo call will not abort the whole process, so
+ // we cannot make any cgo call here. See http://golang.org/issue/9456.
+ atomic.StoreInt32((*int32)(unsafe.Pointer(&C.spinlock)), 1)
+ for atomic.LoadInt32((*int32)(unsafe.Pointer(&C.spinlock))) == 1 {
+ runtime.Gosched()
+ }
+ println("OK")
+}
+`
diff --git a/src/runtime/sigqueue.go b/src/runtime/sigqueue.go
index 2d9c24d2d2..d2fc5729da 100644
--- a/src/runtime/sigqueue.go
+++ b/src/runtime/sigqueue.go
@@ -154,6 +154,15 @@ func signal_disable(s uint32) {
// This runs on a foreign stack, without an m or a g. No stack split.
//go:nosplit
func badsignal(sig uintptr) {
+ // Some external libraries, for example, OpenBLAS, create worker threads in
+ // a global constructor. If we're doing cpu profiling, and the SIGPROF signal
+ // comes to one of the foreign threads before we make our first cgo call, the
+ // call to cgocallback below will bring down the whole process.
+ // It's better to miss a few SIGPROF signals than to abort in this case.
+ // See http://golang.org/issue/9456.
+ if sig == _SIGPROF && needextram != 0 {
+ return
+ }
cgocallback(unsafe.Pointer(funcPC(sigsend)), noescape(unsafe.Pointer(&sig)), unsafe.Sizeof(sig))
}