Skip to content

Commit 174d094

Browse files
janoszelig
authored andcommitted
network: fix data race in simulation.NewBzzInProc (ethersphere#1762)
1 parent 02bbb14 commit 174d094

File tree

3 files changed

+7
-24
lines changed

3 files changed

+7
-24
lines changed

network/retrieval/retrieve_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -318,13 +318,8 @@ func newBzzRetrieveWithLocalstore(ctx *adapters.ServiceContext, bucket *sync.Map
318318
return nil, nil, err
319319
}
320320

321-
var kad *network.Kademlia
322-
if kv, ok := bucket.Load(simulation.BucketKeyKademlia); ok {
323-
kad = kv.(*network.Kademlia)
324-
} else {
325-
kad = network.NewKademlia(addr.Over(), network.NewKadParams())
326-
bucket.Store(simulation.BucketKeyKademlia, kad)
327-
}
321+
k, _ := bucket.LoadOrStore(simulation.BucketKeyKademlia, network.NewKademlia(addr.Over(), network.NewKadParams()))
322+
kad := k.(*network.Kademlia)
328323

329324
netStore := storage.NewNetStore(localStore, kad.BaseAddr(), n.ID())
330325
lnetStore := storage.NewLNetStore(netStore)

network/simulation/simulation.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,9 @@ func NewBzzInProc(services map[string]ServiceFunc) (s *Simulation) {
107107
hp := network.NewHiveParams()
108108
hp.KeepAliveInterval = time.Duration(200) * time.Millisecond
109109
hp.Discovery = false
110-
var kad *network.Kademlia
111-
112-
// check if another kademlia already exists and load it if necessary - we dont want two independent copies of it
113-
if kv, ok := bucket.Load(BucketKeyKademlia); ok {
114-
kad = kv.(*network.Kademlia)
115-
} else {
116-
kad = network.NewKademlia(addr.Over(), network.NewKadParams())
117-
bucket.Store(BucketKeyKademlia, kad)
118-
}
110+
111+
k, _ := bucket.LoadOrStore(BucketKeyKademlia, network.NewKademlia(addr.Over(), network.NewKadParams()))
112+
kad := k.(*network.Kademlia)
119113

120114
config := &network.BzzConfig{
121115
OverlayAddr: addr.Over(),

network/stream/v2/common_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,9 @@ func newSyncSimServiceFunc(o *SyncSimServiceOptions) func(ctx *adapters.ServiceC
117117
return nil, nil, err
118118
}
119119

120-
var kad *network.Kademlia
121-
122120
// check if another kademlia already exists and load it if necessary - we dont want two independent copies of it
123-
if kv, ok := bucket.Load(simulation.BucketKeyKademlia); ok {
124-
kad = kv.(*network.Kademlia)
125-
} else {
126-
kad = network.NewKademlia(addr.Over(), network.NewKadParams())
127-
bucket.Store(simulation.BucketKeyKademlia, kad)
128-
}
121+
k, _ := bucket.LoadOrStore(simulation.BucketKeyKademlia, network.NewKademlia(addr.Over(), network.NewKadParams()))
122+
kad := k.(*network.Kademlia)
129123

130124
netStore := storage.NewNetStore(localStore, kad.BaseAddr(), n.ID())
131125
lnetStore := storage.NewLNetStore(netStore)

0 commit comments

Comments
 (0)