@@ -235,15 +235,31 @@ func New() *LightningTerminal {
235
235
236
236
// Run starts everything and then blocks until either the application is shut
237
237
// down or a critical error happens.
238
- func (g * LightningTerminal ) Run () error {
239
- ctx := context .TODO ()
240
-
238
+ func (g * LightningTerminal ) Run (ctx context.Context ) error {
241
239
// Hook interceptor for os signals.
242
240
shutdownInterceptor , err := signal .Intercept ()
243
241
if err != nil {
244
242
return fmt .Errorf ("could not intercept signals: %v" , err )
245
243
}
246
244
245
+ ctx , cancel := context .WithCancel (ctx )
246
+ defer cancel ()
247
+
248
+ // Make sure the context is canceled if the user requests shutdown and
249
+ // that the shutdown signal is requested if the context is canceled.
250
+ go func () {
251
+ select {
252
+ // Client requests shutdown, cancel the wait.
253
+ case <- shutdownInterceptor .ShutdownChannel ():
254
+ cancel ()
255
+
256
+ // The check was completed and the above defer canceled the
257
+ // context. We can just exit the goroutine, nothing more to do.
258
+ case <- ctx .Done ():
259
+ shutdownInterceptor .RequestShutdown ()
260
+ }
261
+ }()
262
+
247
263
cfg , err := loadAndValidateConfig (shutdownInterceptor )
248
264
if err != nil {
249
265
return fmt .Errorf ("could not load config: %w" , err )
@@ -601,8 +617,8 @@ func (g *LightningTerminal) start(ctx context.Context) error {
601
617
602
618
return fmt .Errorf ("LND has stopped" )
603
619
604
- case <- interceptor . ShutdownChannel ():
605
- return fmt . Errorf ( "received the shutdown signal" )
620
+ case <- ctx . Done ():
621
+ return ctx . Err ( )
606
622
}
607
623
608
624
// Connect to LND.
@@ -683,8 +699,8 @@ func (g *LightningTerminal) start(ctx context.Context) error {
683
699
684
700
return fmt .Errorf ("LND has stopped" )
685
701
686
- case <- interceptor . ShutdownChannel ():
687
- return fmt . Errorf ( "received the shutdown signal" )
702
+ case <- ctx . Done ():
703
+ return ctx . Err ( )
688
704
}
689
705
}
690
706
@@ -758,7 +774,7 @@ func (g *LightningTerminal) start(ctx context.Context) error {
758
774
759
775
return fmt .Errorf ("LND is not running" )
760
776
761
- case <- interceptor . ShutdownChannel ():
777
+ case <- ctx . Done ():
762
778
log .Infof ("Shutdown signal received" )
763
779
}
764
780
@@ -812,8 +828,8 @@ func (g *LightningTerminal) setUpLNDClients(ctx context.Context,
812
828
case <- lndQuit :
813
829
return fmt .Errorf ("LND has stopped" )
814
830
815
- case <- interceptor . ShutdownChannel ():
816
- return fmt . Errorf ( "received the shutdown signal" )
831
+ case <- ctx . Done ():
832
+ return ctx . Err ( )
817
833
818
834
case <- time .After (g .cfg .LndConnectInterval ):
819
835
return nil
@@ -874,25 +890,7 @@ func (g *LightningTerminal) setUpLNDClients(ctx context.Context,
874
890
// wallet being fully synced to its chain backend. The chain notifier
875
891
// will always be ready first so if we instruct the lndclient to wait
876
892
// for the wallet sync, we should be fully ready to start all our
877
- // subservers. This will just block until lnd signals readiness. But we
878
- // still want to react to shutdown requests, so we need to listen for
879
- // those.
880
- ctxc , cancel := context .WithCancel (ctx )
881
- defer cancel ()
882
-
883
- // Make sure the context is canceled if the user requests shutdown.
884
- go func () {
885
- select {
886
- // Client requests shutdown, cancel the wait.
887
- case <- interceptor .ShutdownChannel ():
888
- cancel ()
889
-
890
- // The check was completed and the above defer canceled the
891
- // context. We can just exit the goroutine, nothing more to do.
892
- case <- ctxc .Done ():
893
- }
894
- }()
895
-
893
+ // subservers. This will just block until lnd signals readiness.
896
894
log .Infof ("Connecting full lnd client" )
897
895
for {
898
896
g .lndClient , err = lndclient .NewLndServices (
@@ -907,7 +905,7 @@ func (g *LightningTerminal) setUpLNDClients(ctx context.Context,
907
905
),
908
906
BlockUntilChainSynced : true ,
909
907
BlockUntilUnlocked : true ,
910
- CallerCtx : ctxc ,
908
+ CallerCtx : ctx ,
911
909
CheckVersion : minimalCompatibleVersion ,
912
910
RPCTimeout : g .cfg .LndRPCTimeout ,
913
911
ChainSyncPollInterval : g .cfg .LndConnectInterval ,
0 commit comments