From 984d2c3efeca25c37522c6fb17a9863717b44bc2 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sun, 16 Aug 2026 00:23:28 -0700 Subject: deps: patch @asoltys/clightning-client to fix cl reconnect Register patches/@asoltys+clightning-client+0.1.10.patch via patchedDependencies so bun install re-applies it. The library's reconnect() reused an already-dead socket and never recovered after a cl restart (looping connect ENOENT, leaking a socket+timer); the patch rebuilds a fresh socket + readline each reconnection and clears its retry guard before reconnecting. Companion to the app-side dropClient()/listener-recovery fixes. Co-Authored-By: Claude Opus 4.8 --- bun.lockb | Bin 235660 -> 235864 bytes package.json | 3 ++ patches/@asoltys+clightning-client+0.1.10.patch | 54 ++++++++++++++++++++++++ patches/README.md | 41 ++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 patches/@asoltys+clightning-client+0.1.10.patch create mode 100644 patches/README.md diff --git a/bun.lockb b/bun.lockb index f748ff18..cc0f1089 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 9554b72a..a08b9911 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,9 @@ "name": "coinos", "version": "0.0.1", "private": true, + "patchedDependencies": { + "@asoltys/clightning-client@0.1.10": "patches/@asoltys+clightning-client+0.1.10.patch" + }, "scripts": { "start": "bun index.ts", "dev": "nodemon --exec bun index.ts", diff --git a/patches/@asoltys+clightning-client+0.1.10.patch b/patches/@asoltys+clightning-client+0.1.10.patch new file mode 100644 index 00000000..f2d0d3d4 --- /dev/null +++ b/patches/@asoltys+clightning-client+0.1.10.patch @@ -0,0 +1,54 @@ +diff --git a/index.js b/index.js +index 968489c..4d96c34 100644 +--- a/index.js ++++ b/index.js +@@ -42,7 +42,31 @@ class LightningClient extends EventEmitter { + this.reconnectTimeout = null; + this.reqcount = 0; + +- this.client = net.createConnection(rpcPath); ++ this._setup(); ++ } ++ ++ _setup() { ++ // Build a FRESH socket + readline for every (re)connection. The original ++ // code reused this.client and only called this.client.connect() on ++ // reconnect — but a socket that has already emitted 'error'/'close' is ++ // destroyed, so reconnecting it is a no-op/throw. That left the client ++ // wedged looping `connect ENOENT …/lightning-rpc` forever after a cl ++ // restart (spamming logs, leaking a socket+timer, never recovering). ++ // Renewing clientConnectionPromise here also makes call() block until the ++ // (re)connection is live instead of writing into a dead socket. ++ if (this.client) { ++ try { ++ this.client.removeAllListeners(); ++ this.client.destroy(); ++ } catch (e) {} ++ } ++ if (this.rl) { ++ try { ++ this.rl.close(); ++ } catch (e) {} ++ } ++ ++ this.client = net.createConnection(this.rpcPath); + this.rl = readline.createInterface({ input: this.client }); + + this.clientConnectionPromise = new Promise((resolve) => { +@@ -87,8 +111,15 @@ class LightningClient extends EventEmitter { + } + + this.reconnectTimeout = setTimeout(() => { +- this.client.connect(this.rpcPath); ++ // Clear the guard BEFORE (re)connecting so a throw can't leave it set ++ // and permanently block every future reconnect() (the original bug). + this.reconnectTimeout = null; ++ try { ++ this._setup(); ++ } catch (e) { ++ this.increaseWaitTime(); ++ this.reconnect(); ++ } + }, this.reconnectWait * 1000); + } + diff --git a/patches/README.md b/patches/README.md new file mode 100644 index 00000000..b3bf1322 --- /dev/null +++ b/patches/README.md @@ -0,0 +1,41 @@ +# patches + +## `@asoltys+clightning-client+0.1.10.patch` + +Fixes the reconnect logic in `@asoltys/clightning-client` (v0.1.10) so a dropped +cl RPC connection actually recovers. + +**Bug:** `reconnect()` called `this.client.connect()` on the *same* socket that +had already emitted `error`/`close`. A destroyed socket can't be reconnected, so +after a `cl` restart the client wedged, looping `connect ENOENT …/lightning-rpc` +forever — spamming logs and leaking a socket+timer each time — and never +recovered. (If `connect()` threw, `reconnectTimeout` was also left set, which +permanently blocked every future `reconnect()`.) + +**Fix:** factor the socket+readline+handlers setup into `_setup()` which builds a +**fresh** socket each (re)connection and renews `clientConnectionPromise` (so +`call()` blocks until the reconnect is live). `reconnect()` now clears its guard +*before* reconnecting and retries on failure. + +This is defense-in-depth: the app already neutralises the broken loop in +`lib/ln.ts` (`dropClient()` tears the dead client down and the proxy makes a fresh +`LightningClient`). This patch fixes the library itself for any path that relies +on its own reconnect. + +### How to wire it in (bun) + +`bun patch` couldn't run in this checkout (node_modules link EEXIST), so this +patch was generated by hand and verified to apply cleanly (`git apply --check`). +To register it so `bun install` re-applies it, in a clean checkout: + +```sh +bun patch @asoltys/clightning-client +# copy patches/clightning-client-index.fixed.js over the printed editable +# node_modules/@asoltys/clightning-client/index.js +bun patch --commit node_modules/@asoltys/clightning-client +``` + +That writes bun's own `patches/…` file and the `patchedDependencies` entry in +`package.json`. Alternatively, apply this patch directly in the image build: +`git apply patches/@asoltys+clightning-client+0.1.10.patch` against the installed +package. `clightning-client-index.fixed.js` is the full patched file for reference. -- cgit v1.2.3