contractcourt: modify the incoming contest resolver to use concurrent queue

In this commit, we modify the incoming contest resolver to use a
concurrent queue. This is meant to ensure that the invoice registry
subscription loop never blocks. This change is meant to be minimal and
implements option `5` as outlined here:
https://github.com/lightningnetwork/lnd/issues/8023.

With this change, the inner loop of the subscription dispatch method in
the invoice registry will no longer block, as the concurrent queue uses
a fixed buffer of a queue, then overflows into another queue when that
gets full.

Fixes https://github.com/lightningnetwork/lnd/issues/7917
This commit is contained in:
Olaoluwa Osuntokun committed 2023-09-25 18:48:30 -05:00
1 parent a1b0a7eb02
commit abe888f719
2 files changed
+17 -4

No files matched your search

@@ -16,6 +16,7 @@ import (
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/queue"
)
// htlcIncomingContestResolver is a ContractResolver that's able to resolve an
@@ -283,12 +284,15 @@ func (h *htlcIncomingContestResolver) Resolve() (ContractResolver, error) {
}
var (
hodlChan chan interface{}
hodlChan <-chan interface{}
witnessUpdates <-chan lntypes.Preimage
)
if payload.FwdInfo.NextHop == hop.Exit {
// Create a buffered hodl chan to prevent deadlock.
hodlChan = make(chan interface{}, 1)
hodlQueue := queue.NewConcurrentQueue(10)
hodlQueue.Start()
hodlChan = hodlQueue.ChanOut()
// Notify registry that we are potentially resolving as an exit
// hop on-chain. If this HTLC indeed pays to an existing
@@ -301,13 +305,17 @@ func (h *htlcIncomingContestResolver) Resolve() (ContractResolver, error) {
resolution, err := h.Registry.NotifyExitHopHtlc(
h.htlc.RHash, h.htlc.Amt, h.htlcExpiry, currentHeight,
circuitKey, hodlChan, payload,
circuitKey, hodlQueue.ChanIn(), payload,
)
if err != nil {
return nil, err
}
defer h.Registry.HodlUnsubscribeAll(hodlChan)
defer func() {
h.Registry.HodlUnsubscribeAll(hodlQueue.ChanIn())
hodlQueue.Stop()
}()
// Take action based on the resolution we received. If the htlc
// was settled, or a htlc for a known invoice failed we can
@@ -79,6 +79,11 @@ fails](https://github.com/lightningnetwork/lnd/pull/7876).
* `lnd` [now properly handles a case where an erroneous force close attempt
would impeded start up](https://github.com/lightningnetwork/lnd/pull/7985).
* A bug that could cause the invoice related sub-system to lock up (potentially
the entire daemon) related to [incoming HTLCs going on chain related to a
hodl invoice has been
fixed](https://github.com/lightningnetwork/lnd/pull/8024).
# New Features
## Functional Enhancements