登录
首页 >  Golang >  Go问答

Golang Race Detector 出现误报?

来源:stackoverflow

时间:2024-04-18 12:09:33 409浏览 收藏

哈喽!大家好,很高兴又见面了,我是golang学习网的一名作者,今天由我给大家带来一篇《Golang Race Detector 出现误报?》,本文主要会讲到等等知识点,希望大家一起学习进步,也欢迎大家关注、点赞、收藏、转发! 下面就一起来看看吧!

问题内容

我几天前发布了这个问题,但由于代码中有错误而被关闭。已修复此问题,因此重新发布此内容

package main

import (
    "fmt"
    "time"
    "sync/atomic"
    "math/rand"
)
//this data is normally fetched via http request
var dummydata1 = []string{"a", "b", "c", "d", "e", "f"}
var activemap = new(int32)
var map1 = make(map[string]*int32)
var map2 map[string]*int32
var combinedmap = make(map[string]*int32)

func mapkeyupdater () {
    for _, key := range dummydata1 {
        combinedmap[key] = new(int32)
        map1[key] = new(int32)
    }
    atomic.addint32(activemap, 1)
    time.sleep(3 * time.second)
    for {
        if atomic.loadint32(activemap) == 1 {
            map2 = make(map[string]*int32)
            for _, key := range dummydata1 {
                map2[key] = new(int32)
            }
            atomic.addint32(activemap, 1)
            time.sleep(500 * time.millisecond) //added after edit. see below
            for key, count := range map1{
                *combinedmap[key] += *count
            }
        } else {
            map1 = make(map[string]*int32)
            for _, key := range dummydata1 {
                map1[key] = new(int32)
            }
            atomic.addint32(activemap, -1)
            time.sleep(500 * time.millisecond) //added after edit. see below
            for key, count := range map2 {
                *combinedmap[key] += *count
            }
        }
        time.sleep(3 * time.second)
    }
}

func counter () {
    for {
        randomindex := rand.intn(5)
        randomkey := dummydata1[randomindex]
        if atomic.loadint32(activemap) == 1 {
            val := atomic.addint32(map1[randomkey], 100)
            fmt.printf("added 100 to %v in map1. updated value %v\n", randomkey, val)
        } else {
            val := atomic.addint32(map2[randomkey], 100)
            fmt.printf("added 100 to %v in map2. updated value %v\n", randomkey, val)
        }
    }
}

func main () {
    go mapkeyupdater()
    time.sleep(500 * time.millisecond)
    go counter()
    time.sleep(15 * time.second)
}

现在,当我使用命令 go run -raceracebug.go 运行此命令时,我每次都会得到 4 个 race。然而,从输出中可以清楚地看出,没有比赛,并且地图正在按预期工作

==================
added 100 to e in map2. updated value 7990900
warning: data race
write at 0x0000011cdbd0 by goroutine 7:
added 100 to a in map2. updated value 7972000
  main.mapkeyupdater()
      /racebug.go:34 +0x14d

previous read at 0x0000011cdbd0 by goroutine 9:
added 100 to e in map2. updated value 7991000
  [failed to restore the stack]

goroutine 7 (running) created at:
  main.main()
      /racebug.go:62 +0x29
added 100 to e in map2. updated value 7991100

goroutine 9 (running) created at:
  main.main()
      /racebug.go:64 +0x44
==================
added 100 to c in map2. updated value 7956400
added 100 to b in map2. updated value 7993400
==================
warning: data race
added 100 to e in map1. updated value 100
read at 0x00c00001acec by goroutine 7:
  main.mapkeyupdater()
      /racebug.go:40 +0x2d4

added 100 to c in map1. updated value 100
previous write at 0x00c00001acec by goroutine 9:
  sync/atomic.addint32()
      /usr/local/cellar/go/1.18/libexec/src/runtime/race_amd64.s:279 +0xb
  sync/atomic.addint32()
      :1 +0x1a
added 100 to d in map1. updated value 100

goroutine 7 (running) created at:
  main.main()
      /racebug.go:62 +0x29

added 100 to b in map1. updated value 100
goroutine 9 (running) created at:
  main.main()
      /racebug.go:64 +0x44
==================

google 工程师的这篇文章说 - https://medium.com/@val_deleplace/does-the-race- detector-catch-all-data-races-1afed51d57fb

如果您坚信自己目睹了误报,请报告竞争检测器的错误。如果您有充分的理由相信竞争条件是由标准库或运行时(而不是您自己的代码)引起的,请报告标准库或运行时的错误。

由于我对 go 还很陌生,只是想得到一些确认。

编辑:为了确保 combinedmap 循环在开始之前有足够的时间,我添加了 time.sleep(500 * time.millisecond)。然而,仍然会检测到 race,但输出现在有所不同。

新输出

==================
WARNING: DATA RACEAdded 100 to e in Map2. Updated value 9447300

Write at 0x0000011cdbd0 by goroutine 7:
Added 100 to c in Map2. Updated value 9465100
  main.mapKeyUpdater()
      /raceBug2.go:35 +0x14d

Previous read at 0x0000011cdbd0 by goroutine 9:
Added 100 to b in Map2. Updated value 9461300
  [failed to restore the stack]

Goroutine 7 (running) created at:
  main.main()
      /raceBug2.go:64 +0x29
Added 100 to d in Map2. Updated value 9479400

Goroutine 9 (running) created at:
  main.main()
      /raceBug2.go:66 +0x44
Added 100 to c in Map2. Updated value 9465200
==================

正确答案


当两个 goroutine 同时访问同一个变量,并且至少其中一次访问是写入时,就会发生数据竞争。

在您的代码中,全局变量map1和map2由两个goroutine访问。使用原子包来读取和操作映射项是不够的,因为在mapkeyupdater中重新创建映射时,映射项值(int32的指针)发生了变化。使用互斥体锁定这两个映射以消除竞争。

package main

import (
    "fmt"
    "math/rand"
    "sync"
    "sync/atomic"
    "time"
)

//This data is normally fetched via HTTP Request
var dummyData1 = []string{"a", "b", "c", "d", "e", "f"}
var activeMap = new(int32)
var combinedMap = make(map[string]*int32)

type myMap struct {
    mutex sync.RWMutex
    value map[string]*int32
}

var (
    map1 = myMap{
        value: make(map[string]*int32),
    }
    map2 = myMap{}
)

func mapKeyUpdater() {
    for _, key := range dummyData1 {
        combinedMap[key] = new(int32)
        map1.mutex.Lock()
        map1.value[key] = new(int32)
        map1.mutex.Unlock()
    }
    atomic.AddInt32(activeMap, 1)
    time.Sleep(3 * time.Second)
    for {
        if atomic.LoadInt32(activeMap) == 1 {
            map2.mutex.Lock()
            map2.value = make(map[string]*int32)
            for _, key := range dummyData1 {
                map2.value[key] = new(int32)
            }
            map2.mutex.Unlock()
            atomic.AddInt32(activeMap, 1)
            time.Sleep(500 * time.Millisecond) //Added after EDIT. See below
            map1.mutex.Lock()
            for key, count := range map1.value {
                *combinedMap[key] += *count
            }
            map1.mutex.Unlock()
        } else {
            map1.mutex.Lock()
            for _, key := range dummyData1 {
                map1.value[key] = new(int32)
            }
            map1.mutex.Unlock()
            atomic.AddInt32(activeMap, -1)
            time.Sleep(500 * time.Millisecond) //Added after EDIT. See below
            map2.mutex.Lock()
            for key, count := range map2.value {
                *combinedMap[key] += *count
            }
            map2.mutex.Unlock()
        }
        time.Sleep(3 * time.Second)
    }
}

func counter() {
    for {
        randomIndex := rand.Intn(5)
        randomKey := dummyData1[randomIndex]
        if atomic.LoadInt32(activeMap) == 1 {
            map1.mutex.Lock()
            val := atomic.AddInt32(map1.value[randomKey], 100)
            map1.mutex.Unlock()
            fmt.Printf("Added 100 to %v in Map1. Updated value %v\n", randomKey, val)
        } else {
            map2.mutex.Lock()
            val := atomic.AddInt32(map2.value[randomKey], 100)
            map2.mutex.Unlock()
            fmt.Printf("Added 100 to %v in Map2. Updated value %v\n", randomKey, val)
        }
    }
}

func main() {
    go mapKeyUpdater()
    time.Sleep(500 * time.Millisecond)
    go counter()
    time.Sleep(15 * time.Second)
}

应避免使用共享变量(如全局 var)向多个 goroutine 发送值。使用渠道是推荐的方式。注意:使用通道传递指针也不安全。只需通过通道发送值即可。

Do not communicate by sharing memory; instead, share memory by communicating.

到这里,我们也就讲完了《Golang Race Detector 出现误报?》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>