doujiu8145 2015-12-02 04:42
浏览 21
已采纳

如何将此循环视为数据竞赛[Golang]

I have a loop and I get this error, I'm running go run -race main.go

This is the loop in question:

var wg sync.WaitGroup

for _, member := range p.Members { << Line 254
    go func() {
        wg.Add(1)
        _, err := db.Exec("UPDATE party_members SET active = ? WHERE steamid = ?", false, member.SteamID) << Line 257
        if err != nil {
            log.Println(err)
        }
        _, err = db.Exec("INSERT INTO party_members SET belongs_to =?, active = ?, steamid = ?", partyUUID, true, member.SteamID)
        if err != nil {
            log.Println(err)
        }
        wg.Done()
    }() << Line 266
}

and then this is the error I get :

WARNING: DATA RACE
Read by goroutine 44:
  BLAH/controllers/partybot.func·001()
      /Users/Dev/gocode/src/BLAH/controllers/partybot/partybot.go:257 +0x136

Previous write by goroutine 43:
  BLAH/controllers/partybot.(*PartialParty).SendReadyCheck()
      /Users/Dev/gocode/src/BLAH/controllers/partybot/partybot.go:254 +0xda5

Goroutine 44 (running) created at:
  BLAH/controllers/partybot.(*PartialParty).SendReadyCheck()
      /Users/Dev/gocode/src/BLAH/controllers/partybot/partybot.go:266 +0xf13

Goroutine 43 (finished) created at:
  BLAH/controllers/partybot.(*PartyHub).SortMemberIntoParty()
      /Users/Dev/gocode/src/BLAH/controllers/partybot/partybot.go:173 +0xdb9
  BLAH/controllers/partybot.(*Connection).readPump()
      /Users/Dev/gocode/src/BLAH/controllers/partybot/partybot.go:359 +0x1872
  BLAH/controllers/partybot.WSPartyBot()
      /Users/Dev/gocode/src/BLAH/controllers/partybot/partybot.go:440 +0x4f0
  net/http.HandlerFunc.ServeHTTP()
      /usr/local/go/src/net/http/server.go:1265 +0x4e
  github.com/gorilla/mux.(*Router).ServeHTTP()
      /Users/Dev/gocode/src/github.com/gorilla/mux/mux.go:98 +0x377
  BLAH/controllers/common.func·001()
      /Users/Dev/gocode/src/BLAH/controllers/common/common.go:36 +0xc2
  net/http.HandlerFunc.ServeHTTP()
      /usr/local/go/src/net/http/server.go:1265 +0x4e
  net/http.(*ServeMux).ServeHTTP()
      /usr/local/go/src/net/http/server.go:1541 +0x20c
  net/http.serverHandler.ServeHTTP()
      /usr/local/go/src/net/http/server.go:1703 +0x1f6
  net/http.(*conn).serve()
      /usr/local/go/src/net/http/server.go:1204 +0x1087
==================

I'm not sure why this is a Data race as I'm fairly new to Go anyways if anybody could tell me why this is that would be great, I marked the lines in question.

  • 写回答

1条回答 默认 最新

  • dongshou2024 2015-12-02 05:13
    关注

    the problem is that the goroutine is accessing the variable 'member' via the closure, and in go this reference is evaluated when its reached (i.e. it sees the value of member as of when the goroutine executes, not when it was created via the go func()... call. the member variable is being updated by the loop, so there's a race between the loop updates and the goroutines you've started reading them. (you'll find if you dig further that your db doesn't see the exact set of members from the collection). Typically you resolve this by forcing an evaluation of the variable in the loop, either by creating a local var, or by adding it as a parameter to the func, e.g.

    var wg sync.WaitGroup
    
    for _, member := range p.Members {
        wg.Add(1)
        m := member
        go func() {
            _, err := db.Exec("UPDATE party_members SET active = ? WHERE steamid = ?", false, m.SteamID)
            ...
            wg.Done()
        }() 
    }
    

    or

    var wg sync.WaitGroup
    
    for _, member := range p.Members {
        wg.Add(1)
        go func(m memberType) {
            _, err := db.Exec("UPDATE party_members SET active = ? WHERE steamid = ?", false, m.SteamID)
            ...
            wg.Done()
        }(member) 
    }
    

    also note that you need to call wg.Add(1) from the loop, not from within the goroutine itself.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?