I ran into the following race detector error:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Goroutine 44 was running this code:
1 2 3 |
|
and nil’ing out the r.EventChan field.
While goroutine 27 was calling this code on the same *Replication
instance:
1 2 3 |
|
It didn’t make sense, because they were accessing different fields of the Replication
— one was writing to r.EventChan
while the other was reading from r.Stats
.
Then I changed the GetStats()
method to this:
1 2 3 |
|
and it still failed!
I started wandering around the Couchbase office looking for help, and got Steve Yen to help me.
He was asking me about using a pointer receiver vs a value receiver here, and then we realized that by using a value reciever it was copying all the fields, and therefore reading all of the fields, including the r.EventChan
field that the other goroutine was concurrently writing to! Hence, the data race that was subtly caused by using a value receiver..
The fix was to convert this over to a pointer reciever, and the data race disappeared!
1 2 3 |
|