Description
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (go version
)?
go version go1.10.3 linux/amd64
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (go env
)?
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GORACE=""
GOROOT="/usr/lib/google-golang"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/google-golang/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="clang"
CGO_ENABLED="1"
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=/tmp/go-build622183756=/tmp/go-build -gno-record-gcc-switches"
What did you do?
I have a function consume()
made by reflect.MakeFunc()
. When I run it as a goroutine go consume(...)
, if it coincides with a GC cycle, the program dies with the following error:
runtime: confused by reflect.makeFuncStub
fatal error: reflect mismatch
...
runtime stack:
...
goroutine 166 [runnable (scan)]:
reflect.makeFuncStub(0xc42001e0c0, 0xc420014140, 0x0, 0x0, 0x0, 0x0, 0xc42013e800, 0x0, 0x0, 0x0, ...)
/usr/lib/google-golang/src/reflect/asm_amd64.s:12
created by main.runConsume
/usr/local/google/home/liulk/tmp/test-go/reflectmismatch.go:52 +0x7e
Each time the fatal error happens, the goroutine with runnable (scan)
status is always in reflect.makeFuncStub()
. However, if I wrap the goroutine under a func() closure like go func() { consume(...) }()
then the problem goes away.
Here is a playground link showing that this issue is reproducible in the playground: https://play.golang.org/p/vDyOEMZoAWf
There is a previous issue also mentioning reflect mismatch, but I think that's a different issue that is already fixed: #18635
What did you expect to see?
The program should run until completion.
What did you see instead?
The program crashed with "fatal error: reflect mismatch"
Activity
bradfitz commentedon Jun 14, 2018
/cc @ianlancetaylor @aclements
ianlancetaylor commentedon Jun 14, 2018
Works in Go 1.3, fails in every version since. Works with gccgo. Easy to work around. Setting milestone to 1.12.
[-]fatal error: reflect mismatch when reflect.MakeFunc is used as a goroutine[/-][+]reflect: fatal error: reflect mismatch when reflect.MakeFunc is used as a goroutine[/+]odeke-em commentedon Mar 12, 2019
Kindly paging also @randall77
josharian commentedon Mar 12, 2019
The easy, shallow fix for this is probably to teach the compiler to do the “wrap in a closure” trick itself. It already does that for a print and some other builtins; this probably just requires adding a special case for (say) any calls in package reflect. Might be a decent starter issue for someone working on the compiler.
randall77 commentedon May 7, 2019
It looks like the problem is scanning a goroutine where the top level function is
reflect.makeFuncStub
, but the goroutine hasn't started yet. There's no frame forreflect.makeFuncStub
that the runtime can grab the arg map from. We'd have to grab that map from the ctxt register (or, more precisely, the g.gobuf.ctxt value), like we do with defers.Here's a simpler repro:
run with
@josharian I don't think the wrapper strategy will work. At the point of the
go
statement, the thing we're running is just a closure - we don't know where the implementation comes from. Or are you saying that we should wrap just makeFuncStub in another wrapper function?gopherbot commentedon Jun 3, 2019
Change https://golang.org/cl/180258 mentions this issue:
runtime: get map of args of unstarted goroutines like we do for defers