From b262d27597cfe614983399b25f0911b5eb2ce1c6 Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 12 Feb 2019 17:13:21 +0400 Subject: [PATCH] swarm/pss: mutex lifecycle fixed --- swarm/pss/protocol.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/swarm/pss/protocol.go b/swarm/pss/protocol.go index 5fcae090efb9..5f47ee47d1ec 100644 --- a/swarm/pss/protocol.go +++ b/swarm/pss/protocol.go @@ -228,6 +228,7 @@ func ToP2pMsg(msg []byte) (p2p.Msg, error) { // to link the peer to. // The key must exist in the pss store prior to adding the peer. func (p *Protocol) AddPeer(peer *p2p.Peer, topic Topic, asymmetric bool, key string) (p2p.MsgReadWriter, error) { + var ok bool rw := &PssReadWriter{ Pss: p.Pss, rw: make(chan p2p.Msg), @@ -242,19 +243,21 @@ func (p *Protocol) AddPeer(peer *p2p.Peer, topic Topic, asymmetric bool, key str } if asymmetric { p.Pss.pubKeyPoolMu.Lock() - if _, ok := p.Pss.pubKeyPool[key]; !ok { + _, ok = p.Pss.pubKeyPool[key] + p.Pss.pubKeyPoolMu.Unlock() + if !ok { return nil, fmt.Errorf("asym key does not exist: %s", key) } - p.Pss.pubKeyPoolMu.Unlock() p.RWPoolMu.Lock() p.pubKeyRWPool[key] = rw p.RWPoolMu.Unlock() } else { p.Pss.symKeyPoolMu.Lock() - if _, ok := p.Pss.symKeyPool[key]; !ok { + _, ok = p.Pss.symKeyPool[key] + p.Pss.symKeyPoolMu.Unlock() + if !ok { return nil, fmt.Errorf("symkey does not exist: %s", key) } - p.Pss.symKeyPoolMu.Unlock() p.RWPoolMu.Lock() p.symKeyRWPool[key] = rw p.RWPoolMu.Unlock()