diff options
| author | Adam Soltys <[email protected]> | 2026-11-20 01:55:55 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-11-20 01:55:55 -0700 |
| commit | 8ec9a1e70d7b43455c89768f27187f15d5983a38 (patch) | |
| tree | dd5b9387672b212da2df7c5a27ba91a3fb505a6a | |
| parent | 6efa593d210370339c23c7cc01caa69a8dc233a5 (diff) | |
| download | coinosv3-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
| -rw-r--r-- | bridge/ln.js | 34 | ||||
| -rw-r--r-- | bridge/ln.test.js | 48 | ||||
| -rw-r--r-- | names/server.js | 11 | ||||
| -rw-r--r-- | package.json | 2 |
4 files changed, 89 insertions, 6 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); +}); diff --git a/names/server.js b/names/server.js index 34aae77..4d7a81c 100644 --- a/names/server.js +++ b/names/server.js @@ -310,7 +310,16 @@ function usableInvoice(pr, sat, network = null) { } catch { return false; } } -const ln = CFG.ln ? lnBackend(CFG.ln) : null; +// The node's socket file is mounted into this container; when the node is +// restarted the mount goes stale and every invoice fails until this process +// is restarted too (2026-11-12, 2026-11-20). Exit and let docker do that. +const ln = CFG.ln ? lnBackend({ + ...CFG.ln, + onDead: (e) => { + console.error(`lightning socket dead for a minute (${e.message}); exiting so the restart mounts the new one`); + process.exit(1); + }, +}) : null; // Optional mutinynet CLN — lets staging sell (play-money) hats. Nothing else // uses it: names, offers and forwards remain mainnet affairs. const lnMut = CFG.lnMut ? lnBackend(CFG.lnMut) : null; diff --git a/package.json b/package.json index 357f54f..1915df8 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "description": "Server-side services for coinos v3: the payment-name registrar, NWC push, Blossom sync storage, the public feed and media proxy", "license": "MIT", "scripts": { - "test": "bun test names/store.test.js names/forget.test.js", + "test": "bun test names/store.test.js names/forget.test.js bridge/ln.test.js", "names": "bun names/server.js", "nwcpush": "bun nwcpush/server.js", "blossom": "bun blossom/server.js", |
