aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ln.ts
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-06-04 18:17:47 -0700
committerAdam Soltys <[email protected]>2026-06-04 18:17:47 -0700
commitbd1ece38e42751575ac2e03a5c795b5b109653ac (patch)
treece5f648953b89db5ce94f6f8218ed35799a4f960 /lib/ln.ts
parentbe896868de55ef82b268d8c9f92c3f0ba857fed8 (diff)
downloadcoinos-server-bd1ece38e42751575ac2e03a5c795b5b109653ac.tar.gz
coinos-server-bd1ece38e42751575ac2e03a5c795b5b109653ac.zip
ln/lightning: watchdog recycles zombie receive-listener socket
On 2026-06-04 cl restarted (autoheal, after its 15s healthcheck timed out), and the app's waitanyinvoice long-poll hung on the now-dead listen socket. listenerActive stayed true forever, so ensureListenerAlive never restarted it, and incoming lightning payments stopped crediting for ~2h until a manual app restart. The per-call RPC timeout in ln.ts can't catch this: waitanyinvoice is intentionally in NO_TIMEOUT (it's meant to block). Fix: ensureListenerAlive (runs every 120s) now also detects a stalled listener — active but blocked in waitanyinvoice for >5min. It probes cl via the main ln socket (getinfo); only if cl is actually alive does it treat the listen socket as a zombie, call lnListen.reset() (new: drops the proxy's socket so the next call reconnects), and re-arm. A listenerEpoch guard makes the wedged invocation bow out when its waitanyinvoice finally rejects, so we never run two listeners concurrently and never double-credit. Verified with a state-machine test covering normal receive, quiet-but-healthy (left alone), stall+cl-alive (recycled), stall+cl-down (left alone), and the no-double-listener race. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Diffstat (limited to 'lib/ln.ts')
-rw-r--r--lib/ln.ts16
1 files changed, 16 insertions, 0 deletions
diff --git a/lib/ln.ts b/lib/ln.ts
index de01ea81..4215731e 100644
--- a/lib/ln.ts
+++ b/lib/ln.ts
@@ -144,6 +144,22 @@ function lightningProxy(rpcPath: string) {
if (prop === "toString") return () => "[LightningProxy]";
if (prop === Symbol.toStringTag) return "LightningProxy";
+ // Force the underlying socket to be dropped so the next call reconnects.
+ // Used by the lightning-listener watchdog to recover a zombie long-poll
+ // (waitanyinvoice blocked on a dead socket while cl is actually alive —
+ // the per-call timeout can't catch it because long-polls are exempt).
+ if (prop === "reset") {
+ return () => {
+ const had = !!client;
+ try {
+ client?.removeAllListeners?.();
+ client?.destroy?.();
+ } catch (_) {}
+ client = null;
+ return had;
+ };
+ }
+
return async (...args: any[]) => {
const c = ensure();
const v = c[prop as any];