Skip to content
This repository was archived by the owner on Aug 2, 2021. It is now read-only.
This repository was archived by the owner on Aug 2, 2021. It is now read-only.

Data race in Peer.Handshake() (p2p/protocols/protocol.go) #1160

@frncmx

Description

@frncmx

How to reproduce?

There is no test (yet) what would reliable show this Data Race.

Assumption:
Only occurs in case of an error, as the 2nd write happens on the error path.

Model:

package main


func h() (rhs interface{}) {
	go func(msg interface{}) {
			rhs = msg
	}(1)
	return nil
}

func main() {
	h()
}

go run -race main.go => fails

In Peer.Handshake() we create a closure called handle. That makes an assignment rhs=msg where rhs is the return variable of the enclosing function.

Acceptance criteria

  1. Create a test what reliably fails with -race (is there no test currently to test error path?)
  2. Provide a fix
  3. Prove with the test that the race is gone

Stack trace

==================
WARNING: DATA RACE
Write at 0x00c009bcb020 by goroutine 2984:
  github.com/ethereum/go-ethereum/p2p/protocols.(*Peer).Handshake.func1()
      /home/frncmx/code/src/github.com/ethereum/go-ethereum/p2p/protocols/protocol.go:395 +0x53
  github.com/ethereum/go-ethereum/p2p/protocols.(*Peer).handleIncoming()
      /home/frncmx/code/src/github.com/ethereum/go-ethereum/p2p/protocols/protocol.go:374 +0xa8a
  github.com/ethereum/go-ethereum/p2p/protocols.(*Peer).Handshake.func3()
      /home/frncmx/code/src/github.com/ethereum/go-ethereum/p2p/protocols/protocol.go:402 +0x57
  github.com/ethereum/go-ethereum/p2p/protocols.(*Peer).Handshake.func4()
      /home/frncmx/code/src/github.com/ethereum/go-ethereum/p2p/protocols/protocol.go:410 +0x90

Previous write at 0x00c009bcb020 by goroutine 1361:
  github.com/ethereum/go-ethereum/p2p/protocols.(*Peer).Handshake()
      /home/frncmx/code/src/github.com/ethereum/go-ethereum/p2p/protocols/protocol.go:421 +0x5c4
  github.com/ethereum/go-ethereum/swarm/network.(*Bzz).performHandshake()
      /home/frncmx/code/src/github.com/ethereum/go-ethereum/swarm/network/protocol.go:203 +0x17c
  github.com/ethereum/go-ethereum/swarm/network.(*Bzz).runBzz()
      /home/frncmx/code/src/github.com/ethereum/go-ethereum/swarm/network/protocol.go:223 +0x382
  github.com/ethereum/go-ethereum/swarm/network.(*Bzz).runBzz-fm()
      /home/frncmx/code/src/github.com/ethereum/go-ethereum/swarm/network/protocol.go:125 +0x63
  github.com/ethereum/go-ethereum/p2p.(*Peer).startProtocols.func1()
      /home/frncmx/code/src/github.com/ethereum/go-ethereum/p2p/peer.go:361 +0x8d

Goroutine 2984 (running) created at:
  github.com/ethereum/go-ethereum/p2p/protocols.(*Peer).Handshake()
      /home/frncmx/code/src/github.com/ethereum/go-ethereum/p2p/protocols/protocol.go:404 +0x39d
  github.com/ethereum/go-ethereum/swarm/network.(*Bzz).performHandshake()
      /home/frncmx/code/src/github.com/ethereum/go-ethereum/swarm/network/protocol.go:203 +0x17c
  github.com/ethereum/go-ethereum/swarm/network.(*Bzz).runBzz()
      /home/frncmx/code/src/github.com/ethereum/go-ethereum/swarm/network/protocol.go:223 +0x382
  github.com/ethereum/go-ethereum/swarm/network.(*Bzz).runBzz-fm()
      /home/frncmx/code/src/github.com/ethereum/go-ethereum/swarm/network/protocol.go:125 +0x63
  github.com/ethereum/go-ethereum/p2p.(*Peer).startProtocols.func1()
      /home/frncmx/code/src/github.com/ethereum/go-ethereum/p2p/peer.go:361 +0x8d

Goroutine 1361 (running) created at:
  github.com/ethereum/go-ethereum/p2p.(*Peer).startProtocols()
      /home/frncmx/code/src/github.com/ethereum/go-ethereum/p2p/peer.go:360 +0x2a8
  github.com/ethereum/go-ethereum/p2p.(*Peer).run()
      /home/frncmx/code/src/github.com/ethereum/go-ethereum/p2p/peer.go:210 +0x16a
  github.com/ethereum/go-ethereum/p2p.(*Server).runPeer()
      /home/frncmx/code/src/github.com/ethereum/go-ethereum/p2p/server.go:1002 +0x25d
==================
--- FAIL: TestDiscoveryPersistenceSimulationSimAdapter (103.99s)
    discovery_test.go:192: Simulation with 32 nodes passed in 1m33.067198724s
    testing.go:771: race detected during execution of test
FAIL
FAIL	github.com/ethereum/go-ethereum/swarm/network/simulations/discovery	216.016s

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions