Skip to content

Commit b58c230

Browse files
committed
proof: ensure callers always tear down couriers
This is similar to the prior commit: we add a new method to allow a caller to close down a courier once they're done with it. This ensures that we'll always release the resources once we're done with them.
1 parent 8cff364 commit b58c230

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

proof/courier.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"github.com/btcsuite/btcd/btcec/v2"
14+
"github.com/davecgh/go-spew/spew"
1415
"github.com/lightninglabs/lightning-node-connect/hashmailrpc"
1516
"github.com/lightninglabs/taproot-assets/asset"
1617
"github.com/lightninglabs/taproot-assets/fn"
@@ -66,6 +67,9 @@ type Courier interface {
6667
// SetSubscribers sets the set of subscribers that will be notified
6768
// of proof courier related events.
6869
SetSubscribers(map[uint64]*fn.EventReceiver[fn.Event])
70+
71+
// Close stops the courier instance.
72+
Close() error
6973
}
7074

7175
// CourierAddr is a fully validated courier address (including protocol specific
@@ -223,6 +227,7 @@ func (h *UniverseRpcCourierAddr) NewCourier(_ context.Context,
223227
backoffHandle: backoffHandle,
224228
transfer: cfg.TransferLog,
225229
subscribers: subscribers,
230+
rawConn: conn,
226231
}, nil
227232
}
228233

@@ -291,11 +296,16 @@ type ProofMailbox interface {
291296
// CleanUp attempts to tear down the mailbox as specified by the passed
292297
// sid.
293298
CleanUp(ctx context.Context, sid streamID) error
299+
300+
// Close closes the underlying connection to the hashmail server.
301+
Close() error
294302
}
295303

296304
// HashMailBox is an implementation of the ProofMailbox interface backed by the
297305
// hashmailrpc.HashMailClient.
298306
type HashMailBox struct {
307+
rawConn *grpc.ClientConn
308+
299309
client hashmailrpc.HashMailClient
300310
}
301311

@@ -341,7 +351,8 @@ func NewHashMailBox(courierAddr *url.URL) (*HashMailBox,
341351
client := hashmailrpc.NewHashMailClient(conn)
342352

343353
return &HashMailBox{
344-
client: client,
354+
client: client,
355+
rawConn: conn,
345356
}, nil
346357
}
347358

@@ -480,6 +491,11 @@ func (h *HashMailBox) CleanUp(ctx context.Context, sid streamID) error {
480491
return err
481492
}
482493

494+
// Close closes the underlying connection to the hashmail server.
495+
func (h *HashMailBox) Close() error {
496+
return h.rawConn.Close()
497+
}
498+
483499
// A compile-time assertion to ensure that the HashMailBox meets the
484500
// ProofMailbox interface.
485501
var _ ProofMailbox = (*HashMailBox)(nil)
@@ -853,6 +869,8 @@ func (h *HashMailCourier) DeliverProof(ctx context.Context,
853869

854870
log.Infof("Received ACK from receiver! Cleaning up mailboxes...")
855871

872+
defer h.Close()
873+
856874
// Once we receive this ACK, we can clean up our mailbox and also the
857875
// receiver's mailbox.
858876
if err := h.mailbox.CleanUp(ctx, senderStreamID); err != nil {
@@ -928,6 +946,17 @@ func (h *HashMailCourier) publishSubscriberEvent(event fn.Event) {
928946
}
929947
}
930948

949+
// Close closes the underlying connection to the hashmail server.
950+
func (h *HashMailCourier) Close() error {
951+
if err := h.mailbox.Close(); err != nil {
952+
log.Warnf("unable to close mailbox session, "+
953+
"recipient=%v: %v", err, spew.Sdump(h.recipient))
954+
return err
955+
}
956+
957+
return nil
958+
}
959+
931960
// BackoffWaitEvent is an event that is sent to a subscriber each time we
932961
// wait via the Backoff procedure before retrying to deliver a proof to the
933962
// receiver.
@@ -1030,6 +1059,10 @@ type UniverseRpcCourier struct {
10301059
// the universe RPC server.
10311060
client unirpc.UniverseClient
10321061

1062+
// rawConn is the raw connection that the courier will use to interact
1063+
// with the remote gRPC service.
1064+
rawConn *grpc.ClientConn
1065+
10331066
// backoffHandle is a handle to the backoff procedure used in proof
10341067
// delivery.
10351068
backoffHandle *BackoffHandler
@@ -1297,6 +1330,11 @@ func (c *UniverseRpcCourier) publishSubscriberEvent(event fn.Event) {
12971330
}
12981331
}
12991332

1333+
// Close closes the courier's connection to the remote gRPC service.
1334+
func (c *UniverseRpcCourier) Close() error {
1335+
return c.rawConn.Close()
1336+
}
1337+
13001338
// A compile-time assertion to ensure the UniverseRpcCourier meets the
13011339
// proof.Courier interface.
13021340
var _ Courier = (*UniverseRpcCourier)(nil)

tapfreighter/chain_porter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,8 @@ func (p *ChainPorter) transferReceiverProof(pkg *sendPackage) error {
671671
"service handle: %w", err)
672672
}
673673

674+
defer courier.Close()
675+
674676
// Update courier events subscribers before attempting to
675677
// deliver proof.
676678
p.subscriberMtx.Lock()

universe/auto_syncer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ func (f *FederationEnvoy) Start() error {
175175
return nil
176176
}
177177

178+
// Close frees up any ephemeral resources allocated by the envoy.
179+
func (f *FederationEnvoy) Close() error {
180+
return nil
181+
}
182+
178183
// Stop stops all active goroutines.
179184
func (f *FederationEnvoy) Stop() error {
180185
f.stopOnce.Do(func() {

universe/base.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ func NewArchive(cfg ArchiveConfig) *Archive {
7171
return a
7272
}
7373

74+
// Close closes the archive, stopping all goroutines and freeing all resources.
75+
func (a *Archive) Close() error {
76+
return nil
77+
}
78+
7479
// fetchUniverse returns the base universe instance for the passed identifier.
7580
// The universe will be loaded in on demand if it has not been seen before.
7681
func (a *Archive) fetchUniverse(id Identifier) BaseBackend {

0 commit comments

Comments
 (0)