Skip to content

Commit ec8df32

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 ec8df32

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

terminal.go

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -526,31 +526,57 @@ 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:
532536

533-
case err := <-g.errQueue.ChanOut():
534-
return err
537+
case err := <-g.errQueue.ChanOut():
538+
return err
539+
540+
case <-lndQuit:
541+
return nil
542+
543+
case <-interceptor.ShutdownChannel():
544+
return fmt.Errorf("received the shutdown signal")
545+
}
535546

536-
case <-lndQuit:
537547
return nil
548+
}
538549

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

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

550576
// Set up all the LND clients required by LiT.
551577
err = g.setUpLNDClients()
552578
if err != nil {
553-
log.Errorf("Could not set up LND clients: %w", err)
579+
log.Errorf("Could not set up LND clients: %v", err)
554580
return err
555581
}
556582

0 commit comments

Comments
 (0)