Skip to content

Commit 1af2dad

Browse files
committed
[FAB-12034] Fix data races in pull_test.go
The pull test has tests which have instances of puller, that send each other messages via a common membership map. This map is populated at the creation of each instance via having each instance register itself into the map. The instance (which is test code) - spawns a goroutine at its creation, which schedules the production code and the latter accesses the said map. This makes it an unsafe memory access, and hence a data race occurs. I simply extractd the goroutine to its own start method, and made all tests first create the instances (which involves the write to the map) and only afterwards - start the goroutine, and thus - there is a happens before relation and the golang memory model is happy. FAB-12034 #done Change-Id: I976e9161873d1a7022b0263189af4cc2d3151001 Signed-off-by: yacovm <yacovm@il.ibm.com>
1 parent 211e63e commit 1af2dad

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

gossip/gossip/pull/pullstore_test.go

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ type pullInstance struct {
7171
msgChan chan *pullMsg
7272
peer2PullInst map[string]*pullInstance
7373
stopChan chan struct{}
74+
pullAdapter *PullAdapter
75+
config Config
7476
}
7577

7678
func (p *pullInstance) Send(msg *proto.SignedGossipMessage, peers ...*comm.RemotePeer) {
@@ -91,6 +93,20 @@ func (p *pullInstance) GetMembership() []discovery.NetworkMember {
9193
return members
9294
}
9395

96+
func (p *pullInstance) start() {
97+
p.mediator = NewPullMediator(p.config, p.pullAdapter)
98+
go func() {
99+
for {
100+
select {
101+
case <-p.stopChan:
102+
return
103+
case msg := <-p.msgChan:
104+
p.mediator.HandleMessage(msg)
105+
}
106+
}
107+
}()
108+
}
109+
94110
func (p *pullInstance) stop() {
95111
p.mediator.Stop()
96112
p.stopChan <- struct{}{}
@@ -139,31 +155,23 @@ func createPullInstanceWithFilters(endpoint string, peer2PullInst map[string]*pu
139155
blockConsumer := func(msg *proto.SignedGossipMessage) {
140156
inst.items.Add(msg.GetDataMsg().Payload.SeqNum)
141157
}
142-
adapter := &PullAdapter{
158+
inst.pullAdapter = &PullAdapter{
143159
Sndr: inst,
144160
MemSvc: inst,
145161
IdExtractor: seqNumFromMsg,
146162
MsgCons: blockConsumer,
147163
EgressDigFilter: df,
148164
IngressDigFilter: digestsFilter,
149165
}
150-
inst.mediator = NewPullMediator(conf, adapter)
151-
go func() {
152-
for {
153-
select {
154-
case <-inst.stopChan:
155-
return
156-
case msg := <-inst.msgChan:
157-
inst.mediator.HandleMessage(msg)
158-
}
159-
}
160-
}()
166+
inst.config = conf
167+
161168
return inst
162169
}
163170

164171
func TestCreateAndStop(t *testing.T) {
165172
t.Parallel()
166173
pullInst := createPullInstance("localhost:2000", make(map[string]*pullInstance))
174+
pullInst.start()
167175
pullInst.stop()
168176
}
169177

@@ -172,6 +180,8 @@ func TestRegisterMsgHook(t *testing.T) {
172180
peer2pullInst := make(map[string]*pullInstance)
173181
inst1 := createPullInstance("localhost:5611", peer2pullInst)
174182
inst2 := createPullInstance("localhost:5612", peer2pullInst)
183+
inst1.start()
184+
inst2.start()
175185
defer inst1.stop()
176186
defer inst2.stop()
177187

@@ -215,6 +225,8 @@ func TestFilter(t *testing.T) {
215225
inst2 := createPullInstance("localhost:5612", peer2pullInst)
216226
defer inst1.stop()
217227
defer inst2.stop()
228+
inst1.start()
229+
inst2.start()
218230

219231
inst1.mediator.Add(dataMsg(0))
220232
inst1.mediator.Add(dataMsg(1))
@@ -232,6 +244,8 @@ func TestAddAndRemove(t *testing.T) {
232244
peer2pullInst := make(map[string]*pullInstance)
233245
inst1 := createPullInstance("localhost:5611", peer2pullInst)
234246
inst2 := createPullInstance("localhost:5612", peer2pullInst)
247+
inst1.start()
248+
inst2.start()
235249
defer inst1.stop()
236250
defer inst2.stop()
237251

@@ -268,6 +282,8 @@ func TestDigestsFilters(t *testing.T) {
268282
inst1 := createPullInstanceWithFilters("localhost:5611", make(map[string]*pullInstance), nil, df1)
269283
inst2 := createPullInstance("localhost:5612", make(map[string]*pullInstance))
270284
inst1ReceivedDigest := int32(0)
285+
inst1.start()
286+
inst2.start()
271287

272288
defer inst1.stop()
273289
defer inst2.stop()
@@ -303,6 +319,8 @@ func TestHandleMessage(t *testing.T) {
303319
t.Parallel()
304320
inst1 := createPullInstance("localhost:5611", make(map[string]*pullInstance))
305321
inst2 := createPullInstance("localhost:5612", make(map[string]*pullInstance))
322+
inst1.start()
323+
inst2.start()
306324
defer inst1.stop()
307325
defer inst2.stop()
308326

0 commit comments

Comments
 (0)