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

reflect: fatal error: reflect mismatch when reflect.MakeFunc is used as a goroutine #25897

Closed
liulk opened this issue Jun 14, 2018 · 6 comments

Comments

Projects
None yet
8 participants
@liulk
Copy link

commented Jun 14, 2018

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"

@bradfitz bradfitz added the NeedsFix label Jun 14, 2018

@bradfitz bradfitz added this to the Go1.11 milestone Jun 14, 2018

@bradfitz

This comment has been minimized.

Copy link
Member

commented Jun 14, 2018

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented 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.

@ianlancetaylor ianlancetaylor changed the title 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 Jun 14, 2018

@ianlancetaylor ianlancetaylor modified the milestones: Go1.11, Go1.12 Jun 14, 2018

@andybons andybons modified the milestones: Go1.12, Go1.13 Feb 12, 2019

@odeke-em

This comment has been minimized.

Copy link
Member

commented Mar 12, 2019

Kindly paging also @randall77

@josharian

This comment has been minimized.

Copy link
Contributor

commented 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

This comment has been minimized.

Copy link
Contributor

commented 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 for reflect.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:

package main

import (
	"reflect"
	"runtime"
)

const N = 100

func main() {
	c := make(chan bool, N)
	for i := 0; i < N; i++ {
		f := reflect.MakeFunc(reflect.TypeOf(((func(*int))(nil))),
			func(args []reflect.Value) []reflect.Value {
				c <- true
				return nil
			}).Interface().(func(*int))
		go f(nil)
	}
	runtime.GC()
	for i := 0; i < N; i++ {
		<-c
	}
}

run with

GOMAXPROCS=1 go run issue25897.go

@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

This comment has been minimized.

Copy link

commented 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

@gopherbot gopherbot closed this in 38c129b Jun 3, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.