From 32b2a5885f062845e32761b9542e748d1d86e149 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Tigerstr=C3=B6m?= Date: Fri, 15 Sep 2023 14:14:19 +0200 Subject: [PATCH] terminal: don't stop litd on account system error --- accounts/service.go | 36 +++++++++++------------------------- accounts/service_test.go | 9 +++++---- terminal.go | 15 ++++++++++++--- 3 files changed, 28 insertions(+), 32 deletions(-) diff --git a/accounts/service.go b/accounts/service.go index c4f0ab5e9..bdf671730 100644 --- a/accounts/service.go +++ b/accounts/service.go @@ -60,16 +60,18 @@ type InterceptorService struct { invoiceToAccount map[lntypes.Hash]AccountID pendingPayments map[lntypes.Hash]*trackedPayment - mainErrChan chan<- error - wg sync.WaitGroup - quit chan struct{} + mainErrCallback func(error) + wg sync.WaitGroup + quit chan struct{} isEnabled bool } // NewService returns a service backed by the macaroon Bolt DB stored in the // passed-in directory. -func NewService(dir string, errChan chan<- error) (*InterceptorService, error) { +func NewService(dir string, + errCallback func(error)) (*InterceptorService, error) { + accountStore, err := NewBoltStore(dir, DBFilename) if err != nil { return nil, err @@ -83,7 +85,7 @@ func NewService(dir string, errChan chan<- error) (*InterceptorService, error) { contextCancel: contextCancel, invoiceToAccount: make(map[lntypes.Hash]AccountID), pendingPayments: make(map[lntypes.Hash]*trackedPayment), - mainErrChan: errChan, + mainErrCallback: errCallback, quit: make(chan struct{}), isEnabled: false, }, nil @@ -184,11 +186,7 @@ func (s *InterceptorService) Start(lightningClient lndclient.LightningClient, log.Errorf("Error processing invoice "+ "update: %v", err) - select { - case s.mainErrChan <- err: - case <-s.mainCtx.Done(): - case <-s.quit: - } + s.mainErrCallback(err) return } @@ -199,11 +197,7 @@ func (s *InterceptorService) Start(lightningClient lndclient.LightningClient, err = s.disableAndErrorf("Error in invoice "+ "subscription: %w", err) - select { - case s.mainErrChan <- err: - case <-s.mainCtx.Done(): - case <-s.quit: - } + s.mainErrCallback(err) return case <-s.mainCtx.Done(): @@ -581,11 +575,7 @@ func (s *InterceptorService) TrackPayment(id AccountID, hash lntypes.Hash, hash, paymentUpdate, ) if err != nil { - select { - case s.mainErrChan <- err: - case <-s.mainCtx.Done(): - case <-s.quit: - } + s.mainErrCallback(err) return } @@ -614,11 +604,7 @@ func (s *InterceptorService) TrackPayment(id AccountID, hash lntypes.Hash, "error from TrackPayment RPC "+ "for payment %v: %w", hash, err) - select { - case s.mainErrChan <- err: - case <-s.mainCtx.Done(): - case <-s.quit: - } + s.mainErrCallback(err) } return diff --git a/accounts/service_test.go b/accounts/service_test.go index 7098e72b3..f1cf120d8 100644 --- a/accounts/service_test.go +++ b/accounts/service_test.go @@ -287,7 +287,7 @@ func TestAccountService(t *testing.T) { err := s.store.UpdateAccount(acct) require.NoError(t, err) - lnd.mainErrChan <- testErr + s.mainErrCallback(testErr) }, validate: func(t *testing.T, lnd *mockLnd, s *InterceptorService) { @@ -672,9 +672,10 @@ func TestAccountService(t *testing.T) { tt.Parallel() lndMock := newMockLnd() - service, err := NewService( - t.TempDir(), lndMock.mainErrChan, - ) + errFunc := func(err error) { + lndMock.mainErrChan <- err + } + service, err := NewService(t.TempDir(), errFunc) require.NoError(t, err) // Is a setup call required to initialize initial diff --git a/terminal.go b/terminal.go index a4ac4afdf..c141e1d6e 100644 --- a/terminal.go +++ b/terminal.go @@ -305,8 +305,14 @@ func (g *LightningTerminal) Run() error { func (g *LightningTerminal) start() error { var err error + accountServiceErrCallback := func(err error) { + log.Errorf("Error thrown in the accounts service, keeping "+ + "litd running: %v", err, + ) + } + g.accountService, err = accounts.NewService( - filepath.Dir(g.cfg.MacaroonPath), g.errQueue.ChanIn(), + filepath.Dir(g.cfg.MacaroonPath), accountServiceErrCallback, ) if err != nil { return fmt.Errorf("error creating account service: %v", err) @@ -843,9 +849,12 @@ func (g *LightningTerminal) startInternalSubServers( g.lndClient.ChainParams, ) if err != nil { - return fmt.Errorf("error starting account service: %v", - err) + log.Errorf("error starting account service: %v, disabling "+ + "account service", err) } + // Even if we error on accountService.Start, we still want to mark the + // service as started so that we can properly shut it down in the + // shutdownSubServers call. g.accountServiceStarted = true requestLogger, err := firewall.NewRequestLogger(