Skip to content

Commit f82185a

Browse files
frncmxzelig
authored andcommitted
p2p/protocols: fix data race in TestProtocolHook (#19242)
dummyHook's fields were concurrently written by nodes and read by the test. The simplest solution is to protect all fields with a mutex. Enable: TestMultiplePeersDropSelf, TestMultiplePeersDropOther as they seemingly accidentally stayed disabled during a refactor/rewrite since 1836366. resolves ethersphere/swarm#1286
1 parent bb55b0f commit f82185a

3 files changed

Lines changed: 37 additions & 15 deletions

File tree

‎p2p/protocols/accounting_simulation_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func TestAccountingSimulation(t *testing.T) {
161161
type matrix struct {
162162
n int //number of nodes
163163
m []int64 //array of balances
164-
lock sync.RWMutex
164+
lock sync.Mutex
165165
}
166166

167167
// create a new matrix

‎p2p/protocols/protocol_test.go‎

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"context"
2222
"errors"
2323
"fmt"
24+
"sync"
2425
"testing"
2526
"time"
2627

@@ -172,8 +173,11 @@ func protoHandshakeExchange(id enode.ID, proto *protoHandshake) []p2ptest.Exchan
172173
}
173174

174175
func runProtoHandshake(t *testing.T, proto *protoHandshake, errs ...error) {
176+
t.Helper()
175177
pp := p2ptest.NewTestPeerPool()
176178
s := protocolTester(pp)
179+
defer s.Stop()
180+
177181
// TODO: make this more than one handshake
178182
node := s.Nodes[0]
179183
if err := s.TestExchanges(protoHandshakeExchange(node.ID(), proto)...); err != nil {
@@ -195,13 +199,17 @@ type dummyHook struct {
195199
send bool
196200
err error
197201
waitC chan struct{}
202+
mu sync.Mutex
198203
}
199204

200205
type dummyMsg struct {
201206
Content string
202207
}
203208

204209
func (d *dummyHook) Send(peer *Peer, size uint32, msg interface{}) error {
210+
d.mu.Lock()
211+
defer d.mu.Unlock()
212+
205213
d.peer = peer
206214
d.size = size
207215
d.msg = msg
@@ -210,6 +218,9 @@ func (d *dummyHook) Send(peer *Peer, size uint32, msg interface{}) error {
210218
}
211219

212220
func (d *dummyHook) Receive(peer *Peer, size uint32, msg interface{}) error {
221+
d.mu.Lock()
222+
defer d.mu.Unlock()
223+
213224
d.peer = peer
214225
d.size = size
215226
d.msg = msg
@@ -263,6 +274,7 @@ func TestProtocolHook(t *testing.T) {
263274
if err != nil {
264275
t.Fatal(err)
265276
}
277+
testHook.mu.Lock()
266278
if testHook.msg == nil || testHook.msg.(*dummyMsg).Content != "handshake" {
267279
t.Fatal("Expected msg to be set, but it is not")
268280
}
@@ -278,6 +290,7 @@ func TestProtocolHook(t *testing.T) {
278290
if testHook.size != 11 { //11 is the length of the encoded message
279291
t.Fatalf("Expected size to be %d, but it is %d ", 1, testHook.size)
280292
}
293+
testHook.mu.Unlock()
281294

282295
err = tester.TestExchanges(p2ptest.Exchange{
283296
Triggers: []p2ptest.Trigger{
@@ -294,6 +307,8 @@ func TestProtocolHook(t *testing.T) {
294307
if err != nil {
295308
t.Fatal(err)
296309
}
310+
311+
testHook.mu.Lock()
297312
if testHook.msg == nil || testHook.msg.(*dummyMsg).Content != "response" {
298313
t.Fatal("Expected msg to be set, but it is not")
299314
}
@@ -306,6 +321,7 @@ func TestProtocolHook(t *testing.T) {
306321
if testHook.size != 10 { //11 is the length of the encoded message
307322
t.Fatalf("Expected size to be %d, but it is %d ", 1, testHook.size)
308323
}
324+
testHook.mu.Unlock()
309325

310326
testHook.err = fmt.Errorf("dummy error")
311327
err = tester.TestExchanges(p2ptest.Exchange{
@@ -325,7 +341,6 @@ func TestProtocolHook(t *testing.T) {
325341
if err != nil {
326342
t.Fatalf("Expected a specific disconnect error, but got different one: %v", err)
327343
}
328-
329344
}
330345

331346
//We need to test that if the hook is not defined, then message infrastructure
@@ -342,16 +357,19 @@ func TestNoHook(t *testing.T) {
342357
ctx := context.TODO()
343358
msg := &perBytesMsgSenderPays{Content: "testBalance"}
344359
//send a message
345-
err := peer.Send(ctx, msg)
346-
if err != nil {
360+
361+
if err := peer.Send(ctx, msg); err != nil {
347362
t.Fatal(err)
348363
}
349364
//simulate receiving a message
350365
rw.msg = msg
351-
peer.handleIncoming(func(ctx context.Context, msg interface{}) error {
366+
handler := func(ctx context.Context, msg interface{}) error {
352367
return nil
353-
})
354-
//all should just work and not result in any error
368+
}
369+
370+
if err := peer.handleIncoming(handler); err != nil {
371+
t.Fatal(err)
372+
}
355373
}
356374

357375
func TestProtoHandshakeVersionMismatch(t *testing.T) {
@@ -391,8 +409,11 @@ func moduleHandshakeExchange(id enode.ID, resp uint) []p2ptest.Exchange {
391409
}
392410

393411
func runModuleHandshake(t *testing.T, resp uint, errs ...error) {
412+
t.Helper()
394413
pp := p2ptest.NewTestPeerPool()
395414
s := protocolTester(pp)
415+
defer s.Stop()
416+
396417
node := s.Nodes[0]
397418
if err := s.TestExchanges(protoHandshakeExchange(node.ID(), &protoHandshake{42, "420"})...); err != nil {
398419
t.Fatal(err)
@@ -471,8 +492,10 @@ func testMultiPeerSetup(a, b enode.ID) []p2ptest.Exchange {
471492
}
472493

473494
func runMultiplePeers(t *testing.T, peer int, errs ...error) {
495+
t.Helper()
474496
pp := p2ptest.NewTestPeerPool()
475497
s := protocolTester(pp)
498+
defer s.Stop()
476499

477500
if err := s.TestExchanges(testMultiPeerSetup(s.Nodes[0].ID(), s.Nodes[1].ID())...); err != nil {
478501
t.Fatal(err)
@@ -542,14 +565,14 @@ WAIT:
542565
}
543566

544567
}
545-
func XTestMultiplePeersDropSelf(t *testing.T) {
568+
func TestMultiplePeersDropSelf(t *testing.T) {
546569
runMultiplePeers(t, 0,
547570
fmt.Errorf("subprotocol error"),
548571
fmt.Errorf("Message handler error: (msg code 3): dropped"),
549572
)
550573
}
551574

552-
func XTestMultiplePeersDropOther(t *testing.T) {
575+
func TestMultiplePeersDropOther(t *testing.T) {
553576
runMultiplePeers(t, 1,
554577
fmt.Errorf("Message handler error: (msg code 3): dropped"),
555578
fmt.Errorf("subprotocol error"),

‎p2p/testing/protocoltester.go‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type ProtocolTester struct {
5151
// NewProtocolTester constructs a new ProtocolTester
5252
// it takes as argument the pivot node id, the number of dummy peers and the
5353
// protocol run function called on a peer connection by the p2p server
54-
func NewProtocolTester(id enode.ID, n int, run func(*p2p.Peer, p2p.MsgReadWriter) error) *ProtocolTester {
54+
func NewProtocolTester(id enode.ID, nodeCount int, run func(*p2p.Peer, p2p.MsgReadWriter) error) *ProtocolTester {
5555
services := adapters.Services{
5656
"test": func(ctx *adapters.ServiceContext) (node.Service, error) {
5757
return &testNode{run}, nil
@@ -74,9 +74,9 @@ func NewProtocolTester(id enode.ID, n int, run func(*p2p.Peer, p2p.MsgReadWriter
7474
}
7575

7676
node := net.GetNode(id).Node.(*adapters.SimNode)
77-
peers := make([]*adapters.NodeConfig, n)
78-
nodes := make([]*enode.Node, n)
79-
for i := 0; i < n; i++ {
77+
peers := make([]*adapters.NodeConfig, nodeCount)
78+
nodes := make([]*enode.Node, nodeCount)
79+
for i := 0; i < nodeCount; i++ {
8080
peers[i] = adapters.RandomNodeConfig()
8181
peers[i].Services = []string{"mock"}
8282
nodes[i] = peers[i].Node()
@@ -100,9 +100,8 @@ func NewProtocolTester(id enode.ID, n int, run func(*p2p.Peer, p2p.MsgReadWriter
100100
}
101101

102102
// Stop stops the p2p server
103-
func (t *ProtocolTester) Stop() error {
103+
func (t *ProtocolTester) Stop() {
104104
t.Server.Stop()
105-
return nil
106105
}
107106

108107
// Connect brings up the remote peer node and connects it using the

0 commit comments

Comments
 (0)