Skip to content

Commit

Permalink
terminal: don't stop litd on account system error
Browse files Browse the repository at this point in the history
  • Loading branch information
ViktorTigerstrom committed Sep 28, 2023
1 parent 4696c47 commit 32b2a58
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
36 changes: 11 additions & 25 deletions accounts/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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():
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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

Expand Down
9 changes: 5 additions & 4 deletions accounts/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 32b2a58

Please sign in to comment.