cache: fix data race in NotifyJoin (#1028)

In 35af5c0b91, the check for multiple
cache servers in NotifyJoin is made to be done in a goroutine. That can
lead to a data race, because the memberlist can be changed at the time
the goroutine was run. go warns about this race when test memberlist was
run with "-race".

To fix this, we pass the nil check as argument to goroutine.
This commit is contained in:
Cuong Manh Le 2020-07-01 00:11:40 +07:00 committed by GitHub
parent 6ab797eb0b
commit b90885b4c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

6
cache/memberlist.go vendored
View file

@ -69,11 +69,11 @@ func (c *Cache) runMemberList(ctx context.Context) error {
func (mh *memberlistHandler) NotifyJoin(node *memberlist.Node) {
mh.log.Debug().Interface("node", node).Msg("node joined")
go func() {
if mh.memberlist != nil && mh.memberlist.NumMembers() > 1 {
go func(memberListNotNil bool) {
if memberListNotNil && mh.memberlist.NumMembers() > 1 {
mh.log.Error().Msg("detected multiple cache servers, which is not supported")
}
}()
}(mh.memberlist != nil)
}
func (mh *memberlistHandler) NotifyLeave(node *memberlist.Node) {