Skip to content

Commit d15e867

Browse files
committed
sweep: add timeout when waiting for spending event
1 parent 0ebcfd9 commit d15e867

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

sweep/fee_bumper.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"sync"
77
"sync/atomic"
8+
"time"
89

910
"github.com/btcsuite/btcd/btcutil"
1011
"github.com/btcsuite/btcd/chaincfg/chainhash"
@@ -24,6 +25,10 @@ import (
2425
"github.com/lightningnetwork/lnd/tlv"
2526
)
2627

28+
// spentNotificationTimeout defines the time to wait for a spending event from
29+
// `RegisterSpendNtfn` when an immediate response is expected.
30+
const spentNotificationTimeout = 1 * time.Second
31+
2732
var (
2833
// ErrInvalidBumpResult is returned when the bump result is invalid.
2934
ErrInvalidBumpResult = errors.New("invalid bump result")
@@ -1467,6 +1472,25 @@ func (t *TxPublisher) getSpentInputs(
14671472
spendingTx.TxHash())
14681473

14691474
spentInputs[op] = spendingTx
1475+
1476+
// The above spent event should be returned immediately, yet we
1477+
// still perform a timeout check here in case it blocks forever.
1478+
//
1479+
// TODO(yy): The proper way to fix this is to redesign the area
1480+
// so we use the async flow for checking whether a given input
1481+
// is spent or not. A better approach is to implement a new
1482+
// synchronous method to check for spending, which should be
1483+
// attempted when implementing SQL into btcwallet.
1484+
case <-time.After(spentNotificationTimeout):
1485+
log.Errorf("Input is reported as spent by GetUtxo, "+
1486+
"but spending notification is not returned "+
1487+
"immediately: input=%v, heightHint=%v", op,
1488+
heightHint)
1489+
1490+
// Return with the inputs we've found so far. This
1491+
// prevents us from treating this known-spent input as
1492+
// unspent in this cycle.
1493+
return spentInputs
14701494
}
14711495
}
14721496

0 commit comments

Comments
 (0)