Fix data-race in UpdateSelf (#991)

This commit is contained in:
Egon Elbre 2019-01-08 18:01:22 +02:00 committed by GitHub
parent 387fb14e23
commit 9e55a7209d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 7 deletions

View File

@ -176,7 +176,11 @@ func (k *Kademlia) Bootstrap(ctx context.Context) error {
bootstrapContext, bootstrapCancel := context.WithCancel(ctx)
atomic.StorePointer(&k.bootstrapCancel, unsafe.Pointer(&bootstrapCancel))
//find nodes most similar to self
_, err := k.lookup(bootstrapContext, k.routingTable.self.Id, true)
k.routingTable.mutex.Lock()
id := k.routingTable.self.Id
k.routingTable.mutex.Unlock()
_, err := k.lookup(bootstrapContext, id, true)
return err
}

View File

@ -96,6 +96,8 @@ func (rt *RoutingTable) Close() error {
// Local returns the local nodes ID
func (rt *RoutingTable) Local() pb.Node {
rt.mutex.Lock()
defer rt.mutex.Unlock()
return rt.self
}
@ -163,15 +165,13 @@ func (rt *RoutingTable) FindNear(id storj.NodeID, limit int) (nodes []*pb.Node,
// UpdateSelf updates a node on the routing table
func (rt *RoutingTable) UpdateSelf(node *pb.Node) error {
if node.Id != rt.Local().Id {
// TODO: replace UpdateSelf with UpdateRestrictions and UpdateAddress
rt.mutex.Lock()
if node.Id != rt.self.Id {
rt.mutex.Unlock()
return RoutingErr.New("self does not have a matching node id")
}
rt.mutex.Lock()
rt.self = *node
rt.mutex.Unlock()
rt.mutex.Lock()
rt.seen[node.Id] = node
rt.mutex.Unlock()