Skip to content

Harden API against downed lightning node#38

Closed
Copilot wants to merge 3 commits into
masterfrom
copilot/fix-api-issues-with-lightning-node
Closed

Harden API against downed lightning node#38
Copilot wants to merge 3 commits into
masterfrom
copilot/fix-api-issues-with-lightning-node

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 20, 2026

A downed LN node caused ECONNREFUSED to surface as an unhandled socket 'error' event, crashing the entire server process. A secondary bug in check_invoice_is_paid — the new Promise(async ...) anti-pattern — let ln_rpc rejections escape as unhandled promise rejections, also fatal in Node.js v15+.

Fixes

  • src/index.js — Global process.on('uncaughtException') and process.on('unhandledRejection') handlers as last-resort safety nets; logs the error and keeps the process alive
  • src/invoicing.js — Rewrote check_invoice_is_paid to eliminate the new Promise(async ...) anti-pattern:
    // Before: async executor errors silently escaped as unhandled rejections
    return new Promise(async (resolve, reject) => {
      const res = await this.ln_rpc(...)  // rejection here → unhandled
      resolve(res.error ? false : true)
    })
    
    // After: explicit error containment
    const rpc_promise = this.ln_rpc({ method: "waitinvoice", params })
      .then(res => res.error ? false : true)
      .catch((e) => { error("Error checking invoice..."); return undefined })
    return Promise.race([timeout_promise, rpc_promise])
    Also added timer.unref() on the timeout so it doesn't block process exit.
  • src/router_config.js — Added missing try/catch to POST /ln-checkout/:id/check-invoice and PUT /admin/ln-checkout/new-verified-checkout; added Express error-handling middleware ((err, req, res, next)) at the end of config_router as a final catch-all for unhandled route errors

Tests

  • Extended MockLNNodeController with simulate_node_down() / simulate_node_up() that causes connect_and_init to reject
  • Added LN Flow — Downed LN node does not crash the server covering: verify-checkout with node down (→ 400), check-invoice with node down (→ 200 with paid: undefined), background poll with node down (no throw), and server still responsive after all failures

Copilot AI and others added 2 commits May 20, 2026 20:56
…e server

Agent-Logs-Url: https://github.com/damus-io/api/sessions/49edca8a-52fd-4a6b-8835-4821190f5c17

Co-authored-by: danieldaquino <24692108+danieldaquino@users.noreply.github.com>
…uter catch

Agent-Logs-Url: https://github.com/damus-io/api/sessions/49edca8a-52fd-4a6b-8835-4821190f5c17

Co-authored-by: danieldaquino <24692108+danieldaquino@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix API crash caused by downed lightning node Harden API against downed lightning node May 20, 2026
Copilot AI requested a review from danieldaquino May 20, 2026 21:01
@danieldaquino
Copy link
Copy Markdown
Collaborator

Superseded by #40

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.

Downed lightning node can take down our API

2 participants