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 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题