Skip to content

runtime/race: panic+recover in init() adds extra info to data race stack traces in main() #46095

@mattfedd

Description

@mattfedd

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

$ go version
go version go1.16.4 windows/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
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Matthew\AppData\Local\go-build
set GOENV=C:\Users\Matthew\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Matthew\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Matthew\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16.4
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=F:\Documents\Github\temp\go-test\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\Matthew\AppData\Local\Temp\go-build309976296=/tmp/go-build -gno-record-gcc-switches

What did you do?

Ran the following program with go run -race main.go

package main

import (
	"fmt"
)

func init() {
	// call multiple times to exaggerate issue
	A()
	A()
	A()
	A()
}

func A() {
	defer func() {
		if e := recover(); e != nil {
			fmt.Println(e)
		}
	}()
	fmt.Println("A")
	B()
}

func B() {
	// this level of separation between the panic call and the recover call appears to be important.
	// If panic/recover happens within the same function, or within 1 call stack, nothing looks weird in the output data race
	fmt.Println("B")
	C()
}

func C() {
	fmt.Println("C")
	panic("panicking")
}

func main() {
	a := 0
	b := 0
	go func() {
		b = 1
	}()

	go func() {
		a = b
	}()
}

What did you expect to see?

I expect a detected data race in main() to not contain stack trace information from init().

> go run -race main.go
A
B
C
panicking
A
B
C
panicking
A
B
C
panicking
A
B
C
panicking
==================
WARNING: DATA RACE
Read at 0x00c00012e0e0 by goroutine 8:
  main.main.func2()
      F:/Documents/Github/temp/go-test/main.go:45 +0x44

Previous write at 0x00c00012e0e0 by goroutine 7:
  main.main.func1()
      F:/Documents/Github/temp/go-test/main.go:41 +0x44

Goroutine 8 (running) created at:
  main.main()
      F:/Documents/Github/temp/go-test/main.go:44 +0xdc

Goroutine 7 (finished) created at:
  main.main()
      F:/Documents/Github/temp/go-test/main.go:40 +0xb0
==================
Found 1 data race(s)
exit status 66

What did you see instead?

> go run -race main.go
A
B
C
panicking
A
B
C
panicking
A
B
C
panicking
A
B
C
panicking
==================
WARNING: DATA RACE
Read at 0x00c00012e128 by goroutine 8:
  main.main.func2()
      F:/Documents/Github/temp/go-test/main.go:45 +0x44

Previous write at 0x00c00012e128 by goroutine 7:
  main.main.func1()
      F:/Documents/Github/temp/go-test/main.go:41 +0x44

Goroutine 8 (running) created at:
  main.main()
      F:/Documents/Github/temp/go-test/main.go:44 +0xdc
  runtime.main()
      C:/Program Files/Go/src/runtime/proc.go:225 +0x255
  main.init.0()
      F:/Documents/Github/temp/go-test/main.go:11 +0x44
  main.init.0()
      F:/Documents/Github/temp/go-test/main.go:10 +0x3b
  main.init.0()
      F:/Documents/Github/temp/go-test/main.go:9 +0x36

Goroutine 7 (finished) created at:
  main.main()
      F:/Documents/Github/temp/go-test/main.go:40 +0xb0
  runtime.main()
      C:/Program Files/Go/src/runtime/proc.go:225 +0x255
  main.init.0()
      F:/Documents/Github/temp/go-test/main.go:11 +0x44
  main.init.0()
      F:/Documents/Github/temp/go-test/main.go:10 +0x3b
  main.init.0()
      F:/Documents/Github/temp/go-test/main.go:9 +0x36
==================
Found 1 data race(s)
exit status 66

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.RaceDetectorcompiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    Status

    Triage Backlog

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions