Skip to content

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

Closed
@liulk

Description

@liulk

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

added
NeedsFixThe path to resolution is known, but the work has not been done.
on Jun 14, 2018
added this to the Go1.11 milestone on Jun 14, 2018
bradfitz

bradfitz commented on Jun 14, 2018

@bradfitz
Contributor
ianlancetaylor

ianlancetaylor commented on Jun 14, 2018

@ianlancetaylor
Contributor

Works in Go 1.3, fails in every version since. Works with gccgo. Easy to work around. Setting milestone to 1.12.

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[/+] on Jun 14, 2018
modified the milestones: Go1.11, Go1.12 on Jun 14, 2018
modified the milestones: Go1.12, Go1.13 on Feb 12, 2019
odeke-em

odeke-em commented on Mar 12, 2019

@odeke-em
Member

Kindly paging also @randall77

josharian

josharian commented on Mar 12, 2019

@josharian
Contributor

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

randall77 commented on May 7, 2019

@randall77
Contributor

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

gopherbot commented on Jun 3, 2019

@gopherbot
Contributor

Change https://golang.org/cl/180258 mentions this issue: runtime: get map of args of unstarted goroutines like we do for defers

locked and limited conversation to collaborators on Jun 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @bradfitz@josharian@andybons@liulk@ianlancetaylor

        Issue actions

          reflect: fatal error: reflect mismatch when reflect.MakeFunc is used as a goroutine · Issue #25897 · golang/go