Skip to content

runtime/race: false positive with go + defer #42599

@randall77

Description

@randall77
package main

import (
	"runtime"
	"sync/atomic"
)

type foo struct {
	bar int64
}

func (f *foo) doFork(depth int) {
	atomic.StoreInt64(&f.bar, 1)
	defer atomic.StoreInt64(&f.bar, 0)
	if depth > 0 {
		for i := 0; i < 2; i++ {
			f2 := &foo{}
			go f2.doFork(depth - 1)
		}
	}
	runtime.GC()
}

func main() {
	f := &foo{}
	f.doFork(11)
}

This reports a race when run with the race detector. I don't think it should.

==================
WARNING: DATA RACE
Write at 0x00c00011a008 by goroutine 6:
  sync/atomic.StoreInt64()
      /home/khr/go1.14/src/runtime/race_amd64.s:234 +0xb
  main.(*foo).doFork()
      /home/khr/gowork/tmp1.go:22 +0x135

Previous write at 0x00c00011a008 by goroutine 2576:
  main.(*foo).doFork()
      /home/khr/gowork/tmp1.go:17 +0xdb

Goroutine 6 (running) created at:
  main.(*foo).doFork()
      /home/khr/gowork/tmp1.go:18 +0x118
  main.main()
      /home/khr/gowork/tmp1.go:26 +0x6b

Goroutine 2576 (running) created at:
  main.(*foo).doFork()
      /home/khr/gowork/tmp1.go:18 +0x118
==================

This fails at tip, go1.14 and go1.15. Not a release blocker.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions