Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/release-notes/release-notes-next.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@

#### Bug Fixes

* Fix a connection ownership bug where an embedded loop daemon closed the
shared lnd connection on shutdown. When running as a subserver of
lightning-terminal, a graceful stop or a runtime error in the loop daemon
tore down the lnd connection owned by the parent process, breaking lnd
access for LiT and all other integrated subservers. The daemon now tracks
whether it created the connection itself and only closes owned connections.

* Bound the static Loop In risk decision replay cache to prevent unbounded
memory growth from swap server notifications for unknown swaps. Cached
decisions retain only a copy of the validated swap hash, without unknown
Expand Down
50 changes: 38 additions & 12 deletions loopd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,22 @@ type Daemon struct {
listenerCfg *ListenerCfg
internalErrChan chan error

lnd *lndclient.GrpcLndServices
// lnd is the set of lnd services the daemon uses. It is a
// non-closable view: the closeable connection wrapper is only ever
// held in lndOwner, and only when the daemon created the connection
// itself (standalone mode).
lnd *lndclient.LndServices
assetClient *assets.TapdClient
clientCleanup func()

// lndOwner holds the full lnd services wrapper, including its Close
// method, if and only if the daemon created the lnd connection
// itself (standalone mode via Start). When the daemon is started as
// a subserver (StartAsSubserver), the lnd connection is shared with
// and owned by the parent process (e.g. LiT) and lndOwner is nil,
// so there is no closable handle to the borrowed connection.
lndOwner *lndclient.GrpcLndServices

wg sync.WaitGroup
quit chan struct{}
stopOnce sync.Once
Expand Down Expand Up @@ -147,11 +159,17 @@ func (d *Daemon) Start() error {
network := lndclient.Network(d.cfg.Network)

var err error
d.lnd, err = d.listenerCfg.getLnd(network, d.cfg.Lnd)
lndGrpc, err := d.listenerCfg.getLnd(network, d.cfg.Lnd)
if err != nil {
return err
}

// We created the lnd connection ourselves, so we own it and are
// responsible for closing it on shutdown. The rest of the daemon
// only ever sees the non-closable services view.
d.lndOwner = lndGrpc
d.lnd = &lndGrpc.LndServices

// Initialize the assets client.
if d.cfg.Tapd.Activate {
d.assetClient, err = assets.NewTapdClient(d.cfg.Tapd)
Expand Down Expand Up @@ -205,7 +223,7 @@ func (d *Daemon) Start() error {
// create its own gRPC server but registers to an existing one. The same goes
// for REST (if enabled), instead of creating an own mux and HTTP server, we
// register to an existing one.
func (d *Daemon) StartAsSubserver(lndGrpc *lndclient.GrpcLndServices,
func (d *Daemon) StartAsSubserver(lndServices *lndclient.LndServices,
withMacaroonService bool) error {

// There should be no reason to start the daemon twice. Therefore,
Expand All @@ -216,8 +234,11 @@ func (d *Daemon) StartAsSubserver(lndGrpc *lndclient.GrpcLndServices,
}

// When starting as a subserver, we get passed in an already established
// connection to lnd that might be shared among other subservers.
d.lnd = lndGrpc
// connection to lnd that might be shared among other subservers. We
// only receive the non-closable services view: the connection is
// owned by the parent process (e.g. LiT) and it is not possible for
// us to close it.
d.lnd = lndServices

// With lnd already pre-connected, initialize everything else, such as
// the swap server client, the RPC server instance and our main swap
Expand Down Expand Up @@ -446,7 +467,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {

// Run the cost migration.
err = loop.MigrateLoopOutCosts(
d.mainCtx, d.lnd.LndServices, d.cfg.MigrationRPCBatchSize,
d.mainCtx, *d.lnd, d.cfg.MigrationRPCBatchSize,
swapDb,
)
if err != nil {
Expand Down Expand Up @@ -491,7 +512,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {

// Create an instance of the loop client library.
swapClient, clientCleanup, err := getClient(
d.cfg, swapDb, sweeperDb, &d.lnd.LndServices, d.assetClient,
d.cfg, swapDb, sweeperDb, d.lnd, d.assetClient,
)
if err != nil {
return err
Expand Down Expand Up @@ -548,7 +569,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
},
RequiredPerms: loop_looprpc.RequiredPermissions,
DBPassword: macDbDefaultPw,
LndClient: &d.lnd.LndServices,
LndClient: d.lnd,
EphemeralKey: lndclient.SharedKeyNUMS,
KeyLocator: lndclient.SharedKeyLocator,
},
Expand Down Expand Up @@ -801,7 +822,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
impl: swapClient,
loopInQuoter: swapClient,
liquidityMgr: liquidityMgr,
lnd: &d.lnd.LndServices,
lnd: d.lnd,
swaps: make(map[lntypes.Hash]loop.SwapInfo),
subscribers: make(map[int]chan<- any),
statusChan: statusChan,
Expand Down Expand Up @@ -1148,9 +1169,14 @@ func (d *Daemon) stop() {
}
}

// Next, shut down the connections to lnd and the swap server.
if d.lnd != nil {
d.lnd.Close()
// Next, shut down the connections to lnd and the swap server. The
// lnd connection is only closed if we created it ourselves
// (standalone mode), in which case lndOwner holds the closable
// wrapper. In subserver mode the connection is owned by the parent
// process and shared with other subservers, closing it here would
// tear down their lnd access as well.
if d.lndOwner != nil {
d.lndOwner.Close()
}
if d.clientCleanup != nil {
d.clientCleanup()
Expand Down
Loading
Loading