From f2e87158f06902daa44d34b4846611f8520e4491 Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Wed, 15 Mar 2017 14:48:23 -0400 Subject: runtime: make GOTRACEBACK=crash crash promptly in cgo binaries GOTRACEBACK=crash works by bouncing a SIGQUIT around the process sched.mcount times. However, sched.mcount includes the extra Ms allocated by oneNewExtraM for cgo callbacks. Hence, if there are any extra Ms that don't have real OS threads, we'll try to send SIGQUIT more times than there are threads to catch it. Since nothing will catch these extra signals, we'll fall back to blocking for five seconds before aborting the process. Avoid this five second delay by subtracting out the number of extra Ms when sending SIGQUITs. Of course, in a cgo binary, it's still possible for the SIGQUIT to go to a cgo thread and cause some other failure mode. This does not fix that. Change-Id: I4fbf3c52dd721812796c4c1dcb2ab4cb7026d965 Reviewed-on: https://go-review.googlesource.com/38182 Run-TryBot: Austin Clements TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/runtime/proc.go | 4 ++++ src/runtime/signal_sighandler.go | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/runtime/proc.go b/src/runtime/proc.go index caeb51205b..45d98de151 100644 --- a/src/runtime/proc.go +++ b/src/runtime/proc.go @@ -1399,6 +1399,7 @@ func needm(x byte) { // running at all (that is, there's no garbage collection // running right now). mp.needextram = mp.schedlink == 0 + extraMCount-- unlockextra(mp.schedlink.ptr()) // Save and block signals before installing g. @@ -1484,6 +1485,7 @@ func oneNewExtraM() { // Add m to the extra list. mnext := lockextra(true) mp.schedlink.set(mnext) + extraMCount++ unlockextra(mp) } @@ -1525,6 +1527,7 @@ func dropm() { unminit() mnext := lockextra(true) + extraMCount++ mp.schedlink.set(mnext) setg(nil) @@ -1541,6 +1544,7 @@ func getm() uintptr { } var extram uintptr +var extraMCount uint32 // Protected by lockextra var extraMWaiters uint32 // lockextra locks the extra list and returns the list head. diff --git a/src/runtime/signal_sighandler.go b/src/runtime/signal_sighandler.go index 5af12d7b2f..3b9ba296d9 100644 --- a/src/runtime/signal_sighandler.go +++ b/src/runtime/signal_sighandler.go @@ -111,7 +111,7 @@ func sighandler(sig uint32, info *siginfo, ctxt unsafe.Pointer, gp *g) { if docrash { crashing++ - if crashing < sched.mcount { + if crashing < sched.mcount-int32(extraMCount) { // There are other m's that need to dump their stacks. // Relay SIGQUIT to the next m by sending it to the current process. // All m's that have already received SIGQUIT have signal masks blocking -- cgit v1.2.1