From 9e55a7209dd43303411585aa3daa0006ca9b986d Mon Sep 17 00:00:00 2001 From: Egon Elbre Date: Tue, 8 Jan 2019 18:01:22 +0200 Subject: [PATCH] Fix data-race in UpdateSelf (#991) --- pkg/kademlia/kademlia.go | 6 +++++- pkg/kademlia/routing.go | 12 ++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pkg/kademlia/kademlia.go b/pkg/kademlia/kademlia.go index dce76ff00..2b6a9adfe 100644 --- a/pkg/kademlia/kademlia.go +++ b/pkg/kademlia/kademlia.go @@ -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 } diff --git a/pkg/kademlia/routing.go b/pkg/kademlia/routing.go index 610c6fbab..9fad40fe7 100644 --- a/pkg/kademlia/routing.go +++ b/pkg/kademlia/routing.go @@ -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()