Skip to content

pool: track lnd connection ownership, don't close borrowed conns - #528

Merged
Roasbeef merged 2 commits into
lightninglabs:masterfrom
litbot-9000:fix/lnd-conn-ownership
Nov 4, 2026
Merged

Roasbeef merged 2 commits into
lightninglabs:masterfrom
litbot-9000:fix/lnd-conn-ownership

Conversation

@litbot-9000

@litbot-9000 litbot-9000 commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

The bug

When the pool trader runs as an integrated subserver of lightning-terminal (LiT), it receives lnd services backed by a connection that LiT creates once and shares among all its subservers (loop, pool, faraday, tapd) via StartAsSubserver.

On shutdown the trader unconditionally closed that borrowed connection:

  • Server.Stop() called s.lndServices.Close() on the shared connection.

Once that borrowed connection is closed, everything else in the LiT process that uses it fails with grpc: the client connection is closing — the LiT macaroon service (signrpc.DeriveSharedKey), tapd, faraday and accounts — while the remote lnd stays perfectly healthy. This is the root cause behind the remote-lnd startup failure reported in lightninglabs/lightning-terminal#1381 (comment) (litd 0.17.8 + remote lnd 0.21.4-beta failing 12ms after "Starting LiT macaroon service"), as directed in lightninglabs/lightning-terminal#1329 (comment).

Loop had the identical bug (closed both on graceful stop and on a runtime error), fixed in the sister PR lightninglabs/loop#1272. Faraday already tracked connection ownership correctly (lndOwned), and tapd never receives the GrpcLndServices wrapper, so neither needed a fix. LiT itself already closes the shared client exactly once in shutdownSubServers (terminal.go), after stopping all subservers, so no lightning-terminal change is needed for the ownership model. The ownership model is now documented (with diagrams) in lightninglabs/lightning-terminal#1458.

The fix (two layers)

Layer 1 — ownership tracking (commit 1). Mirror faraday's model: Start() (standalone) marks the connection as owned, StartAsSubserver() marks it borrowed, and Stop() only closes owned connections (via stopLnd()).

Layer 2 — type-level prevention (commit 2). Make borrowed-ness unrepresentable. StartAsSubserver now accepts the non-closable *lndclient.LndServices view instead of the *lndclient.GrpcLndServices wrapper, so a subserver cannot call Close on the shared connection even if it tries. The closable wrapper is only held in the new lndOwner field, which is set only when the server creates the connection itself (standalone Start). Judge: the type checker.

Note for integrators: this changes the exported StartAsSubserver signature. LiT's call site (subservers/pool.go) must pass &g.lndClient.LndServices instead of g.lndClient when bumping the pool dependency.

Verification (each layer demonstrated to fail pre-fix)

  • server_ownership_test.go starts a real (bufconn, TLS) mock lnd, builds a real GrpcLndServices with a real injected grpc.ClientConn exactly like LiT does, starts an embedded trader with the borrowed services, and asserts the injected conn does NOT enter connectivity.Shutdown after Stop(). TestOwnedLndConnClosedOnStop asserts an owned connection is still closed (no standalone leak). The subserver test fails on master ("subserver closed the shared lnd connection") and passes with the fix.
  • server_ownership_guard_test.go is a static guard (AST scan of the package, stdlib-only) that fails CI if any .Close() targets an lnd connection handle outside the whitelisted owner sites, or if StartAsSubserver ever accepts the closable wrapper again. Demonstrated on master: it flags the original bug site (server.go:753 s.lndServices.Close()), the standalone cleanup site and the wrapper-typed signature.
  • Full root package + account/funding/order/auctioneer tests pass; golangci-lint clean.

Skipped as low-value-after-type-level: a formal TLA+-style spec — the lifecycle has four states and one invariant ("a subserver stopping never closes the shared conn"), which is now enforced by the type checker, the regression tests and the static guard; the state machine is sketched in the litd docs PR instead.

The reconnect/restart work from lightning-terminal#644 is intentionally kept separate and out of scope.


View the OpenClaw team session

When running as a subserver inside lightning-terminal, the pool trader
server receives an lnd connection that is created and owned by the
parent process and shared among all subservers. On Stop, the server
unconditionally closed that connection, tearing down lnd access for
LiT itself and all other integrated subservers (loop, faraday, tapd,
accounts and the LiT macaroon service).

Track the connection ownership like faraday already does: the
standalone Start path marks the connection as owned, StartAsSubserver
marks it as borrowed and Stop only closes owned connections.

Add a regression test that starts an embedded trader server against a
mock lnd with a real injected grpc.ClientConn and asserts the
connection survives Stop, plus a test that an owned connection is
still closed on stop.

Refs lightninglabs/lightning-terminal#1381 (comment 6530026672).
Strengthen the ownership fix from a runtime flag to type-level
prevention: StartAsSubserver now accepts the non-closable
*lndclient.LndServices view instead of the *lndclient.GrpcLndServices
wrapper, so a subserver cannot call Close on the shared connection
even if it tries. The closable wrapper is only held in the new
lndOwner field, which is only set when the server creates the
connection itself (standalone Start).

Add a mechanically enforced guard test that fails CI if any code in
the package closes an lnd connection handle outside the whitelisted
owner sites, or if StartAsSubserver ever accepts the closable wrapper
again. The guard was verified to fail against master (flagging the
original s.lndServices.Close() bug site in Stop and the wrapper-typed
signature) and to pass with this change.
@litbot-9000

Copy link
Copy Markdown
Collaborator Author

/gateway review

@Roasbeef
Roasbeef merged commit 561c94b into lightninglabs:master Nov 4, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants