@@ -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.
@@ -82,7 +83,7 @@ type MailBox interface {
82
83
83
84
// Start starts the mailbox and any goroutines it needs to operate
84
85
// properly.
85
- Start ()
86
+ Start (ctx context. Context )
86
87
87
88
// Stop signals the mailbox and its goroutines for a graceful shutdown.
88
89
Stop ()
@@ -148,7 +149,9 @@ type memoryMailBox struct {
148
149
149
150
wireShutdown chan struct {}
150
151
pktShutdown chan struct {}
151
- quit chan struct {}
152
+
153
+ cancel fn.Option [context.CancelFunc ]
154
+ quit chan struct {}
152
155
153
156
// feeRate is set when the link receives or sends out fee updates. It
154
157
// is refreshed when AttachMailBox is called in case a fee update did
@@ -205,10 +208,13 @@ const (
205
208
// Start starts the mailbox and any goroutines it needs to operate properly.
206
209
//
207
210
// NOTE: This method is part of the MailBox interface.
208
- func (m * memoryMailBox ) Start () {
211
+ func (m * memoryMailBox ) Start (ctx context. Context ) {
209
212
m .started .Do (func () {
210
- go m .wireMailCourier ()
211
- go m .pktMailCourier ()
213
+ ctx , cancel := context .WithCancel (ctx )
214
+ m .cancel = fn .Some (cancel )
215
+
216
+ go m .wireMailCourier (ctx )
217
+ go m .pktMailCourier (ctx )
212
218
})
213
219
}
214
220
@@ -324,6 +330,7 @@ func (m *memoryMailBox) HasPacket(inKey CircuitKey) bool {
324
330
// NOTE: This method is part of the MailBox interface.
325
331
func (m * memoryMailBox ) Stop () {
326
332
m .stopped .Do (func () {
333
+ m .cancel .WhenSome (func (fn context.CancelFunc ) { fn () })
327
334
close (m .quit )
328
335
329
336
m .signalUntilShutdown (wireCourier )
@@ -372,7 +379,7 @@ func (p *pktWithExpiry) deadline(clock clock.Clock) <-chan time.Time {
372
379
373
380
// wireMailCourier is a dedicated goroutine whose job is to reliably deliver
374
381
// wire messages.
375
- func (m * memoryMailBox ) wireMailCourier () {
382
+ func (m * memoryMailBox ) wireMailCourier (ctx context. Context ) {
376
383
defer close (m .wireShutdown )
377
384
378
385
for {
@@ -389,6 +396,9 @@ func (m *memoryMailBox) wireMailCourier() {
389
396
case <- m .quit :
390
397
m .wireCond .L .Unlock ()
391
398
return
399
+ case <- ctx .Done ():
400
+ m .wireCond .L .Unlock ()
401
+ return
392
402
default :
393
403
}
394
404
}
@@ -418,13 +428,15 @@ func (m *memoryMailBox) wireMailCourier() {
418
428
close (msgDone )
419
429
case <- m .quit :
420
430
return
431
+ case <- ctx .Done ():
432
+ return
421
433
}
422
434
}
423
435
}
424
436
425
437
// pktMailCourier is a dedicated goroutine whose job is to reliably deliver
426
438
// packet messages.
427
- func (m * memoryMailBox ) pktMailCourier () {
439
+ func (m * memoryMailBox ) pktMailCourier (ctx context. Context ) {
428
440
defer close (m .pktShutdown )
429
441
430
442
for {
@@ -447,6 +459,11 @@ func (m *memoryMailBox) pktMailCourier() {
447
459
case <- m .quit :
448
460
m .pktCond .L .Unlock ()
449
461
return
462
+
463
+ case <- ctx .Done ():
464
+ m .pktCond .L .Unlock ()
465
+ return
466
+
450
467
default :
451
468
}
452
469
}
@@ -541,7 +558,7 @@ func (m *memoryMailBox) pktMailCourier() {
541
558
case <- deadline :
542
559
log .Debugf ("Expiring add htlc with " +
543
560
"keystone=%v" , add .keystone ())
544
- m .FailAdd (add )
561
+ m .FailAdd (ctx , add )
545
562
546
563
case pktDone := <- m .pktReset :
547
564
m .pktCond .L .Lock ()
@@ -553,6 +570,9 @@ func (m *memoryMailBox) pktMailCourier() {
553
570
554
571
case <- m .quit :
555
572
return
573
+
574
+ case <- ctx .Done ():
575
+ return
556
576
}
557
577
}
558
578
}
@@ -688,9 +708,7 @@ func (m *memoryMailBox) DustPackets() (lnwire.MilliSatoshi,
688
708
// delivered after the link restarts if the switch has remained online. The
689
709
// generated LinkError will show an OutgoingFailureDownstreamHtlcAdd
690
710
// FailureDetail.
691
- func (m * memoryMailBox ) FailAdd (pkt * htlcPacket ) {
692
- ctx := context .TODO ()
693
-
711
+ func (m * memoryMailBox ) FailAdd (ctx context.Context , pkt * htlcPacket ) {
694
712
// First, remove the packet from mailbox. If we didn't find the packet
695
713
// because it has already been acked, we'll exit early to avoid sending
696
714
// a duplicate fail message through the switch.
@@ -844,8 +862,8 @@ func (mo *mailOrchestrator) Stop() {
844
862
845
863
// GetOrCreateMailBox returns an existing mailbox belonging to `chanID`, or
846
864
// creates and returns a new mailbox if none is found.
847
- func (mo * mailOrchestrator ) GetOrCreateMailBox (chanID lnwire. ChannelID ,
848
- shortChanID lnwire.ShortChannelID ) MailBox {
865
+ func (mo * mailOrchestrator ) GetOrCreateMailBox (ctx context. Context ,
866
+ chanID lnwire. ChannelID , shortChanID lnwire.ShortChannelID ) MailBox {
849
867
850
868
// First, try lookup the mailbox directly using only the shared mutex.
851
869
mo .mu .RLock ()
@@ -859,7 +877,7 @@ func (mo *mailOrchestrator) GetOrCreateMailBox(chanID lnwire.ChannelID,
859
877
// Otherwise, we will try again with exclusive lock, creating a mailbox
860
878
// if one still has not been created.
861
879
mo .mu .Lock ()
862
- mailbox = mo .exclusiveGetOrCreateMailBox (chanID , shortChanID )
880
+ mailbox = mo .exclusiveGetOrCreateMailBox (ctx , chanID , shortChanID )
863
881
mo .mu .Unlock ()
864
882
865
883
return mailbox
@@ -870,7 +888,7 @@ func (mo *mailOrchestrator) GetOrCreateMailBox(chanID lnwire.ChannelID,
870
888
// recorded.
871
889
//
872
890
// NOTE: This method MUST be invoked with the mailOrchestrator's exclusive lock.
873
- func (mo * mailOrchestrator ) exclusiveGetOrCreateMailBox (
891
+ func (mo * mailOrchestrator ) exclusiveGetOrCreateMailBox (ctx context. Context ,
874
892
chanID lnwire.ChannelID , shortChanID lnwire.ShortChannelID ) MailBox {
875
893
876
894
mailbox , ok := mo .mailboxes [chanID ]
@@ -882,7 +900,7 @@ func (mo *mailOrchestrator) exclusiveGetOrCreateMailBox(
882
900
expiry : mo .cfg .expiry ,
883
901
failMailboxUpdate : mo .cfg .failMailboxUpdate ,
884
902
})
885
- mailbox .Start ()
903
+ mailbox .Start (ctx )
886
904
mo .mailboxes [chanID ] = mailbox
887
905
}
888
906
@@ -916,7 +934,7 @@ func (mo *mailOrchestrator) BindLiveShortChanID(mailbox MailBox,
916
934
// to channel_id. If the mailbox is found, the message is delivered directly.
917
935
// Otherwise the packet is recorded as unclaimed, and will be delivered to the
918
936
// mailbox upon the subsequent call to BindLiveShortChanID.
919
- func (mo * mailOrchestrator ) Deliver (
937
+ func (mo * mailOrchestrator ) Deliver (ctx context. Context ,
920
938
sid lnwire.ShortChannelID , pkt * htlcPacket ) error {
921
939
922
940
var (
@@ -961,7 +979,7 @@ func (mo *mailOrchestrator) Deliver(
961
979
// index should only be set if the mailbox had been initialized
962
980
// beforehand. However, this does ensure that this case is
963
981
// handled properly in the event that it could happen.
964
- mailbox = mo .exclusiveGetOrCreateMailBox (chanID , sid )
982
+ mailbox = mo .exclusiveGetOrCreateMailBox (ctx , chanID , sid )
965
983
mo .mu .Unlock ()
966
984
967
985
// Deliver the packet to the mailbox if it was found or created.
0 commit comments