Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime: "fatal: morestack on g0" and blocking forever when go export functions is called in any signal handlers registered by the non-Go code #35814

Closed
searKing opened this issue Nov 25, 2019 · 2 comments

Comments

@searKing
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.13.4 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/chenhaixin/Library/Caches/go-build"
GOENV="/Users/chenhaixin/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY="gitlab.hobot.cc"
GONOSUMDB="gitlab.hobot.cc"
GOOS="darwin"
GOPATH="/Users/chenhaixin/go"
GOPRIVATE="gitlab.hobot.cc"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/12/870qx8rd0_d96nt6g078wp080000gn/T/go-build405046801=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

The following repository contains an example of the problem:

https://github.com/searKing/go-issue-call-go-in-c-signal-handler

(Note: this code can only be compiled and run on a unix-like system.)

What did you expect to see?

  • ExampleTestCHandlerCalledByCSignal passed.
    Output:Got signal: interrupt.
  • ExampleTestGoHandlerCalledByCSignal passed.
    Output:Got signal: interrupt.
  • ExampleTestGoHandlerCalledByCSignalWithSigAltStack passed.
    Output:Got signal: interrupt.

What did you see instead?

  • ExampleTestCHandlerCalledByCSignal passed.
    Output:Got signal: interrupt.
  • ExampleTestGoHandlerCalledByCSignal failed.
    Output:Got none signal in 1s, Time overseed.
  • ExampleTestGoHandlerCalledByCSignalWithSigAltStack failed.
    Output:Got none signal in 1s, Time overseed.

if executed in a binary under https://github.com/searKing/go-issue-call-go-in-c-signal-handler/tree/master/cmd, fatal: morestack on g0 will be printed sometimes.

@searKing searKing changed the title runtime: fatal: morestack on g0 and blocking forever when go export functions is called in any signal handlers registered by the non-Go code runtime: "fatal: morestack on g0" and blocking forever when go export functions is called in any signal handlers registered by the non-Go code Nov 25, 2019
searKing added a commit to searKing/go-issue-call-go-in-c-signal-handler that referenced this issue Nov 25, 2019
@searKing
Copy link
Author

I have also tried to rewrite signal handler in golang asm, with runtime.setsig, but fatal: morestack on g0 will be thrown directly.

@ianlancetaylor
Copy link
Contributor

ianlancetaylor commented Nov 25, 2019

I'm sorry, this will never work. A signal handler can only call async-signal-safe functions. Go code will never be async-signal-safe. That would be nearly impossible to implement, and it is not a goal.

I recommend that you open a pipe, read from the pipe on the Go side, and in the signal handler write a byte to the pipe. The write call is async-signal-safe.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants