Fixes #68275
Change-Id: I47b7a2092f1b4d48aebf437db4e329815c956bb9
GitHub-Last-Rev:
b89bf3cab7f9f7611122f535914f2788564643c5
GitHub-Pull-Request: golang/go#69126
Reviewed-on: https://go-review.googlesource.com/c/go/+/609296
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>
Reviewed-by: Ian Lance Taylor <iant@google.com>
// without func main returning. Since func main has not returned,
// the program continues execution of other goroutines.
// If all other goroutines exit, the program crashes.
+//
+// It crashes if called from a thread not created by the Go runtime.
func Goexit() {
// Create a panic object for Goexit, so we can recognize when it might be
// bypassed by a recover().
if locked && mp.lockedInt != 0 {
print("runtime: mp.lockedInt = ", mp.lockedInt, "\n")
+ if mp.isextra {
+ throw("runtime.Goexit called in a thread that was not created by the Go runtime")
+ }
throw("exited a goroutine internally locked to the OS thread")
}
gfput(pp, gp)
t.Errorf("output:\n%s\nwanted:\nunknown function: NonexistentTest", output)
}
}
+
+func TestCgoToGoCallGoexit(t *testing.T) {
+ if runtime.GOOS == "plan9" || runtime.GOOS == "windows" {
+ t.Skipf("no pthreads on %s", runtime.GOOS)
+ }
+ output := runTestProg(t, "testprogcgo", "CgoToGoCallGoexit")
+ if !strings.Contains(output, "runtime.Goexit called in a thread that was not created by the Go runtime") {
+ t.Fatalf("output should contain %s, got %s", "runtime.Goexit called in a thread that was not created by the Go runtime", output)
+ }
+}
func init() {
register("CgoCallbackGC", CgoCallbackGC)
+ register("CgoToGoCallGoexit", CgoToGoCallGoexit)
}
+func CgoToGoCallGoexit() {
+ goexit = true
+ C.foo()
+}
+
+var goexit = false
+
//export go_callback
func go_callback() {
+ if goexit {
+ runtime.Goexit()
+ }
if e := extraMInUse.Load(); e == 0 {
fmt.Printf("in callback extraMInUse got %d want >0\n", e)
os.Exit(1)