Datsik
Datsik

Reputation: 14824

How come this loop is considered a data race [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.

Upvotes: 4

Views: 1077

Answers (1)

superfell
superfell

Reputation: 19040

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.

Upvotes: 10

Related Questions