diff --git a/peer.go b/peer.go index c8f8ef27..ade90b41 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 e8f0618b..b0bd8723 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 {