diff options
author | Elad <theman@elad.im> | 2019-01-23 03:11:43 +0800 |
---|---|---|
committer | Rafael Matias <rafael@skyle.net> | 2019-02-19 19:56:30 +0800 |
commit | 475a0664c5d58e7f50d5253915dd7ce5707b6e70 (patch) | |
tree | ac440aacf93ec793e100e2916cae3024b58eb39c | |
parent | 4625b1257f614646cdac49fcc0a76f2a1d58fb5c (diff) | |
download | dexon-475a0664c5d58e7f50d5253915dd7ce5707b6e70.tar dexon-475a0664c5d58e7f50d5253915dd7ce5707b6e70.tar.gz dexon-475a0664c5d58e7f50d5253915dd7ce5707b6e70.tar.bz2 dexon-475a0664c5d58e7f50d5253915dd7ce5707b6e70.tar.lz dexon-475a0664c5d58e7f50d5253915dd7ce5707b6e70.tar.xz dexon-475a0664c5d58e7f50d5253915dd7ce5707b6e70.tar.zst dexon-475a0664c5d58e7f50d5253915dd7ce5707b6e70.zip |
p2p/simulations: fix data race on swarm/network/simulations (#18464)
(cherry picked from commit 85a79b3ad3c5863f8612d25c246bcfad339f36b7)
-rw-r--r-- | p2p/simulations/network.go | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/p2p/simulations/network.go b/p2p/simulations/network.go index 86f7dc9be..6edcefc53 100644 --- a/p2p/simulations/network.go +++ b/p2p/simulations/network.go @@ -168,24 +168,28 @@ 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 { + net.lock.Unlock() return fmt.Errorf("node %v does not exist", id) } if node.Up { + net.lock.Unlock() return fmt.Errorf("node %v already up", id) } log.Trace("Starting node", "id", id, "adapter", net.nodeAdapter.Name()) if err := node.Start(snapshots); err != nil { + net.lock.Unlock() log.Warn("Node startup failed", "id", id, "err", err) return err } 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() @@ -210,12 +214,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 { return } node.Up = false - net.events.Send(NewEvent(node)) + ev := NewEvent(node) + net.events.Send(ev) }() for { select { @@ -254,9 +260,11 @@ func (net *Network) Stop(id enode.ID) error { net.lock.Lock() node := net.getNode(id) if node == nil { + net.lock.Unlock() return fmt.Errorf("node %v does not exist", id) } if !node.Up { + net.lock.Unlock() return fmt.Errorf("node %v already down", id) } node.Up = false @@ -270,7 +278,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 } |