@@ -235,15 +235,31 @@ func New() *LightningTerminal {
235235
236236// Run starts everything and then blocks until either the application is shut
237237// 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 {
241239 // Hook interceptor for os signals.
242240 shutdownInterceptor , err := signal .Intercept ()
243241 if err != nil {
244242 return fmt .Errorf ("could not intercept signals: %v" , err )
245243 }
246244
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+
247263 cfg , err := loadAndValidateConfig (shutdownInterceptor )
248264 if err != nil {
249265 return fmt .Errorf ("could not load config: %w" , err )
@@ -601,8 +617,8 @@ func (g *LightningTerminal) start(ctx context.Context) error {
601617
602618 return fmt .Errorf ("LND has stopped" )
603619
604- case <- interceptor . ShutdownChannel ():
605- return fmt . Errorf ( "received the shutdown signal" )
620+ case <- ctx . Done ():
621+ return ctx . Err ( )
606622 }
607623
608624 // Connect to LND.
@@ -683,8 +699,8 @@ func (g *LightningTerminal) start(ctx context.Context) error {
683699
684700 return fmt .Errorf ("LND has stopped" )
685701
686- case <- interceptor . ShutdownChannel ():
687- return fmt . Errorf ( "received the shutdown signal" )
702+ case <- ctx . Done ():
703+ return ctx . Err ( )
688704 }
689705 }
690706
@@ -758,7 +774,7 @@ func (g *LightningTerminal) start(ctx context.Context) error {
758774
759775 return fmt .Errorf ("LND is not running" )
760776
761- case <- interceptor . ShutdownChannel ():
777+ case <- ctx . Done ():
762778 log .Infof ("Shutdown signal received" )
763779 }
764780
@@ -812,8 +828,8 @@ func (g *LightningTerminal) setUpLNDClients(ctx context.Context,
812828 case <- lndQuit :
813829 return fmt .Errorf ("LND has stopped" )
814830
815- case <- interceptor . ShutdownChannel ():
816- return fmt . Errorf ( "received the shutdown signal" )
831+ case <- ctx . Done ():
832+ return ctx . Err ( )
817833
818834 case <- time .After (g .cfg .LndConnectInterval ):
819835 return nil
@@ -874,25 +890,7 @@ func (g *LightningTerminal) setUpLNDClients(ctx context.Context,
874890 // wallet being fully synced to its chain backend. The chain notifier
875891 // will always be ready first so if we instruct the lndclient to wait
876892 // 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.
896894 log .Infof ("Connecting full lnd client" )
897895 for {
898896 g .lndClient , err = lndclient .NewLndServices (
@@ -907,7 +905,7 @@ func (g *LightningTerminal) setUpLNDClients(ctx context.Context,
907905 ),
908906 BlockUntilChainSynced : true ,
909907 BlockUntilUnlocked : true ,
910- CallerCtx : ctxc ,
908+ CallerCtx : ctx ,
911909 CheckVersion : minimalCompatibleVersion ,
912910 RPCTimeout : g .cfg .LndRPCTimeout ,
913911 ChainSyncPollInterval : g .cfg .LndConnectInterval ,
0 commit comments