From cf5dc90d0441684e87d09c0928245aa69e80fc2d Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Tue, 1 Oct 2019 17:35:23 -0400 Subject: [PATCH] peer+server: wait until peer is active to begin channel opening process Without waiting, we would proceed to retrieve the remote peer's supported features, which may have not been set due to not yet receiving their Init message. --- peer.go | 9 +++++++++ server.go | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/peer.go b/peer.go index c8f8ef27c..ade90b41b 100644 --- a/peer.go +++ b/peer.go @@ -121,6 +121,10 @@ type peer struct { addr *lnwire.NetAddress pubKeyBytes [33]byte + // activeSignal when closed signals that the peer is now active and + // ready to process messages. + activeSignal chan struct{} + // startTime is the time this peer connection was successfully // established. It will be zero for peers that did not successfully // Start(). @@ -241,6 +245,8 @@ func newPeer(conn net.Conn, connReq *connmgr.ConnReq, server *server, conn: conn, addr: addr, + activeSignal: make(chan struct{}), + inbound: inbound, connReq: connReq, @@ -369,6 +375,9 @@ func (p *peer) Start() error { go p.channelManager() go p.pingHandler() + // Signal to any external processes that the peer is now active. + close(p.activeSignal) + // Now that the peer has started up, we send any channel sync messages // that must be resent for borked channels. if len(msgs) > 0 { diff --git a/server.go b/server.go index e8f0618bc..b0bd87234 100644 --- a/server.go +++ b/server.go @@ -3207,6 +3207,18 @@ func (s *server) OpenChannel( } s.mu.RUnlock() + // We'll wait until the peer is active before beginning the channel + // opening process. + select { + case <-peer.activeSignal: + case <-peer.quit: + req.err <- fmt.Errorf("peer %x disconnected", pubKeyBytes) + return req.updates, req.err + case <-s.quit: + req.err <- ErrServerShuttingDown + return req.updates, req.err + } + // If the fee rate wasn't specified, then we'll use a default // confirmation target. if req.fundingFeePerKw == 0 {