Skip to content

Commit 0e72c2b

Browse files
committed
loopin: handle EOF case for SubscribeSingleInvoice
From lnd 0.13.0, the SubscribeSingleInvoice rpc will return an EOF once it has served a final state to the stream. This is handled in our lndclient wrapper by closing the channels that we send updates/ errors on. When we are exclusively consuming updates from these streams, we don't need to handle this case because we will receive our final update and exit. However, in the case where we continue to listen on the update channels after consuming the final update, we need to handle this EOF/closed channels case. This is done by setting the channels to nil after they're closed so that we no longer select on them but can continue waiting for our other cases to complete. We have similar handling in loopout's waitForHtlcSpendConfirmed.
1 parent 7dca93f commit 0e72c2b

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

loopin.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -779,12 +779,30 @@ func (s *loopInSwap) waitForSwapComplete(ctx context.Context,
779779
htlcSpend = true
780780

781781
// Swap invoice ntfn error.
782-
case err := <-swapInvoiceErr:
782+
case err, ok := <-swapInvoiceErr:
783+
// If the channel has been closed, the server has
784+
// finished sending updates, so we set the channel to
785+
// nil because we don't want to constantly select this
786+
// case.
787+
if !ok {
788+
swapInvoiceErr = nil
789+
continue
790+
}
791+
783792
return err
784793

785794
// An update to the swap invoice occurred. Check the new state
786795
// and update the swap state accordingly.
787-
case update := <-swapInvoiceChan:
796+
case update, ok := <-swapInvoiceChan:
797+
// If the channel has been closed, the server has
798+
// finished sending updates, so we set the channel to
799+
// nil because we don't want to constantly select this
800+
// case.
801+
if !ok {
802+
swapInvoiceChan = nil
803+
continue
804+
}
805+
788806
s.log.Infof("Received swap invoice update: %v",
789807
update.State)
790808

loopin_testcontext_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,13 @@ func (c *loopInTestContext) updateInvoiceState(amount btcutil.Amount,
8484
AmtPaid: amount,
8585
State: state,
8686
}
87+
88+
// If we're in a final state, close our update channels as lndclient
89+
// would.
90+
if state == channeldb.ContractCanceled ||
91+
state == channeldb.ContractSettled {
92+
93+
close(c.swapInvoiceSubscription.Update)
94+
close(c.swapInvoiceSubscription.Err)
95+
}
8796
}

0 commit comments

Comments
 (0)