// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
/*
#include <pthread.h>
void go_callback();
static void *thr(void *arg) {
go_callback();
return 0;
}
static void foo() {
pthread_t th;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, 256 << 10);
pthread_create(&th, &attr, thr, 0);
pthread_join(th, 0);
}
*/
import "C"
import (
"runtime"
)
//export go_callback
func go_callback() {
runtime.Goexit()
}
func main() {
C.foo()
}
The problem here is that it is not valid to call runtime.Goexit in a cgo callback from C to Go on a C-created thread. I think that restriction is fine (it is unclear what Goexit should do if we were to allow it), but the error message here should be more clear.
I think with an additional mp.isextra check in gdestroy we could be more explicit about what the user did wrong.
We should also update the runtime.Goexit docs to mention this case.
Go version
tip
Output of
go env
in your module/workspace:What did you do?
What did you see happen?
Note that this error was improved a bit recently in https://go.dev/cl/583675. In 1.22, the error is
What did you expect to see?
A more clear description of the problem.
The problem here is that it is not valid to call
runtime.Goexit
in a cgo callback from C to Go on a C-created thread. I think that restriction is fine (it is unclear what Goexit should do if we were to allow it), but the error message here should be more clear.I think with an additional
mp.isextra
check ingdestroy
we could be more explicit about what the user did wrong.We should also update the
runtime.Goexit
docs to mention this case.