Skip to content

Commit dd9711e

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 edab9a4 commit dd9711e

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
@@ -536,31 +536,56 @@ func (g *LightningTerminal) start() error {
536536
return fmt.Errorf("error displaying startup info: %v", err)
537537
}
538538

539-
// Wait for lnd to be unlocked, then start all clients.
540-
select {
541-
case <-readyChan:
539+
// waitForSignal is a helper closure that can be used to wait on the
540+
// given channel for a signal while also being responsive to an error
541+
// from the error Queue, LND quiting or the interceptor receiving a
542+
// shutdown signal.
543+
waitForSignal := func(c chan struct{}) error {
544+
select {
545+
case <-c:
546+
return nil
542547

543-
case err := <-g.errQueue.ChanOut():
544-
return err
548+
case err := <-g.errQueue.ChanOut():
549+
return err
545550

546-
case <-lndQuit:
547-
return nil
551+
case <-lndQuit:
552+
return nil
548553

549-
case <-interceptor.ShutdownChannel():
550-
return fmt.Errorf("received the shutdown signal")
554+
case <-interceptor.ShutdownChannel():
555+
return fmt.Errorf("received the shutdown signal")
556+
}
557+
}
558+
559+
// Wait for lnd to be unlocked, then start all clients.
560+
if err = waitForSignal(readyChan); err != nil {
561+
return err
551562
}
552563

553564
// If we're in integrated mode, we'll need to wait for lnd to send the
554565
// macaroon after unlock before going any further.
555566
if g.cfg.LndMode == ModeIntegrated {
556-
<-bufReadyChan
557-
g.cfg.lndAdminMacaroon = <-macChan
567+
if err = waitForSignal(bufReadyChan); err != nil {
568+
return err
569+
}
570+
571+
// Create a new macReady channel that will serve to signal that
572+
// the LND macaroon is ready. Spin off a goroutine that will
573+
// close this channel when the macaroon has been received.
574+
macReady := make(chan struct{})
575+
go func() {
576+
g.cfg.lndAdminMacaroon = <-macChan
577+
close(macReady)
578+
}()
579+
580+
if err = waitForSignal(macReady); err != nil {
581+
return err
582+
}
558583
}
559584

560585
// Set up all the LND clients required by LiT.
561586
err = g.setUpLNDClients()
562587
if err != nil {
563-
log.Errorf("Could not set up LND clients: %w", err)
588+
log.Errorf("Could not set up LND clients: %v", err)
564589
return err
565590
}
566591

0 commit comments

Comments
 (0)