Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

p2p/simulations: fix data race on swarm/network/simulations #18464

Merged
merged 3 commits into from
Jan 22, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions p2p/simulations/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ func (net *Network) Start(id enode.ID) error {
// snapshots
func (net *Network) startWithSnapshots(id enode.ID, snapshots map[string][]byte) error {
net.lock.Lock()
defer net.lock.Unlock()

node := net.getNode(id)
if node == nil {
frncmx marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -184,8 +183,10 @@ func (net *Network) startWithSnapshots(id enode.ID, snapshots map[string][]byte)
}
node.Up = true
log.Info("Started node", "id", id)
ev := NewEvent(node)
net.lock.Unlock()

net.events.Send(NewEvent(node))
net.events.Send(ev)

// subscribe to peer events
client, err := node.Client()
Expand All @@ -209,13 +210,14 @@ func (net *Network) watchPeerEvents(id enode.ID, events chan *p2p.PeerEvent, sub

// assume the node is now down
net.lock.Lock()
defer net.lock.Unlock()
node := net.getNode(id)
if node == nil {
frncmx marked this conversation as resolved.
Show resolved Hide resolved
return
}
node.Up = false
net.events.Send(NewEvent(node))
ev := NewEvent(node)
net.lock.Unlock()
net.events.Send(ev)
}()
for {
select {
Expand Down Expand Up @@ -270,7 +272,10 @@ func (net *Network) Stop(id enode.ID) error {
return err
}
log.Info("Stopped node", "id", id, "err", err)
net.events.Send(ControlEvent(node))
net.lock.Lock()
ev := ControlEvent(node)
net.lock.Unlock()
net.events.Send(ev)
return nil
}

Expand Down