@@ -315,10 +315,20 @@ func (p *peer) Start() error {
315
315
return nil
316
316
}
317
317
318
+ // QuitSignal is a method that should return a channel which will be sent upon
319
+ // or closed once the backing peer exits. This allows callers using the
320
+ // interface to cancel any processing in the event the backing implementation
321
+ // exits.
322
+ //
323
+ // NOTE: Part of the lnpeer.Peer interface.
324
+ func (p * peer ) QuitSignal () <- chan struct {} {
325
+ return p .quit
326
+ }
327
+
318
328
// loadActiveChannels creates indexes within the peer for tracking all active
319
329
// channels returned by the database.
320
330
func (p * peer ) loadActiveChannels (chans []* channeldb.OpenChannel ) error {
321
- var activeChans []wire.OutPoint
331
+ var activePublicChans []wire.OutPoint
322
332
for _ , dbChan := range chans {
323
333
lnChan , err := lnwallet .NewLightningChannel (
324
334
p .server .cc .signer , p .server .witnessBeacon , dbChan ,
@@ -431,14 +441,19 @@ func (p *peer) loadActiveChannels(chans []*channeldb.OpenChannel) error {
431
441
p .activeChannels [chanID ] = lnChan
432
442
p .activeChanMtx .Unlock ()
433
443
434
- activeChans = append (activeChans , * chanPoint )
444
+ // Only if the channel is public do we need to collect it for
445
+ // sending out a new enable update.
446
+ chanIsPublic := dbChan .ChannelFlags & lnwire .FFAnnounceChannel != 0
447
+ if chanIsPublic {
448
+ activePublicChans = append (activePublicChans , * chanPoint )
449
+ }
435
450
}
436
451
437
- // As a final measure we launch a goroutine that will ensure the
438
- // channels are not currently disabled, as that will make us skip it
439
- // during path finding.
452
+ // As a final measure we launch a goroutine that will ensure the newly
453
+ // loaded public channels are not currently disabled, as that will make
454
+ // us skip it during path finding.
440
455
go func () {
441
- for _ , chanPoint := range activeChans {
456
+ for _ , chanPoint := range activePublicChans {
442
457
// Set the channel disabled=false by sending out a new
443
458
// ChannelUpdate. If this channel is already active,
444
459
// the update won't be sent.
@@ -738,6 +753,7 @@ func (ms *msgStream) Stop() {
738
753
func (ms * msgStream ) msgConsumer () {
739
754
defer ms .wg .Done ()
740
755
defer peerLog .Tracef (ms .stopMsg )
756
+ defer atomic .StoreInt32 (& ms .streamShutdown , 1 )
741
757
742
758
peerLog .Tracef (ms .startMsg )
743
759
@@ -752,9 +768,10 @@ func (ms *msgStream) msgConsumer() {
752
768
// Otherwise, we'll check the message queue for any new
753
769
// items.
754
770
select {
771
+ case <- ms .peer .quit :
772
+ return
755
773
case <- ms .quit :
756
774
ms .msgCond .L .Unlock ()
757
- atomic .StoreInt32 (& ms .streamShutdown , 1 )
758
775
return
759
776
default :
760
777
}
@@ -777,8 +794,9 @@ func (ms *msgStream) msgConsumer() {
777
794
// grow indefinitely.
778
795
select {
779
796
case ms .producerSema <- struct {}{}:
797
+ case <- ms .peer .quit :
798
+ return
780
799
case <- ms .quit :
781
- atomic .StoreInt32 (& ms .streamShutdown , 1 )
782
800
return
783
801
}
784
802
}
@@ -837,13 +855,29 @@ func newChanMsgStream(p *peer, cid lnwire.ChannelID) *msgStream {
837
855
// to the other side, they immediately send a
838
856
// channel update message, but we haven't yet
839
857
// sent the channel to the channelManager.
840
- p .server .fundingMgr .waitUntilChannelOpen (cid )
858
+ err := p .server .fundingMgr .waitUntilChannelOpen (
859
+ cid , p .quit ,
860
+ )
861
+ if err != nil {
862
+ // If we have a non-nil error, then the
863
+ // funding manager is shutting down, s
864
+ // we can exit here without attempting
865
+ // to deliver the message.
866
+ return
867
+ }
841
868
}
842
869
843
- // TODO(roasbeef): only wait if not chan sync
870
+ // In order to avoid unnecessarily delivering message
871
+ // as the peer is exiting, we'll check quickly to see
872
+ // if we need to exit.
873
+ select {
874
+ case <- p .quit :
875
+ return
876
+ default :
877
+ }
844
878
845
- // Dispatch the commitment update message to the proper active
846
- // goroutine dedicated to this channel.
879
+ // Dispatch the commitment update message to the proper
880
+ // active goroutine dedicated to this channel.
847
881
if chanLink == nil {
848
882
link , err := p .server .htlcSwitch .GetLink (cid )
849
883
if err != nil {
@@ -854,6 +888,15 @@ func newChanMsgStream(p *peer, cid lnwire.ChannelID) *msgStream {
854
888
chanLink = link
855
889
}
856
890
891
+ // In order to avoid unnecessarily delivering message
892
+ // as the peer is exiting, we'll check quickly to see
893
+ // if we need to exit.
894
+ select {
895
+ case <- p .quit :
896
+ return
897
+ default :
898
+ }
899
+
857
900
chanLink .HandleChannelUpdate (msg )
858
901
},
859
902
)
@@ -878,6 +921,7 @@ func newDiscMsgStream(p *peer) *msgStream {
878
921
//
879
922
// NOTE: This method MUST be run as a goroutine.
880
923
func (p * peer ) readHandler () {
924
+ defer p .wg .Done ()
881
925
882
926
// We'll stop the timer after a new messages is received, and also
883
927
// reset it after we process the next message.
@@ -1056,6 +1100,7 @@ out:
1056
1100
chanStream = newChanMsgStream (p , targetChan )
1057
1101
chanMsgStreams [targetChan ] = chanStream
1058
1102
chanStream .Start ()
1103
+ defer chanStream .Stop ()
1059
1104
}
1060
1105
1061
1106
// With the stream obtained, add the message to the
@@ -1066,16 +1111,8 @@ out:
1066
1111
idleTimer .Reset (idleTimeout )
1067
1112
}
1068
1113
1069
- p .wg .Done ()
1070
-
1071
1114
p .Disconnect (errors .New ("read handler closed" ))
1072
1115
1073
- for cid , chanStream := range chanMsgStreams {
1074
- chanStream .Stop ()
1075
-
1076
- delete (chanMsgStreams , cid )
1077
- }
1078
-
1079
1116
peerLog .Tracef ("readHandler for peer %v done" , p )
1080
1117
}
1081
1118
0 commit comments