Skip to content

Commit 8203295

Browse files
cuonglmgopherbot
authored andcommitted
runtime: Goexit on C-created thread report more useful error message
This reverts CL 609296, with the fix for failing builders. Fixes #68275 Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-nocgo,gotip-darwin-amd64-nocgo,gotip-linux-ppc64_power10,gotip-linux-ppc64_power8 Change-Id: I0f539ee7b0be720642eee8885946edccd9c6e04e Reviewed-on: https://go-review.googlesource.com/c/go/+/612335 Reviewed-by: Tim King <taking@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Commit-Queue: Ian Lance Taylor <iant@google.com> Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: qiu laidongfeng2 <2645477756@qq.com>
1 parent 26aa8d6 commit 8203295

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

src/runtime/crash_cgo_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -859,3 +859,13 @@ func TestStackSwitchCallback(t *testing.T) {
859859
t.Errorf("expected %q, got %v", want, got)
860860
}
861861
}
862+
863+
func TestCgoToGoCallGoexit(t *testing.T) {
864+
if runtime.GOOS == "plan9" || runtime.GOOS == "windows" {
865+
t.Skipf("no pthreads on %s", runtime.GOOS)
866+
}
867+
output := runTestProg(t, "testprogcgo", "CgoToGoCallGoexit")
868+
if !strings.Contains(output, "runtime.Goexit called in a thread that was not created by the Go runtime") {
869+
t.Fatalf("output should contain %s, got %s", "runtime.Goexit called in a thread that was not created by the Go runtime", output)
870+
}
871+
}

src/runtime/panic.go

+2
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,8 @@ func deferreturn() {
614614
// without func main returning. Since func main has not returned,
615615
// the program continues execution of other goroutines.
616616
// If all other goroutines exit, the program crashes.
617+
//
618+
// It crashes if called from a thread not created by the Go runtime.
617619
func Goexit() {
618620
// Create a panic object for Goexit, so we can recognize when it might be
619621
// bypassed by a recover().

src/runtime/proc.go

+3
Original file line numberDiff line numberDiff line change
@@ -4321,6 +4321,9 @@ func gdestroy(gp *g) {
43214321

43224322
if locked && mp.lockedInt != 0 {
43234323
print("runtime: mp.lockedInt = ", mp.lockedInt, "\n")
4324+
if mp.isextra {
4325+
throw("runtime.Goexit called in a thread that was not created by the Go runtime")
4326+
}
43244327
throw("exited a goroutine internally locked to the OS thread")
43254328
}
43264329
gfput(pp, gp)

src/runtime/testdata/testprogcgo/callback.go

+11
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,21 @@ import (
3838

3939
func init() {
4040
register("CgoCallbackGC", CgoCallbackGC)
41+
register("CgoToGoCallGoexit", CgoToGoCallGoexit)
4142
}
4243

44+
func CgoToGoCallGoexit() {
45+
goexit = true
46+
C.foo()
47+
}
48+
49+
var goexit = false
50+
4351
//export go_callback
4452
func go_callback() {
53+
if goexit {
54+
runtime.Goexit()
55+
}
4556
if e := extraMInUse.Load(); e == 0 {
4657
fmt.Printf("in callback extraMInUse got %d want >0\n", e)
4758
os.Exit(1)

0 commit comments

Comments
 (0)