From 50025b007c8f153335639f3037247b0f9c62202d Mon Sep 17 00:00:00 2001 From: claudell Date: Fri, 30 Oct 2026 20:04:15 +0000 Subject: [PATCH 1/3] docs: document sub-server lnd connection ownership model Add an authoritative reference page for the connection-ownership relationship between litd and its integrated sub-servers: litd creates and owns the single shared *lndclient.GrpcLndServices and closes it exactly once in shutdownSubServers; integrated sub-servers receive a borrowed, non-closable view via StartAsSubserver and must never close it. Includes mermaid diagrams of the ownership relationship and the connection lifecycle, the failure mode behind the remote-lnd report in #1381 (comment 6530026672), and the verification layers (type-level prevention, regression tests, static guards) that keep this bug class from silently recurring. Refs lightninglabs/lightning-terminal#1381 (comment 6530026672). Refs lightninglabs/lightning-terminal#1329 (comment 6530856512). --- docs/subserver-connection-ownership.md | 126 +++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 docs/subserver-connection-ownership.md diff --git a/docs/subserver-connection-ownership.md b/docs/subserver-connection-ownership.md new file mode 100644 index 000000000..8806f57b1 --- /dev/null +++ b/docs/subserver-connection-ownership.md @@ -0,0 +1,126 @@ +# Sub-server lnd connection ownership + +This document describes how the single lnd connection inside `litd` is owned, +shared with the integrated sub-servers (Faraday, Loop, Pool, tapd), and shut +down. It is the authoritative reference for the connection-ownership model +that fixed the bug class reported in +[lightning-terminal#1381](https://github.com/lightninglabs/lightning-terminal/issues/1381#issuecomment-6530026672). + +## The ownership model + +`litd` creates exactly one `*lndclient.GrpcLndServices` (the shared lnd +connection) and is its sole owner. Only the owner may close it, and it does so +exactly once, in `shutdownSubServers` (`terminal.go`), after all sub-servers +have been stopped. + +Integrated sub-servers never receive the closable wrapper. They receive a +**borrowed, non-closable view** (`*lndclient.LndServices`) through +`StartAsSubserver` and must never close it — with the type-level fix in +[loop#1272](https://github.com/lightninglabs/loop/pull/1272) and +[pool#528](https://github.com/lightninglabs/pool/pull/528), they cannot close +it even accidentally, because the `Close` method only exists on the wrapper, +which they never see. On the `litd` side, `subservers/loop.go` and +`subservers/pool.go` hand over that view as `&lndGrpc.LndServices`. + +```mermaid +flowchart TD + subgraph LIT["lightning-terminal (litd) — OWNER"] + CREATE["setupFullLNDClient()
creates the single shared
*lndclient.GrpcLndServices
(closable wrapper)"] + SHUTDOWN["shutdownSubServers()
stops all sub-servers first,
then closes the shared conn
exactly once"] + end + + subgraph SUBSERVERS["integrated sub-servers — BORROWERS"] + FARADAY["Faraday
reference model:
lndOwned tracking"] + LOOP["Loop
StartAsSubserver(
*lndclient.LndServices)"] + POOL["Pool
StartAsSubserver(
*lndclient.LndServices)"] + TAPD["tapd
never receives the wrapper"] + end + + LND[("remote or integrated lnd")] + + CREATE -- "gRPC conn + subserver clients" --> LND + CREATE -- "borrowed non-closable view
(*lndclient.LndServices)" --> LOOP + CREATE -- "borrowed non-closable view" --> POOL + CREATE -- "borrowed view
(lndOwned=false)" --> FARADAY + CREATE -- "services only" --> TAPD + + LOOP -. "must never close
(type-level: no Close in scope)" .-> LND + POOL -. "must never close
(type-level: no Close in scope)" .-> LND + SHUTDOWN -- "the ONLY Close()
on the shared conn" --> LND +``` + +## Lifecycle and failure mode + +```mermaid +stateDiagram-v2 + [*] --> ConnAlive: litd creates shared GrpcLndServices + ConnAlive --> SubserversRunning: litd injects borrowed view
via StartAsSubserver + SubserversRunning --> SubserverStopped: a sub-server stops
(graceful or runtime error) + SubserverStopped --> SubserversRunning: other sub-servers keep
using the SAME conn + SubserversRunning --> ConnClosed: litd shutdownSubServers()
after ALL sub-servers stopped + ConnClosed --> [*] + + note right of SubserverStopped + INVARIANT: a sub-server stopping + must NOT close the shared conn. + Pre-fix, loop/pool violated this: + every other component then failed + with "grpc: the client connection + is closing" while remote lnd + stayed healthy. + end note +``` + +### The bug that motivated this model + +Before the fix, an embedded Loop daemon closed the shared connection on +**any** shutdown — graceful `Stop()` or a runtime error — and an embedded Pool +server did the same in `Server.Stop()`. The moment one sub-server stopped, the +connection every other component depends on was torn down: the LiT macaroon +service (`signrpc.DeriveSharedKey`), tapd, faraday and accounts all failed +with `grpc: the client connection is closing`, while the remote lnd itself was +perfectly healthy. In the reported incident (litd v0.17.8, remote lnd +v0.21.4-beta), this happened 12ms after "Starting LiT macaroon service". + +### Historical note: Faraday's `lndOwned` flag + +Faraday was the reference model for the first layer of the fix: it tracks +whether it created the connection itself (`lndOwned`, set in `Start`) and only +closes the connection when it owns it. Loop and Pool now go one step further +with **type-level prevention**: instead of a runtime flag, they hold the +closable wrapper only in a dedicated owner field (`lndOwner`, non-nil only in +standalone mode), and their `StartAsSubserver` entry points accept only the +non-closable `*lndclient.LndServices` view. Borrowed-ness is thereby +unrepresentable — enforced by the type checker, not by convention. + +## The rules + +1. `litd` owns the shared `GrpcLndServices` and closes it exactly once, in + `shutdownSubServers`, after all sub-servers are stopped. +2. Sub-servers receive a borrowed, non-closable view via `StartAsSubserver`. + They must never close the connection — and cannot, by construction. +3. Standalone daemons (loopd, poold, faraday running alone, or the loop + `view` command) create their own connection and close it when done. +4. A sub-server that needs to signal a fatal error reports it to the parent + (e.g. Loop's `ErrChan`) — it never "cleans up" shared state itself. + +## Verification + +The ownership model is enforced mechanically on three levels, so this bug +class cannot silently recur: + +- **Type level (compile time):** `StartAsSubserver` in Loop and Pool accepts + only the non-closable `*lndclient.LndServices`. The closable wrapper exists + in the sub-server packages only as the `lndOwner` field, set solely on the + standalone path. Judge: the type checker. +- **Regression tests:** `loopd/daemon_ownership_test.go` and + `server_ownership_test.go` (pool) start an embedded daemon/server against a + real (bufconn) gRPC connection and assert the injected `grpc.ClientConn` + does not enter `Shutdown` state after a graceful stop or a runtime-error + stop. Both tests were demonstrated to fail on the pre-fix code. +- **Static guard:** `loopd/ownership_guard_test.go` and + `server_ownership_guard_test.go` (pool) parse the package AST in CI (as + ordinary unit tests) and fail if any `.Close()` call targets an lnd + connection handle outside the whitelisted owner sites, or if + `StartAsSubserver` ever accepts the closable wrapper again. Both guards were + demonstrated to fail on the pre-fix code. From 8c95edca76ebf1dbbe5660638e554d7b21574044 Mon Sep 17 00:00:00 2001 From: claudell Date: Fri, 30 Oct 2026 22:54:48 +0000 Subject: [PATCH 2/3] multi: bump loop and pool to lnd conn-ownership fix Pin loop and pool to the connection-ownership fix from loop#1272 and pool#528, backported onto the currently pinned v0.35.2-beta and v0.7.2-beta releases. The replace directives are to be swapped for tagged releases before merge. The fix makes StartAsSubserver take the non-closable *lndclient.LndServices view instead of the closable *lndclient.GrpcLndServices wrapper, so an integrated Loop or Pool can no longer close LiT's shared lnd connection when it stops. Update both call sites to hand over the borrowed view; LiT remains the sole owner and closes the connection once in shutdownSubServers. --- go.mod | 6 ++++++ go.sum | 8 ++++---- subservers/loop.go | 4 +++- subservers/pool.go | 6 +++++- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 0e516c948..44a551de4 100644 --- a/go.mod +++ b/go.mod @@ -269,3 +269,9 @@ tool github.com/lightninglabs/taproot-assets/cmd/tapcli // Use the locally patched TLV module until the upstream fix is released. replace github.com/lightningnetwork/lnd/tlv => ./third_party/lnd/tlv + +// NOTE: drop these and bump to tagged releases once loop#1272 and +// pool#528 are merged and tagged. Until then, pin the connection-ownership fix +// backported onto the currently pinned loop v0.35.2-beta / pool v0.7.2-beta. +replace github.com/lightninglabs/loop => github.com/litbot-9000/loop v0.32.1-beta.0.20261030222623-d72d75fb1ca1 +replace github.com/lightninglabs/pool => github.com/litbot-9000/pool v0.7.2-beta.0.20261030222405-5b777bbd9744 diff --git a/go.sum b/go.sum index 54d91398b..cef9ae7ec 100644 --- a/go.sum +++ b/go.sum @@ -1149,8 +1149,6 @@ github.com/lightninglabs/lightning-node-connect/mailbox v1.0.2-0.20260727084653- github.com/lightninglabs/lightning-node-connect/mailbox v1.0.2-0.20260727084653-785ec57e5d1d/go.mod h1:7VXY4plpd6U+0wJa47OsaGg1bTXVdWO6NLHY8olKHwQ= github.com/lightninglabs/lndclient v0.21.0-2 h1:gA1utFMoKV6OZfHpYAQ1TX0wVs5cAtgTORRVavHR/ls= github.com/lightninglabs/lndclient v0.21.0-2/go.mod h1:RUIcfPr82HrvZr3pu9f8nbD5v6VFbm+KgExqNNp5bE4= -github.com/lightninglabs/loop v0.35.2-beta h1:kPDn4e7PwMd0FuB8iNm0lrqB1KFc3YA67p8rSMTtv80= -github.com/lightninglabs/loop v0.35.2-beta/go.mod h1:1kM8+JiLhR2SpMIFM8TzhdNWvYrv9an2zp2zike1GTw= github.com/lightninglabs/loop/looprpc v1.0.17 h1:FblJqdD+S0+Toc5U16AWlgBuxmgIXOO5WbJpFHrDj1g= github.com/lightninglabs/loop/looprpc v1.0.17/go.mod h1:MGG6KZumEQMTIFWmLiOu5EEZSi8VGYnRuH6emEJoAfk= github.com/lightninglabs/loop/swapserverrpc v1.0.22 h1:NT6MLJ6ZryFzYPboFoJePX0fSw9Uvr3PmA/frB++o34= @@ -1161,8 +1159,6 @@ github.com/lightninglabs/neutrino v0.17.1 h1:lNhgq7ix/N81R6oATroP/kHMzH1qzVVF2dE github.com/lightninglabs/neutrino v0.17.1/go.mod h1:tcwCgRTGWcaua0L/xzdwllW8eslHDbux4XkiYsivvHE= github.com/lightninglabs/neutrino/cache v1.1.3 h1:rgnabC41W+XaPuBTQrdeFjFCCAVKh1yctAgmb3Se9zA= github.com/lightninglabs/neutrino/cache v1.1.3/go.mod h1:qxkJb+pUxR5p84jl5uIGFCR4dGdFkhNUwMSxw3EUWls= -github.com/lightninglabs/pool v0.7.2-beta h1:aI6FG/sfpx2XQIZdHve5+bX9ZeMNz4InmiBZfc/uHWw= -github.com/lightninglabs/pool v0.7.2-beta/go.mod h1:Ttiqa41jcABQp8CmEVpcib+o5PJdaQov8o3Nqj4ZTDE= github.com/lightninglabs/pool/auctioneerrpc v1.1.3 h1:Di5Nf8Gll9wl6tbXsusGXj9e4yNsHvoJ0ssgoHyFXqY= github.com/lightninglabs/pool/auctioneerrpc v1.1.3/go.mod h1:N/X9SxrBCjquK8XkwSgk4qvGC2g8Dra9uiw5sXWmEl0= github.com/lightninglabs/pool/poolrpc v1.0.1 h1:XbNx28TYwEj/PVsnnF9TnveVCMCYfS1vVkcwz29vPmM= @@ -1201,6 +1197,10 @@ github.com/lightningnetwork/lnd/ticker v1.1.1 h1:J/b6N2hibFtC7JLV77ULQp++QLtCwT6 github.com/lightningnetwork/lnd/ticker v1.1.1/go.mod h1:waPTRAAcwtu7Ji3+3k+u/xH5GHovTsCoSVpho0KDvdA= github.com/lightningnetwork/lnd/tor v1.1.7 h1:BfY1KQGz3LVaeaAGI4XYGYCwDQR1ADFD1P/uXsS2dQc= github.com/lightningnetwork/lnd/tor v1.1.7/go.mod h1:DANQ6QCQBiR+CqrM+mKudgkMy6CR/RS4ALDbjb2W4FU= +github.com/litbot-9000/loop v0.32.1-beta.0.20261030222623-d72d75fb1ca1 h1:RJlFNjmnHryY8uE+tXrcvWHhOgOS4mHQytF/mrzrcoo= +github.com/litbot-9000/loop v0.32.1-beta.0.20261030222623-d72d75fb1ca1/go.mod h1:1kM8+JiLhR2SpMIFM8TzhdNWvYrv9an2zp2zike1GTw= +github.com/litbot-9000/pool v0.7.2-beta.0.20261030222405-5b777bbd9744 h1:/89AVeNYg8dxDZT3RgsmFuB/Yu85p1h+b8z0Icx7j8o= +github.com/litbot-9000/pool v0.7.2-beta.0.20261030222405-5b777bbd9744/go.mod h1:Ttiqa41jcABQp8CmEVpcib+o5PJdaQov8o3Nqj4ZTDE= github.com/ltcsuite/ltcd v0.0.0-20190101042124-f37f8bf35796 h1:sjOGyegMIhvgfq5oaue6Td+hxZuf3tDC8lAPrFldqFw= github.com/ltcsuite/ltcd v0.0.0-20190101042124-f37f8bf35796/go.mod h1:3p7ZTf9V1sNPI5H8P3NkTFF4LuwMdPl2DodF60qAKqY= github.com/ltcsuite/ltcutil v0.0.0-20181217130922-17f3b04680b6/go.mod h1:8Vg/LTOO0KYa/vlHWJ6XZAevPQThGH5sufO0Hrou/lA= diff --git a/subservers/loop.go b/subservers/loop.go index 105ffabb6..f519fb28a 100644 --- a/subservers/loop.go +++ b/subservers/loop.go @@ -71,7 +71,9 @@ func (l *loopSubServer) RemoteConfig() *RemoteDaemonConfig { func (l *loopSubServer) Start(_ lnrpc.LightningClient, lndGrpc *lndclient.GrpcLndServices, withMacaroonService bool) error { - return l.StartAsSubserver(lndGrpc, withMacaroonService) + // Loop only borrows the shared lnd connection, so it gets the + // non-closable view. LiT owns the connection and closes it on shutdown. + return l.StartAsSubserver(&lndGrpc.LndServices, withMacaroonService) } // Stop stops the sub-server in integrated mode. diff --git a/subservers/pool.go b/subservers/pool.go index 266b38e19..e533acf66 100644 --- a/subservers/pool.go +++ b/subservers/pool.go @@ -70,7 +70,11 @@ func (p *poolSubServer) RemoteConfig() *RemoteDaemonConfig { func (p *poolSubServer) Start(lnClient lnrpc.LightningClient, lndGrpc *lndclient.GrpcLndServices, withMacaroonService bool) error { - return p.StartAsSubserver(lnClient, lndGrpc, withMacaroonService) + // Pool only borrows the shared lnd connection, so it gets the + // non-closable view. LiT owns the connection and closes it on shutdown. + return p.StartAsSubserver( + lnClient, &lndGrpc.LndServices, withMacaroonService, + ) } // RegisterGrpcService must register the sub-server's GRPC server with the given From a8e8f52602b75811ad1d824383b2813eefdd6311 Mon Sep 17 00:00:00 2001 From: claudell Date: Fri, 30 Oct 2026 22:54:48 +0000 Subject: [PATCH 3/3] docs: add v0.17.11 release notes for lnd conn-ownership fix --- docs/release-notes/release-notes-0.17.11.md | 56 +++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 docs/release-notes/release-notes-0.17.11.md diff --git a/docs/release-notes/release-notes-0.17.11.md b/docs/release-notes/release-notes-0.17.11.md new file mode 100644 index 000000000..f956cda86 --- /dev/null +++ b/docs/release-notes/release-notes-0.17.11.md @@ -0,0 +1,56 @@ +# Release Notes + +- [Lightning Terminal](#lightning-terminal) + - [Bug Fixes](#bug-fixes) + - [Functional Changes/Additions](#functional-changesadditions) + - [Technical and Architectural Updates](#technical-and-architectural-updates) +- [Integrated Binary Updates](#integrated-binary-updates) + - [LND](#lnd) + - [Loop](#loop) + - [Pool](#pool) + - [Faraday](#faraday) + - [Taproot Assets](#taproot-assets) +- [Contributors](#contributors-alphabetical-order) + +## Lightning Terminal + +### Bug Fixes + +* [Fix integrated Loop and Pool closing LiT's shared lnd + connection](https://github.com/lightninglabs/lightning-terminal/pull/1459) + when they stop or hit a runtime error. Before, every other component + (macaroon service, tapd, faraday, accounts) then failed with `grpc: the + client connection is closing` even though lnd was healthy + ([#1381](https://github.com/lightninglabs/lightning-terminal/issues/1381)). + Loop and Pool now only receive a non-closable view of the connection. + +### Functional Changes/Additions + +### Technical and Architectural Updates + +* [Document the sub-server lnd connection ownership + model](https://github.com/lightninglabs/lightning-terminal/pull/1459) in + [`docs/subserver-connection-ownership.md`](../subserver-connection-ownership.md). + +## RPC Updates + +## Integrated Binary Updates + +### LND + +### Loop +* [PR](https://github.com/lightninglabs/lightning-terminal/pull/1459): Bump + `loop` to the connection-ownership fix + ([loop#1272](https://github.com/lightninglabs/loop/pull/1272)). + +### Pool +* [PR](https://github.com/lightninglabs/lightning-terminal/pull/1459): Bump + `pool` to the connection-ownership fix + ([pool#528](https://github.com/lightninglabs/pool/pull/528)). + +### Faraday + +### Taproot Assets + +# Contributors (Alphabetical Order) +