Skip to content

Prevent unhandled promise rejections in LN invoice status checks#37

Draft
Copilot wants to merge 2 commits into
masterfrom
copilot/fix-unhandled-promise-rejection
Draft

Prevent unhandled promise rejections in LN invoice status checks#37
Copilot wants to merge 2 commits into
masterfrom
copilot/fix-unhandled-promise-rejection

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 20, 2026

The service could terminate on ERR_UNHANDLED_REJECTION when LN invoice polling hit a rejected promise with an undefined reason. This PR hardens invoice-status checking so failures are contained and converted into safe, non-crashing outcomes.

  • Invoice wait flow hardening (src/invoicing.js)

    • Reworked check_invoice_is_paid to avoid new Promise(async ...), which can leak unhandled rejections from the async executor.
    • Split timeout and RPC wait into separately managed promises.
    • Wrapped the RPC path with explicit .catch(() => undefined) and used Promise.race(...) to return the first settled result.
    • Ensured timeout cleanup via finally to avoid stale timers.
  • Regression coverage (test/ln_flow.test.js)

    • Added a focused test that simulates LN RPC rejection with undefined and asserts:
      • check_invoice_is_paid(...) resolves to undefined
      • no unhandledRejection process event is emitted
const waitinvoicePromise = this.ln_rpc({ method: "waitinvoice", params })
  .then((res) => res.error ? false : true)
  .catch(() => undefined)

return await Promise.race([waitinvoicePromise, timeoutPromise])

Copilot AI and others added 2 commits May 20, 2026 19:03
Agent-Logs-Url: https://github.com/damus-io/api/sessions/cb180abc-925e-4c61-a500-ba8d094f4f4a

Co-authored-by: danieldaquino <24692108+danieldaquino@users.noreply.github.com>
Agent-Logs-Url: https://github.com/damus-io/api/sessions/cb180abc-925e-4c61-a500-ba8d094f4f4a

Co-authored-by: danieldaquino <24692108+danieldaquino@users.noreply.github.com>
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.

2 participants