From 406e74e2afdce374fd227873f4eb96a0ba99bd23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Wed, 22 Apr 2015 17:40:39 +0300 Subject: [PATCH] whisper: fix a small data race duirng peer connection --- whisper/peer.go | 8 ++------ whisper/whisper.go | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/whisper/peer.go b/whisper/peer.go index 28abf4260..77e09bece 100644 --- a/whisper/peer.go +++ b/whisper/peer.go @@ -23,18 +23,14 @@ type peer struct { // newPeer creates and initializes a new whisper peer connection, returning either // the newly constructed link or a failure reason. -func newPeer(host *Whisper, remote *p2p.Peer, rw p2p.MsgReadWriter) (*peer, error) { - p := &peer{ +func newPeer(host *Whisper, remote *p2p.Peer, rw p2p.MsgReadWriter) *peer { + return &peer{ host: host, peer: remote, ws: rw, known: set.New(), quit: make(chan struct{}), } - if err := p.handshake(); err != nil { - return nil, err - } - return p, nil } // start initiates the peer updater, periodically broadcasting the whisper packets diff --git a/whisper/whisper.go b/whisper/whisper.go index 5d6ee6e3b..a48e1e380 100644 --- a/whisper/whisper.go +++ b/whisper/whisper.go @@ -168,15 +168,9 @@ func (self *Whisper) Messages(id int) []*Message { // handlePeer is called by the underlying P2P layer when the whisper sub-protocol // connection is negotiated. func (self *Whisper) handlePeer(peer *p2p.Peer, rw p2p.MsgReadWriter) error { - // Create, initialize and start the whisper peer - whisperPeer, err := newPeer(self, peer, rw) - if err != nil { - return err - } - whisperPeer.start() - defer whisperPeer.stop() + // Create the new peer and start tracking it + whisperPeer := newPeer(self, peer, rw) - // Start tracking the active peer self.peerMu.Lock() self.peers[whisperPeer] = struct{}{} self.peerMu.Unlock() @@ -186,6 +180,14 @@ func (self *Whisper) handlePeer(peer *p2p.Peer, rw p2p.MsgReadWriter) error { delete(self.peers, whisperPeer) self.peerMu.Unlock() }() + + // Run the peer handshake and state updates + if err := whisperPeer.handshake(); err != nil { + return err + } + whisperPeer.start() + defer whisperPeer.stop() + // Read and process inbound messages directly to merge into client-global state for { // Fetch the next packet and decode the contained envelopes -- GitLab