Skip to content

Commit cba7293

Browse files
committed
terminal: dont block indefinitely on macaroon channel
In this commit, we ensure that when we wait on the macaroon channel for the macaroon from the integrated LND, that we also listen to other exit signals such as the interceptor, lndQuit chanel and the error channel. This is to prevent shutdown from blocking forever in the case where there was an issue preventing LND from sending the initial macaroon.
1 parent 03f4bf9 commit cba7293

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

terminal.go

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -526,31 +526,56 @@ func (g *LightningTerminal) start() error {
526526
return fmt.Errorf("error displaying startup info: %v", err)
527527
}
528528

529-
// Wait for lnd to be unlocked, then start all clients.
530-
select {
531-
case <-readyChan:
529+
// waitForSignal is a helper closure that can be used to wait on the
530+
// given channel for a signal while also being responsive to an error
531+
// from the error Queue, LND quiting or the interceptor receiving a
532+
// shutdown signal.
533+
waitForSignal := func(c chan struct{}) error {
534+
select {
535+
case <-c:
536+
return nil
532537

533-
case err := <-g.errQueue.ChanOut():
534-
return err
538+
case err := <-g.errQueue.ChanOut():
539+
return err
535540

536-
case <-lndQuit:
537-
return nil
541+
case <-lndQuit:
542+
return nil
538543

539-
case <-interceptor.ShutdownChannel():
540-
return fmt.Errorf("received the shutdown signal")
544+
case <-interceptor.ShutdownChannel():
545+
return fmt.Errorf("received the shutdown signal")
546+
}
547+
}
548+
549+
// Wait for lnd to be unlocked, then start all clients.
550+
if err = waitForSignal(readyChan); err != nil {
551+
return err
541552
}
542553

543554
// If we're in integrated mode, we'll need to wait for lnd to send the
544555
// macaroon after unlock before going any further.
545556
if g.cfg.LndMode == ModeIntegrated {
546-
<-bufReadyChan
547-
g.cfg.lndAdminMacaroon = <-macChan
557+
if err = waitForSignal(bufReadyChan); err != nil {
558+
return err
559+
}
560+
561+
// Create a new macReady channel that will serve to signal that
562+
// the LND macaroon is ready. Spin off a goroutine that will
563+
// close this channel when the macaroon has been received.
564+
macReady := make(chan struct{})
565+
go func() {
566+
g.cfg.lndAdminMacaroon = <-macChan
567+
close(macReady)
568+
}()
569+
570+
if err = waitForSignal(macReady); err != nil {
571+
return err
572+
}
548573
}
549574

550575
// Set up all the LND clients required by LiT.
551576
err = g.setUpLNDClients()
552577
if err != nil {
553-
log.Errorf("Could not set up LND clients: %w", err)
578+
log.Errorf("Could not set up LND clients: %v", err)
554579
return err
555580
}
556581

0 commit comments

Comments
 (0)