@@ -10,6 +10,7 @@ import (
10
10
"time"
11
11
12
12
"github.com/lightningnetwork/lnd/clock"
13
+ "github.com/lightningnetwork/lnd/fn/v2"
13
14
"github.com/lightningnetwork/lnd/lntypes"
14
15
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
15
16
"github.com/lightningnetwork/lnd/lnwire"
@@ -53,7 +54,7 @@ type MailBox interface {
53
54
// packet from being delivered after the link restarts if the switch has
54
55
// remained online. The generated LinkError will show an
55
56
// OutgoingFailureDownstreamHtlcAdd FailureDetail.
56
- FailAdd (pkt * htlcPacket )
57
+ FailAdd (ctx context. Context , pkt * htlcPacket )
57
58
58
59
// MessageOutBox returns a channel that any new messages ready for
59
60
// delivery will be sent on.
@@ -149,6 +150,7 @@ type memoryMailBox struct {
149
150
wireShutdown chan struct {}
150
151
pktShutdown chan struct {}
151
152
quit chan struct {}
153
+ cancel fn.Option [context.CancelFunc ]
152
154
153
155
// feeRate is set when the link receives or sends out fee updates. It
154
156
// is refreshed when AttachMailBox is called in case a fee update did
@@ -207,8 +209,11 @@ const (
207
209
// NOTE: This method is part of the MailBox interface.
208
210
func (m * memoryMailBox ) Start () {
209
211
m .started .Do (func () {
212
+ ctx , cancel := context .WithCancel (context .Background ())
213
+ m .cancel = fn .Some (cancel )
214
+
210
215
go m .wireMailCourier ()
211
- go m .pktMailCourier ()
216
+ go m .pktMailCourier (ctx )
212
217
})
213
218
}
214
219
@@ -324,6 +329,7 @@ func (m *memoryMailBox) HasPacket(inKey CircuitKey) bool {
324
329
// NOTE: This method is part of the MailBox interface.
325
330
func (m * memoryMailBox ) Stop () {
326
331
m .stopped .Do (func () {
332
+ m .cancel .WhenSome (func (fn context.CancelFunc ) { fn () })
327
333
close (m .quit )
328
334
329
335
m .signalUntilShutdown (wireCourier )
@@ -424,7 +430,7 @@ func (m *memoryMailBox) wireMailCourier() {
424
430
425
431
// pktMailCourier is a dedicated goroutine whose job is to reliably deliver
426
432
// packet messages.
427
- func (m * memoryMailBox ) pktMailCourier () {
433
+ func (m * memoryMailBox ) pktMailCourier (ctx context. Context ) {
428
434
defer close (m .pktShutdown )
429
435
430
436
for {
@@ -541,7 +547,7 @@ func (m *memoryMailBox) pktMailCourier() {
541
547
case <- deadline :
542
548
log .Debugf ("Expiring add htlc with " +
543
549
"keystone=%v" , add .keystone ())
544
- m .FailAdd (add )
550
+ m .FailAdd (ctx , add )
545
551
546
552
case pktDone := <- m .pktReset :
547
553
m .pktCond .L .Lock ()
@@ -688,9 +694,7 @@ func (m *memoryMailBox) DustPackets() (lnwire.MilliSatoshi,
688
694
// delivered after the link restarts if the switch has remained online. The
689
695
// generated LinkError will show an OutgoingFailureDownstreamHtlcAdd
690
696
// FailureDetail.
691
- func (m * memoryMailBox ) FailAdd (pkt * htlcPacket ) {
692
- ctx := context .TODO ()
693
-
697
+ func (m * memoryMailBox ) FailAdd (ctx context.Context , pkt * htlcPacket ) {
694
698
// First, remove the packet from mailbox. If we didn't find the packet
695
699
// because it has already been acked, we'll exit early to avoid sending
696
700
// a duplicate fail message through the switch.
0 commit comments