From e05ec619cae908973bd26be2e2d0815d395c5095 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Wed, 10 May 2017 17:37:59 -0700 Subject: [PATCH] peer: ensure no messages are sent/processed _before_ all channels loaded MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit fixes a bug which could at times cause channels to be unusable upon connection. The bug would manifest like the following: two peers would connect, one loads their channels faster than the other, this would result in the winning peer attempting to extend their revocation window. However, if the other peer hadn’t yet loaded the channel, then this would appear to them to be an unknown channel. We properly fix this issue by ensure all channels are loaded _before_ any of the goroutines needed for the operation of the peer are launched. --- peer.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/peer.go b/peer.go index b2902f02..011ba1e8 100644 --- a/peer.go +++ b/peer.go @@ -237,6 +237,8 @@ func (p *peer) Start() error { } } + // Once the init message arrives, we can parse it so we can figure out + // the negotiation of features for this session. msg := <-msgChan if msg, ok := msg.(*lnwire.Init); ok { if err := p.handleInitMsg(msg); err != nil { @@ -247,13 +249,6 @@ func (p *peer) Start() error { "must be init message") } - p.wg.Add(5) - go p.queueHandler() - go p.writeHandler() - go p.readHandler() - go p.channelManager() - go p.pingHandler() - // Fetch and then load all the active channels we have with this remote // peer from the database. activeChans, err := p.server.chanDB.FetchOpenChannels(p.addr.IdentityKey) @@ -272,6 +267,13 @@ func (p *peer) Start() error { return fmt.Errorf("unable to load channels: %v", err) } + p.wg.Add(5) + go p.queueHandler() + go p.writeHandler() + go p.readHandler() + go p.channelManager() + go p.pingHandler() + return nil }