@@ -748,12 +748,12 @@ func NewBrontide(cfg Config) *Brontide {
748
748
749
749
// Start starts all helper goroutines the peer needs for normal operations. In
750
750
// the case this peer has already been started, then this function is a noop.
751
- func (p * Brontide ) Start () error {
751
+ func (p * Brontide ) Start (ctx context. Context ) error {
752
752
if atomic .AddInt32 (& p .started , 1 ) != 1 {
753
753
return nil
754
754
}
755
755
756
- ctx := context . TODO ( )
756
+ ctx , _ = p . cg . Create ( ctx )
757
757
758
758
// Once we've finished starting up the peer, we'll signal to other
759
759
// goroutines that the they can move forward to tear down the peer, or
@@ -895,7 +895,7 @@ func (p *Brontide) Start() error {
895
895
p .cg .WgAdd (4 )
896
896
go p .queueHandler ()
897
897
go p .writeHandler ()
898
- go p .channelManager ()
898
+ go p .channelManager (ctx )
899
899
go p .readHandler ()
900
900
901
901
// Signal to any external processes that the peer is now active.
@@ -2871,14 +2871,32 @@ func (p *Brontide) genDeliveryScript() ([]byte, error) {
2871
2871
// channels maintained with the remote peer.
2872
2872
//
2873
2873
// NOTE: This method MUST be run as a goroutine.
2874
- func (p * Brontide ) channelManager () {
2874
+ func (p * Brontide ) channelManager (ctx context. Context ) {
2875
2875
defer p .cg .WgDone ()
2876
2876
2877
2877
// reenableTimeout will fire once after the configured channel status
2878
2878
// interval has elapsed. This will trigger us to sign new channel
2879
2879
// updates and broadcast them with the "disabled" flag unset.
2880
2880
reenableTimeout := time .After (p .cfg .ChanActiveTimeout )
2881
2881
2882
+ // resetChannelState is a helper that resets all active channels to
2883
+ // their default state. This should be used if we are exiting.
2884
+ resetChannelState := func () {
2885
+ p .activeChannels .ForEach (func (_ lnwire.ChannelID ,
2886
+ lc * lnwallet.LightningChannel ) error {
2887
+
2888
+ // Exit if the channel is nil as it's a pending
2889
+ // channel.
2890
+ if lc == nil {
2891
+ return nil
2892
+ }
2893
+
2894
+ lc .ResetState ()
2895
+
2896
+ return nil
2897
+ })
2898
+ }
2899
+
2882
2900
out:
2883
2901
for {
2884
2902
select {
@@ -2894,7 +2912,7 @@ out:
2894
2912
// funding workflow. We'll initialize the necessary local
2895
2913
// state, and notify the htlc switch of a new link.
2896
2914
case req := <- p .newActiveChannel :
2897
- p .handleNewActiveChannel (req )
2915
+ p .handleNewActiveChannel (ctx , req )
2898
2916
2899
2917
// The funding flow for a pending channel is failed, we will
2900
2918
// remove it from Brontide.
@@ -2949,21 +2967,16 @@ out:
2949
2967
}
2950
2968
2951
2969
case <- p .cg .Done ():
2952
- // As, we've been signalled to exit, we'll reset all
2953
- // our active channel back to their default state.
2954
- p .activeChannels .ForEach (func (_ lnwire.ChannelID ,
2955
- lc * lnwallet.LightningChannel ) error {
2956
-
2957
- // Exit if the channel is nil as it's a pending
2958
- // channel.
2959
- if lc == nil {
2960
- return nil
2961
- }
2970
+ // As, we've been signalled to exit, we'll reset all our
2971
+ // active channel back to their default state.
2972
+ resetChannelState ()
2962
2973
2963
- lc . ResetState ()
2974
+ break out
2964
2975
2965
- return nil
2966
- })
2976
+ case <- ctx .Done ():
2977
+ // As, we've been signalled to exit, we'll reset all our
2978
+ // active channel back to their default state.
2979
+ resetChannelState ()
2967
2980
2968
2981
break out
2969
2982
}
@@ -5185,8 +5198,8 @@ func (p *Brontide) addActiveChannel(ctx context.Context,
5185
5198
// handleNewActiveChannel handles a `newChannelMsg` request. Depending on we
5186
5199
// know this channel ID or not, we'll either add it to the `activeChannels` map
5187
5200
// or init the next revocation for it.
5188
- func (p * Brontide ) handleNewActiveChannel (req * newChannelMsg ) {
5189
- ctx := context . TODO ()
5201
+ func (p * Brontide ) handleNewActiveChannel (ctx context. Context ,
5202
+ req * newChannelMsg ) {
5190
5203
5191
5204
newChan := req .channel
5192
5205
chanPoint := newChan .FundingOutpoint
0 commit comments