Skip to content

Commit 747e299

Browse files
committed
terminal: don't stop litd on account system error
1 parent e9475e3 commit 747e299

File tree

3 files changed

+40
-42
lines changed

3 files changed

+40
-42
lines changed

accounts/service.go

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,18 @@ type InterceptorService struct {
6060
invoiceToAccount map[lntypes.Hash]AccountID
6161
pendingPayments map[lntypes.Hash]*trackedPayment
6262

63-
mainErrChan chan<- error
64-
wg sync.WaitGroup
65-
quit chan struct{}
63+
mainErrCallback func(error)
64+
wg sync.WaitGroup
65+
quit chan struct{}
6666

6767
isEnabled bool
6868
}
6969

7070
// NewService returns a service backed by the macaroon Bolt DB stored in the
7171
// passed-in directory.
72-
func NewService(dir string, errChan chan<- error) (*InterceptorService, error) {
72+
func NewService(dir string,
73+
errCallback func(error)) (*InterceptorService, error) {
74+
7375
accountStore, err := NewBoltStore(dir, DBFilename)
7476
if err != nil {
7577
return nil, err
@@ -83,7 +85,7 @@ func NewService(dir string, errChan chan<- error) (*InterceptorService, error) {
8385
contextCancel: contextCancel,
8486
invoiceToAccount: make(map[lntypes.Hash]AccountID),
8587
pendingPayments: make(map[lntypes.Hash]*trackedPayment),
86-
mainErrChan: errChan,
88+
mainErrCallback: errCallback,
8789
quit: make(chan struct{}),
8890
isEnabled: false,
8991
}, nil
@@ -185,28 +187,18 @@ func (s *InterceptorService) Start(lightningClient lndclient.LightningClient,
185187
log.Errorf("Error processing invoice "+
186188
"update: %v", err)
187189

188-
select {
189-
case s.mainErrChan <- err:
190-
case <-s.mainCtx.Done():
191-
case <-s.quit:
192-
}
190+
s.mainErrCallback(err)
193191
return
194192
}
195193

196194
case err := <-invoiceErrChan:
197-
log.Errorf("Error in invoice subscription: %v",
198-
err)
199-
200195
// if the invoice subscription errors out, we
201196
// stop the service as we won't be able to
202197
// process invoices.
203-
s.disable()
198+
err = s.disableAndErrorf("Error in invoice "+
199+
"subscription: %v", err)
204200

205-
select {
206-
case s.mainErrChan <- err:
207-
case <-s.mainCtx.Done():
208-
case <-s.quit:
209-
}
201+
s.mainErrCallback(err)
210202
return
211203

212204
case <-s.mainCtx.Done():
@@ -587,11 +579,7 @@ func (s *InterceptorService) TrackPayment(id AccountID, hash lntypes.Hash,
587579
hash, paymentUpdate,
588580
)
589581
if err != nil {
590-
select {
591-
case s.mainErrChan <- err:
592-
case <-s.mainCtx.Done():
593-
case <-s.quit:
594-
}
582+
s.mainErrCallback(err)
595583
return
596584
}
597585

@@ -613,18 +601,13 @@ func (s *InterceptorService) TrackPayment(id AccountID, hash lntypes.Hash,
613601
return
614602
}
615603

616-
log.Errorf("Received error from TrackPayment "+
617-
"RPC for payment %v: %v", hash, err)
618-
619604
if err != nil {
620605
// If we error when tracking the
621606
// payment, we stop the service.
622-
s.disable()
623-
select {
624-
case s.mainErrChan <- err:
625-
case <-s.mainCtx.Done():
626-
case <-s.quit:
627-
}
607+
err = s.disableAndErrorf("Received "+
608+
"error from TrackPayment RPC "+
609+
"for payment %v: %v", hash, err)
610+
s.mainErrCallback(err)
628611
}
629612
return
630613

accounts/service_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ func (m *mockLnd) assertNoMainErr(t *testing.T) {
6565
}
6666
}
6767

68+
// assertMainErr asserts that the main error is set to the expected error, and
69+
// then resets the main error to nil.
6870
func (m *mockLnd) assertMainErr(t *testing.T, expectedErr error) {
6971
select {
7072
case err := <-m.mainErrChan:
@@ -75,6 +77,8 @@ func (m *mockLnd) assertMainErr(t *testing.T, expectedErr error) {
7577
}
7678
}
7779

80+
// assertMainErr asserts that the main error contains the expected error string,
81+
// and then resets the main error to nil.
7882
func (m *mockLnd) assertMainErrContains(t *testing.T, expectedStr string) {
7983
select {
8084
case err := <-m.mainErrChan:
@@ -285,7 +289,7 @@ func TestAccountService(t *testing.T) {
285289
err := s.store.UpdateAccount(acct)
286290
require.NoError(t, err)
287291

288-
lnd.mainErrChan <- testErr
292+
s.mainErrCallback(testErr)
289293
},
290294
validate: func(t *testing.T, lnd *mockLnd,
291295
s *InterceptorService) {
@@ -670,9 +674,10 @@ func TestAccountService(t *testing.T) {
670674
tt.Parallel()
671675

672676
lndMock := newMockLnd()
673-
service, err := NewService(
674-
t.TempDir(), lndMock.mainErrChan,
675-
)
677+
errFunc := func(err error) {
678+
lndMock.mainErrChan <- err
679+
}
680+
service, err := NewService(t.TempDir(), errFunc)
676681
require.NoError(t, err)
677682

678683
// Is a setup call required to initialize initial

terminal.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,9 @@ type LightningTerminal struct {
181181
middleware *mid.Manager
182182
middlewareStarted bool
183183

184-
accountService *accounts.InterceptorService
185-
accountServiceStarted bool
184+
accountService *accounts.InterceptorService
185+
accountServiceStarted bool
186+
accountServiceErrCallback func(error)
186187

187188
accountRpcServer *accounts.RPCServer
188189

@@ -305,8 +306,14 @@ func (g *LightningTerminal) Run() error {
305306
func (g *LightningTerminal) start() error {
306307
var err error
307308

309+
accountServiceErrCallback := func(err error) {
310+
log.Errorf("Error thrown in the accounts service, keeping "+
311+
"litd running: %v", err,
312+
)
313+
}
314+
308315
g.accountService, err = accounts.NewService(
309-
filepath.Dir(g.cfg.MacaroonPath), g.errQueue.ChanIn(),
316+
filepath.Dir(g.cfg.MacaroonPath), accountServiceErrCallback,
310317
)
311318
if err != nil {
312319
return fmt.Errorf("error creating account service: %v", err)
@@ -843,9 +850,12 @@ func (g *LightningTerminal) startInternalSubServers(
843850
g.lndClient.ChainParams,
844851
)
845852
if err != nil {
846-
return fmt.Errorf("error starting account service: %v",
847-
err)
853+
log.Errorf("error starting account service: %v, disabling "+
854+
"account service", err)
848855
}
856+
// Even if we error on accountService.Start, we still want to mark the
857+
// service as started so that we can properly shut it down in the
858+
// shutdownSubServers call.
849859
g.accountServiceStarted = true
850860

851861
requestLogger, err := firewall.NewRequestLogger(

0 commit comments

Comments
 (0)