aboutsummaryrefslogtreecommitdiffstats
path: root/bridge
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-11-20 01:55:55 -0700
committerAdam Soltys <[email protected]>2026-11-20 01:55:55 -0700
commit8ec9a1e70d7b43455c89768f27187f15d5983a38 (patch)
treedd5b9387672b212da2df7c5a27ba91a3fb505a6a /bridge
parent6efa593d210370339c23c7cc01caa69a8dc233a5 (diff)
downloadcoinosv3-server-8ec9a1e70d7b43455c89768f27187f15d5983a38.tar.gz
coinosv3-server-8ec9a1e70d7b43455c89768f27187f15d5983a38.zip
names restarts itself when the Lightning node's socket goes stale. "tried to zap pnk but it said names.coinos.io could not create an invoice" (Adam, 2026-11-20). bcln was recreated at 04:26Z; names has the node's socket file mounted, the mount kept pointing at the old file, and every @coinos.io LNURL invoice failed with ECONNREFUSED for four and a half hours until names was restarted by hand. The same thing cost 13 hours on 2026-11-11. The socket client now counts refused connections: after a minute of nothing but refusals (ten minutes if this process never reached the node, so a node that is down doesn't restart names in a loop) names logs it and exits, and docker's restart mounts the new socket. Only the mainnet node does this; the mutinynet one is left alone. New bridge/ln.test.js passes (4 checks), with the store and forget tests.
Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01VMan6Upic5L6MMJzkTn8zg
Diffstat (limited to 'bridge')
-rw-r--r--bridge/ln.js34
-rw-r--r--bridge/ln.test.js48
2 files changed, 78 insertions, 4 deletions
diff --git a/bridge/ln.js b/bridge/ln.js
index dce5254..7d1974c 100644
--- a/bridge/ln.js
+++ b/bridge/ln.js
@@ -33,8 +33,34 @@ function dockerAdapter({ container, network }) {
};
}
-function socketAdapter({ path }) {
+// A socket that refuses every call for a minute is not coming back by itself
+// when the socket FILE is bind-mounted into a container: lightningd makes a
+// new file when it restarts, and the mount still points at the old one. The
+// owner gets told through cfg.onDead (names exits there, and docker's restart
+// mounts the new file). `quiet` is how long before it may fire: a minute once
+// this process has reached the node, ten if it never has, so a node that is
+// simply down doesn't restart its client in a loop.
+export function deadSocketWatch(onDead, { streakMs = 60_000, coldMs = 600_000, now = Date.now } = {}) {
+ const born = now();
+ let since = 0, fails = 0, everUp = false, fired = false;
+ return {
+ ok() { everUp = true; since = 0; fails = 0; },
+ fail(err) {
+ if (!['ECONNREFUSED', 'ENOENT', 'ENOTSOCK'].includes(err?.code)) return;
+ const t = now();
+ if (!since) since = t;
+ fails++;
+ if (fired || fails < 3 || t - since < streakMs) return;
+ if (!everUp && t - born < coldMs) return;
+ fired = true;
+ onDead(err);
+ },
+ };
+}
+
+function socketAdapter({ path, onDead }) {
let id = 0;
+ const watch = onDead ? deadSocketWatch(onDead) : null;
return {
call(method, params = {}) {
return new Promise((resolve, reject) => {
@@ -43,11 +69,11 @@ function socketAdapter({ path }) {
// process dies with an uncaught exception.
const sock = new Socket();
let buf = '';
- sock.on('error', reject);
+ sock.on('error', (e) => { watch?.fail(e); reject(e); });
sock.connect(path);
- sock.on('connect', () => sock.write(JSON.stringify({
+ sock.on('connect', () => { watch?.ok(); sock.write(JSON.stringify({
jsonrpc: '2.0', id: ++id, method, params,
- }) + '\n'));
+ }) + '\n'); });
sock.on('data', (d) => {
buf += d;
let nl;
diff --git a/bridge/ln.test.js b/bridge/ln.test.js
new file mode 100644
index 0000000..b9fb731
--- /dev/null
+++ b/bridge/ln.test.js
@@ -0,0 +1,48 @@
+import { test, expect } from 'bun:test';
+import { deadSocketWatch } from './ln.js';
+
+const refused = Object.assign(new Error('connect ECONNREFUSED'), { code: 'ECONNREFUSED' });
+const rig = () => {
+ let t = 0, dead = 0;
+ const w = deadSocketWatch(() => dead++, { now: () => t });
+ return { w, at: (ms) => { t = ms; }, dead: () => dead };
+};
+
+test('a socket that worked, then refuses for a minute, is reported once', () => {
+ const r = rig();
+ r.w.ok();
+ r.at(1000); r.w.fail(refused);
+ r.at(30_000); r.w.fail(refused);
+ r.at(59_000); r.w.fail(refused);
+ expect(r.dead()).toBe(0);
+ r.at(62_000); r.w.fail(refused);
+ r.at(120_000); r.w.fail(refused);
+ expect(r.dead()).toBe(1);
+});
+
+test('a success in between starts the count again', () => {
+ const r = rig();
+ r.w.ok();
+ r.at(1000); r.w.fail(refused);
+ r.at(40_000); r.w.fail(refused);
+ r.at(50_000); r.w.ok();
+ r.at(70_000); r.w.fail(refused);
+ r.at(80_000); r.w.fail(refused);
+ r.at(90_000); r.w.fail(refused);
+ expect(r.dead()).toBe(0);
+});
+
+test('a node that was never reached gets ten minutes', () => {
+ const r = rig();
+ for (let t = 0; t < 590_000; t += 30_000) { r.at(t); r.w.fail(refused); }
+ expect(r.dead()).toBe(0);
+ r.at(601_000); r.w.fail(refused);
+ expect(r.dead()).toBe(1);
+});
+
+test('an error from the node itself is not a dead socket', () => {
+ const r = rig();
+ r.w.ok();
+ for (let t = 0; t < 300_000; t += 30_000) { r.at(t); r.w.fail(new Error('{"code":-1}')); }
+ expect(r.dead()).toBe(0);
+});